gpt4 book ai didi

python - OpenCV:如何获取cv2.fisheye.undistortImage后一点的位置?

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:11 24 4
gpt4 key购买 nike

我正在尝试创建鱼眼效果,但我不熟悉 cv2.fisheye.undistortImage()cv2.fisheye.undistortPoints()。这是我的代码:

import cv2
import glob
import numpy as np

# grab a list of jpg from one folder
img_list = glob.glob('/home/folder/*.jpg')

# get the K and D for cv2.fisheye.undistortImage
K = np.array([[900.,0,512],
[0,900.,288],
[0.,0.,1]])

D = np.array([4.,100.,0.,0.])

# create a random point
point = np.array([[[300.,300.],[300,400],[400,400],[400,300]]])

# get a knew for cv2.fisheye.undistortImage
Knew = K.copy()
Knew[(0,1),(0,1)] = Knew[(0,1),(0,1)]


for img_path in img_list:

img = cv2.imread(img_path)

# draw all four points on the original image
cv2.circle(img,(300,300), 5, (0,0,255), -1)
cv2.circle(img,(400,300), 5, (0,0,255), -1)
cv2.circle(img,(300,400), 5, (0,0,255), -1)
cv2.circle(img,(400,400), 5, (0,0,255), -1)

# distort the images
img_out = cv2.fisheye.undistortImage(img,K,D=D,Knew=Knew)
# send the points to cv2.fisheye.undistortPoints
point_out = cv2.fisheye.undistortPoints(point,K =K1, D=D)

# looks like the out is around (-1,1) so i tried to recover back
#cv2.circle(img,(600,300), 5, (0,0,255), -1)
x = int((512.+(point_out[0][0][0]*1024)))
y = int((288.+(point_out[0][0][1]*576)))
cv2.circle(img_out,(x,y), 5, (0,255,0), 1)
x = int((512.+(point_out[0][1][0]*1024)))
y = int((288.+(point_out[0][1][1]*576)))
cv2.circle(img_out,(x,y), 5, (0,255,0), 1)
x = int((512.+(point_out[0][2][0]*1024)))
y = int((288.+(point_out[0][2][1]*576)))
cv2.circle(img_out,(x,y), 5, (0,255,0), 1)
x = int((512.+(point_out[0][3][0]*1024)))
y = int((288.+(point_out[0][3][1]*576)))
cv2.circle(img_out,(x,y), 5, (0,255,0), 1)

# save the img
cv2.imwrite(img_path+'.jpg',img_out,[100])

enter image description here

此代码是尝试在执行undistortImage() 后获取输出位置的实验。首先,我在原始图像上绘制一个红色点,例如 (300,300)。然后,我使用 undistortImage() 来做一个鱼眼效果,所以红点可能位于不同的位置。但是,我不知道如何计算正确的位置。

我认为 undistortPoints() 可能会得到正确的位置。不幸的是,绿色圆圈表明我的方法是错误的。有人能告诉我如何获得正确的值(value)吗?

最佳答案

调用 fisheye.undistortImage 时,您可以使用 Knew 为失真图像指定相机矩阵。当您调用 fisheye.undistortPoints 时,您需要做同样的事情。如果您阅读文档,您会发现您可以提供一个 P 参数,即“新相机矩阵 (3x3) 或新投影矩阵 (3x4)”。这就是您想要的。

for img_path in img_list:

img = cv2.imread(img_path)

# draw all four points on the original image
cv2.circle(img,(300,300), 5, (0,0,255), -1)
cv2.circle(img,(400,300), 5, (0,0,255), -1)
cv2.circle(img,(300,400), 5, (0,0,255), -1)
cv2.circle(img,(400,400), 5, (0,0,255), -1)

# distort the images
img_out = cv2.fisheye.undistortImage(img,K,D=D,Knew=Knew)
# send the points to cv2.fisheye.undistortPoints
point_out = cv2.fisheye.undistortPoints(point,K =K, D=D, P=Knew)[0]
for point in point_out:
cv2.circle(img_out, (int(point[0]), int(point[1])), 5, (0,255,0), 1)

# save the img
cv2.imwrite(img_path+'.jpg',img_out,[100])

关于python - OpenCV:如何获取cv2.fisheye.undistortImage后一点的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54143125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com