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

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 θ.
鱼眼畸变矫正之-径向矫正_第1张图片
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

原图:
鱼眼畸变矫正之-径向矫正_第2张图片

矫正后图像:
鱼眼畸变矫正之-径向矫正_第3张图片

理论参考文献:
[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

你可能感兴趣的:(计算机视觉,python,算法,opencv)