【问题标题】:opencv undistortPoints doesn't undistortopencv undistortPoints 不会不失真
【发布时间】:2021-07-19 06:45:33
【问题描述】:

我有一台带鱼眼镜头的相机。我成功地不扭曲我的图像,但我想不扭曲点坐标。我知道地图包含 dst 中每个像素的 src 图像坐标,所以我不能直接使用没有一些迭代算法的那些。我认为 undistortPoints() 会这样做,但它错误地转换了第一点并且它不会改变第二点。使用 undistortPointsIter() 并设置更高的标准也不起作用。

之前有人问过类似的问题,this answer 也对我不起作用。我使用 undistortPoints() 得到完全相同的结果。

那么如何从失真图像中的像素点获取未失真图像的像素点?

我的相机参数:

print(K)
print(D)
print(Dims)

[[338.37324094   0.         319.5       ]
 [  0.         339.059099   239.5       ]
 [  0.           0.           1.        ]]
[[ 0.01794191]
 [-0.12190366]
 [ 0.14111533]
 [-0.09602948]]
(640, 480)

我的代码:

img = cv2.imread('Chessboards\img_021.jpg')
img_undistorted = cv2.remap(img, unfishmap1, unfishmap2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) 

points1=[]
points1.append((165,260))
points1.append((175,410))

print(points1)

img2= img.copy()
for p in points1:
    cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)

point_matrix = np.zeros(shape=(len(points1),1,2),dtype=np.float32)

for i in range(0, len(points1)):
    point_matrix[i][0][0] = points1[i][0]
    point_matrix[i][0][1] = points1[i][1]
    
print(point_matrix)
    
points_undistorted = cv2.undistortPoints(point_matrix,K,D,R=None,P=K)
points2=[]
for p in points_undistorted:
    points2.append( (int(p[0][0]),int(p[0][1])) )
  
print(points2)

img2= img_undistorted.copy()
for p in points2:
    cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)

#expected
points3=[]
points3.append((155,265))
points3.append((150,443))

print(points3)

img2= img_undistorted.copy()
for p in points3:
    cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)

结果:

distorted

undistorted with undistortPoints

undistorted with expected points

【问题讨论】:

  • 如果 yoz 使用正确的 K 矩阵(来自未失真图像的那个?)如果你在 initUndistRectifyMap 函数中使用它,那么你正在使用 undistortPoints 函数的鱼眼变体。 cv2.fisheye.undistortPoints?:
  • @Micka 我使用了错误的功能。我仍然得到错误的结果。我正在更新我的帖子。
  • 你能分享你如何初始化地图的代码吗?我可以告诉你它是如何为我工作的(确保在任何地方都使用鱼眼版本):cv::fisheye::estimateNewCameraMatrixForUndistortRectify 来计算新的相机矩阵K_undist。然后cv::fisheye::initUndistortRectifyMapK_distK_undist。之后使用 cv::fisheye::undistortPointsK_dist AND K_undist int 他同一个电话。
  • @Micka 我正要分享该代码,但现在它可以工作了!谢谢!我将在答案中分享对我有用的方法。

标签: python opencv camera-calibration opencv-python fisheye


【解决方案1】:

感谢micka提供的解决方案。

这对我有用:

source image

import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 150
import cv2
import copy
import os
import numpy as np

def imgshow(img):
    if len(img.shape) == 3:
        plt.imshow(img[:, :, ::-1])
    else:
        plt.imshow(img,cmap='gray',vmin=0, vmax=255)
    plt.show() 


K = np.array( [[338.37324094,0,319.5],[0,339.059099,239.5],[0,0,1]],dtype=np.float64)
D = np.array( [[ 0.01794191], [-0.12190366],[ 0.14111533],[-0.09602948]],dtype=np.float64)
new_size = (640, 480)

Knew = K.copy()
# alpha = 0.6
# Knew, roi = cv2.getOptimalNewCameraMatrix(K, D, new_size, alpha, new_size,centerPrincipalPoint = True)
    
unfishmap1, unfishmap2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), Knew, new_size, cv2.CV_32F)
unfishmap1, unfishmap2 = cv2.convertMaps(unfishmap1, unfishmap2, cv2.CV_16SC2)

img = cv2.imread('3FYUT.jpg')
img_undistorted = cv2.remap(img, unfishmap1, unfishmap2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) 

points1=[]
points1.append((165,260))
points1.append((175,410))

print(points1)

img2= img.copy()
for p in points1:
    cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)

point_matrix = np.zeros(shape=(len(points1),1,2),dtype=np.float32)

for i in range(0, len(points1)):
    point_matrix[i][0][0] = points1[i][0]
    point_matrix[i][0][1] = points1[i][1]
        
points_undistorted = cv2.fisheye.undistortPoints(point_matrix,K,D,P=Knew)

points2=[]
for p in points_undistorted:
    points2.append( (int(p[0][0]),int(p[0][1])) )

print("fisheye.undistortPoints:")    
print(points2)

img2= img_undistorted.copy()
for p in points2:
    cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)

print("expected:")
points3=[]
points3.append((155,265))
points3.append((150,443))

print(points3)

img2= img_undistorted.copy()
for p in points3:
    cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)

结果:

fisheye.undistortPoints:
[(152, 261), (147, 441)]
expected:
[(155, 265), (150, 443)]

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2013-02-28
    • 2020-11-07
    • 1970-01-01
    • 2017-11-22
    • 2017-10-12
    相关资源
    最近更新 更多