鱼眼畸变矫正之-径向矫正

1.理论介绍

根据等距映射的原理对鱼眼图像的的径向畸变进行矫正,其中假定图像是通过等距投影算法映射得到。
等距投影的公式:

r = f ∗ θ ( 1 ) r = f*\theta (1) r=fθ1
,where theta θ is projection angle,f the focal length of camera ,whihc shows the projection distance is in proporation to θ.
在这里插入图片描述
p(X,Y,Z)为空间中一点,经过鱼眼镜头点P1(X1,Y1,Z1)之后折射到图像P’(U,V),图像矫正的过程就是找到P点和P’点过程坐标关系的过程。(实际所成图像在下面)
θ为入射光线pp1和XOZ平面所成的夹角,φ为点(X1,Y1)也即(U,V)与X轴所成夹角,通过P点到P’点之间的映射关系可得和公式(1):
r = s q r t ( X 2 + Y 2 ) ( 2 ) r = sqrt(X^2 + Y^2 ) (2) r=sqrt(X2+Y2)2
a r c t a n θ = ( r / Z ) , θ = a r c t a n ( r / Z ) ( 3 ) arctan\theta = (r/Z),\theta = arctan(r/Z)(3) arctanθ=(r/Z),θ=arctan(r/Z)(3)
t a n φ = ( Y / X ) , φ = a r c t a n ( Y / X ) ( 4 ) tanφ = (Y/X),φ = arctan(Y/X)(4) tanφ=(Y/X),φ=arctan(Y/X)(4)
U = r 1 ∗ c o s ( φ ) , V = r 1 ∗ s i n ( φ ) ( 5 ) U = r1 * cos(φ),V = r1* sin(φ)(5) U=r1cos(φ),V=r1sin(φ)(5)
根据(1)式可得:
r 1 = f ∗ θ ( 6 ) r1 = f*\theta(6) r1=fθ(6)
根据(2)~(6)式可得:
U = f ∗ a r c t a n ( s q r t ( X 2 + Y 2 ) / Z ) ∗ c o s ( Y / X ) ( 6 ) U =f* arctan(sqrt(X^2 + Y^2 )/Z) *cos(Y/X) (6) U=farctan(sqrt(X2+Y2)/Z)cos(Y/X)(6)
V = f ∗ a r c t a n ( s q r t ( X 2 + Y 2 ) / Z ) ∗ s i n ( Y / X ) ( 7 ) V =f* arctan(sqrt(X^2 + Y^2 )/Z) *sin(Y/X) (7) V=farctan(sqrt(X2+Y2)/Z)sin(Y/X)(7)

2.Python implementation

import cv2
import numpy as np
import math
import time
import matplotlib.pyplot as plt
foc = 180
if __name__ == '__main__':

    src = cv2.imread('timg.jpg')
    begin = time.time()
    (h, w) = src.shape[:2]

    # print(h, w)
    print(h,w)
    origin = (h/2, w/2)
    R = max(w, h)/2
    image_out = np.zeros(src.shape, src.dtype)
    print(image_out.shape)
    for i in range(h):  # u->x-axis-horizantal, v->y-axis-vertical
        for j in range(w):
            x = i - origin[0]
            y = j - origin[1]
            r = math.sqrt(x*x + y*y)
            theta = math.atan(r/R)
            # r1 and r are respectively the radius of coordination in object image(u,v) and source image(i,j)
            r1 = foc * theta
            if x == 0:
                alpha = math.pi* 90/180
            else:
                alpha = math.atan(y/x)
            uu = r1 * math.cos(alpha)
            vv = r1* math.sin(alpha)
            if(x <0 and y >0):
                uu = -uu
                vv = -vv
            if(x < 0 and y < 0):
                uu = -uu
                vv = -vv

            u = int(uu+origin[0])
            v = int(vv +origin[1])
            # print(u,v)
            if 0 <= u < h and 0 <= v < w:
                image_out[i][j] = src[u][v]
    end = time.time()

    interval = end - begin
    print("The interval consumed is ", interval)
    cv2.imwrite('fishEyeDistoration_dis.jpg', image_out)

    # imgs = np.hstack([src, image_out])
    # cv2.imshow("Correction", imgs)
    # cv2.waitKey()

3.Result

原图:
在这里插入图片描述

矫正后图像:
在这里插入图片描述

理论参考文献:
[1]https://blog.csdn.net/Gavinv/article/details/78386465?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
代码参看文章:
[1]https://blog.csdn.net/smallflyingpig/article/details/56687436?utm_source=blogxgwz9

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
鱼眼图像畸变校正的透视变换原理如下: 1. 首先,需要确定图像中的两个灭点,这可以通过球面透视投影得到。 2. 然后,根据灭点的位置,确定仿射变换矩阵,将图像进行仿射变换。 3. 最后,对变换后的图像进行裁剪,得到最终的校正图像。 下面是一个使用OpenCV进行鱼眼图像畸变校正的Python代码示例: ```python import cv2 import numpy as np # 读取鱼眼图像 img = cv2.imread('fisheye.jpg') # 确定灭点 h, w = img.shape[:2] K = np.array([[w, 0, w/2], [0, w, h/2], [0, 0, 1]]) D = np.array([0, 0, 0, 0]) newK = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K, D, (w, h), np.eye(3)) map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), newK, (w, h), cv2.CV_16SC2) img_undistorted = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) # 确定仿射变换矩阵 src_points = np.float32([[0, 0], [w-1, 0], [0, h-1], [w-1, h-1]]) dst_points = np.float32([[w/4, h/4], [w*3/4, h/4], [0, h], [w, h]]) M = cv2.getPerspectiveTransform(src_points, dst_points) # 进行仿射变换 img_transformed = cv2.warpPerspective(img_undistorted, M, (w, h)) # 裁剪图像 img_corrected = img_transformed[int(h/4):int(h*3/4), int(w/4):int(w*3/4)] # 显示结果 cv2.imshow('Original Image', img) cv2.imshow('Undistorted Image', img_undistorted) cv2.imshow('Transformed Image', img_transformed) cv2.imshow('Corrected Image', img_corrected) cv2.waitKey(0) cv2.destroyAllWindows() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值