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

标签 python opencv

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

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

这段代码是一个实验,尝试在执行un DistorImage()之后获取输出位置。首先,我在原始图像上画一个红点,例如(300,300)。然后,我使用 unactorImage() 来实现鱼眼效果,因此红点可能位于不同的位置。但是,我不知道如何计算正确的位置。

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

最佳答案

调用fisheye.un DistorImage时,您可以使用Knew指定扭曲图像的相机矩阵。当您调用fisheye.unactorPoints时,您需要执行相同的操作。如果您阅读文档,您将看到您可以给出一个 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.un DistorImage后一个点的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54143125/

相关文章:

python - 检查列表中的任何元素是否与 MySQL 表中的行匹配

python - 从对角线开始将向量写入矩阵

c++ - OpenCV VideoCapture 根本无法从我的网络摄像头读取

iphone - 在 NSDictionary 中存储/提取对象

android - 如何使用 javacv/opencv 在 android 中将音频与视频集成?

python - 显示 NLTK 中的标签概率/置信度

Python - 比较两个字符串的最佳方法,记录比较特定项目序列位置的统计数据?

python - 导入错误: No module named 'matplotlib'

c++ - 从数组创建或 reshape OpenCV 3 channel 垫

c++ - OpenCV 使用步幅从数组生成 cv::Mat