116 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			116 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
 | 
						|
# Copyright (C) 2025 Roz K <roz@rozk.net>
 | 
						|
#
 | 
						|
# This program is free software: you can redistribute it and/or modify
 | 
						|
# it under the terms of the GNU Affero General Public License as
 | 
						|
# published by the Free Software Foundation, either version 3 of the
 | 
						|
# License, or (at your option) any later version.
 | 
						|
#
 | 
						|
# This program is distributed in the hope that it will be useful,
 | 
						|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
						|
# GNU Affero General Public License for more details.
 | 
						|
#
 | 
						|
# You should have received a copy of the GNU Affero General Public License
 | 
						|
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
						|
 | 
						|
import math
 | 
						|
 | 
						|
print("\n--- physical sensor ---\n")
 | 
						|
 | 
						|
# https://www.sony-semicon.com/files/62/pdf/p-13_IMX677-AAPH5-J_Flyer.pdf
 | 
						|
 | 
						|
sensor_pixel_size = 1.12 * 0.001 # mm
 | 
						|
 | 
						|
# https://community.gopro.com/s/article/HERO11-Black-Video-Settings-And-Resolutions
 | 
						|
 | 
						|
gopro_array_horizontal = 5312 # p
 | 
						|
gopro_array_vertical   = 4648 # p
 | 
						|
 | 
						|
gopro_texture_horizontal = 3840 # p
 | 
						|
gopro_texture_vertical   = 3360 # p
 | 
						|
 | 
						|
gopro_size_horizontal = gopro_array_horizontal * sensor_pixel_size
 | 
						|
gopro_size_vertical   = gopro_array_vertical   * sensor_pixel_size
 | 
						|
gopro_size_diagonal   = math.hypot(gopro_size_horizontal, gopro_size_vertical)
 | 
						|
 | 
						|
print("gopro  size : horizontal = %7.3f, vertical = %7.3f, diagonal = %7.3f (mm)" % (
 | 
						|
    gopro_size_horizontal,
 | 
						|
    gopro_size_vertical,
 | 
						|
    gopro_size_diagonal))
 | 
						|
 | 
						|
# https://thinglabs.io/gopro-focal-length-guide
 | 
						|
 | 
						|
gopro_focal_length = 2.92
 | 
						|
 | 
						|
gopro_radius_horizontal = gopro_size_horizontal * 0.5
 | 
						|
gopro_radius_vertical   = gopro_size_vertical   * 0.5
 | 
						|
gopro_radius_diagonal   = gopro_size_diagonal   * 0.5
 | 
						|
 | 
						|
# https://en.wikipedia.org/wiki/Fisheye_lens
 | 
						|
 | 
						|
equidistant_angle  = lambda radius: math.degrees(radius / gopro_focal_length)
 | 
						|
 | 
						|
gopro_fov_horizontal = 2.0 * equidistant_angle(gopro_radius_horizontal)
 | 
						|
gopro_fov_vertical   = 2.0 * equidistant_angle(gopro_radius_vertical)
 | 
						|
gopro_fov_diagonal   = 2.0 * equidistant_angle(gopro_radius_diagonal)
 | 
						|
 | 
						|
print("gopro  fov  : horizontal = %7.3f, vertical = %7.3f, diagonal = %7.3f (deg)" % (
 | 
						|
    gopro_fov_horizontal,
 | 
						|
    gopro_fov_vertical,
 | 
						|
    gopro_fov_diagonal))
 | 
						|
 | 
						|
print("\n--- rectilinear re-projection ---\n")
 | 
						|
 | 
						|
rectilinear_focal_length = lambda radius: radius * math.tan(math.radians(90.0 - equidistant_angle(radius)))
 | 
						|
 | 
						|
rectilinear_focal_length_horizontal = rectilinear_focal_length(gopro_radius_horizontal)
 | 
						|
rectilinear_focal_length_vertical   = rectilinear_focal_length(gopro_radius_vertical)
 | 
						|
rectilinear_focal_length_diagonal   = rectilinear_focal_length(gopro_radius_diagonal)
 | 
						|
 | 
						|
print("linear focal: horizontal = %7.3f, vertical = %7.3f, diagonal = %7.3f (mm)" % (
 | 
						|
    rectilinear_focal_length_horizontal,
 | 
						|
    rectilinear_focal_length_vertical,
 | 
						|
    rectilinear_focal_length_diagonal))
 | 
						|
 | 
						|
rectilinear_radius = lambda angle:  rectilinear_focal_length_vertical * math.tan(math.radians(angle))
 | 
						|
rectilinear_angle  = lambda radius: math.degrees(math.atan(radius / rectilinear_focal_length_vertical))
 | 
						|
 | 
						|
rectilinear_fov_horizontal = 2.0 * rectilinear_angle(gopro_radius_horizontal)
 | 
						|
rectilinear_fov_vertical   = 2.0 * rectilinear_angle(gopro_radius_vertical)
 | 
						|
rectilinear_fov_diagonal   = 2.0 * rectilinear_angle(gopro_radius_diagonal)
 | 
						|
 | 
						|
print("linear fov  : horizontal = %7.3f, vertical = %7.3f, diagonal = %7.3f (deg)" % (
 | 
						|
    rectilinear_fov_horizontal,
 | 
						|
    rectilinear_fov_vertical,
 | 
						|
    rectilinear_fov_diagonal))
 | 
						|
 | 
						|
print("\n--- shader parameters ---\n")
 | 
						|
 | 
						|
output_size_horizontal = 2.0 * rectilinear_radius(gopro_fov_horizontal * 0.5)
 | 
						|
output_size_vertical   = gopro_size_vertical
 | 
						|
 | 
						|
output_scale_horizontal = output_size_horizontal / gopro_size_horizontal
 | 
						|
 | 
						|
print("""// parameters\n
 | 
						|
const vec2  sensor_size              = vec2(%.6f, %.6f);
 | 
						|
const vec2  rectilinear_scale        = vec2(%.6f, 1.0);
 | 
						|
const float equidistant_focal_length = %.6f;
 | 
						|
const float rectilinear_focal_length = %.6f;""" % (
 | 
						|
    gopro_size_horizontal, gopro_size_vertical,
 | 
						|
    output_scale_horizontal,
 | 
						|
    gopro_focal_length,
 | 
						|
    rectilinear_focal_length_vertical)
 | 
						|
)
 | 
						|
 | 
						|
print("\n--- filters parameters ---\n")
 | 
						|
 | 
						|
def print_output_array(array_vertical):
 | 
						|
    array_horizontal = (array_vertical / gopro_array_vertical) * gopro_array_horizontal * output_scale_horizontal
 | 
						|
    print("resize: width = %d, height = %d" % (
 | 
						|
        round(array_horizontal / 4.0) * 4,
 | 
						|
        array_vertical))
 | 
						|
 | 
						|
print_output_array(2160)
 | 
						|
print_output_array(1080)
 |