图像处理:用固有矩阵将正常图像转换为鱼眼图像

2024-09-20 19:15:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要合成许多鱼眼图像与不同的内在矩阵基于正常的图片。我遵循了这个paper中提到的方法。在

理想情况下,如果算法正确,理想的鱼眼效果应该如下所示:

ideal fish eye effect。在

但当我用我的算法来转换图片时

originial image

看起来像这样

effect

下面是我的代码流程: 1首先,我用cv2读取原始图像

def read_img(image):

    img = ndimage.imread(image) #this would return a 4-d array: [R,G,B,255]
    img_shape = img.shape
    print(img_shape)

    #get the pixel coordinate
    w = img_shape[1] #the width
    # print(w)
    h= img_shape[0] #the height
    # print(h)
    uv_coord = []
    for u in range(w):
    for v in range(h):
        uv_coord.append([float(u),float(v)])  #this records the coord in the fashion of [x1,y1],[x1, y2], [x1, y3]....
    return np.array(uv_coord)

然后,根据论文:

r(θ)=k1θ+k2θ^3+k3θ^5+k4θ^7,(1) 式中,Ks为畸变系数

给定针孔投影图像中的像素坐标(x,y),鱼眼中对应的图像坐标(x',y')可以计算为:

x'=r(θ)cos(ψ),y'=r(θ)sin(ψ),<2)

式中,ψ=arctan((y−y0)/(x−x0)),且(x0,y0)是针孔投影图像中主点的坐标。在

然后图像坐标(x',y')转换为像素坐标(xf,yf):(xf,yf): *xf=mu*x'+u0,yf=mv*y'+v0,*(3)

其中(u0,v0)是鱼眼中主要点的坐标,mu,mv表示水平和垂直方向上每单位距离的像素数。所以我猜测,从内部矩阵中,fx,fy和u0 v0就是[cx,cy]。在

^{pr2}$

然后,在获得离散坐标后,我以这种方式移动像素,我认为问题可能是:

def main():
    image_name = "original.png"
    img = cv2.imread(image_name)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) #the cv2 read the image as BGR

    w = img.shape[1]
    h = img.shape[0]
    uv_coord = read_img(image_name)

    #for adding distortion
    dmatrix = [-0.391942708316175,0.012746418822063 ,-0.001374061848026 ,0.005349692659231]

    #the Intrinsic matrix of the original picture's 
    Kmatrix = np.array([9.842439e+02,9.808141e+02 , 1392/2, 2.331966e+02, 0.000000e+00])

    # Kmatrix = np.array([2234.23470710156  ,2223.78349134123,  947.511596277837,   647.103139639432,-3.20443253476976]) #the distorted intrinsics
    uv = add_distortion(uv_coord,dmatrix,Kmatrix)

    i = 0
    dstimg = np.zeros_like(img)

    for x in range(w):   #tthe coo
        for y in range(h):
           if i > (512 * 1392 -1):
               break

            xu = uv[i][0] #x, y1, y2, y3
            yu = uv[i][1]
            i +=1

            # if new pixel is in bounds copy from source pixel to destination pixel
            if 0 <= xu and xu < img.shape[1] and 0 <= yu and yu < img.shape[0]:
                dstimg[int(yu)][int(xu)] = img[int(y)][int(x)]

    img = Image.fromarray(dstimg, 'RGB')
    img.save('my.png')
    img.show()

但是,这段代码并没有按照我想要的方式执行。你们能帮我调试一下吗?我花了三天时间,但还是看不出有什么问题。谢谢!!在


Tags: thein图像imageimgfornprange

热门问题