本文介绍了Python 2.7/OpenCV 3.3:cv2.initUndistortRectifyMap中的错误.没有显示未经校正的校正图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扭曲和校正我的立体声图像.为此,我在Python 2.7中使用了Opencv 3.3.我使用的代码是:

I want to distort and rectified my stereo images. For which I used Opencv 3.3 in Python 2.7. Code I used is :

import cv2
import numpy as np

cameraMatrixL = np.load('mtx_left.npy')
distCoeffsL = np.load('dist_left.npy')
cameraMatrixR = np.load('mtx_right.npy')
distCoeffsR = np.load('dist_right.npy')
R = np.load('R.npy')
T = np.load('T.npy')

imgleft = cv2.imread('D:\python\camera calibration and 3d const\left\left60.png',0)
imgright = cv2.imread('D:\python\camera calibration and 3d const\Right/right60.png',0)


R1,R2,P1,P2,Q,validPixROI1, validPixROI2 = cv2.stereoRectify(cameraMatrixL,distCoeffsL,cameraMatrixR,distCoeffsR,(640,480),R,T,alpha=1)

print Q
# distort images
undistort_map1, rectify_map1 = cv2.initUndistortRectifyMap(cameraMatrixL, distCoeffsR, R1, P1, (640,480), cv2.CV_16SC2)
undistort_map2, rectify_map2 = cv2.initUndistortRectifyMap(cameraMatrixR, distCoeffsR, R2, P2, (640,480), cv2.CV_16SC2)

undistor_output1 = cv2.remap(imgleft, undistort_map1, rectify_map1, cv2.INTER_LINEAR)
undistor_output2 = cv2.remap(imgright, undistort_map2, rectify_map2, cv2.INTER_LINEAR)

cv2.imshow('undistor_output1',undistor_output1)
cv2.imshow('undistor_output2',undistor_output2)
while (True):
 if cv2.waitKey(1) & 0xFF == ord('q'):
    break

我分别校准了我的相机,然后将那些获得的矩阵用于cv2.stereoRectify中,以获得我在cv2.initUndistortRectifyMap中使用的R1,R2,P1,P2,Q矩阵.但是我没有正确地获得未失真的图像.看起来像这样:我的矩阵是:

I calibrated my camera separately and then used those obtained matrices into cv2.stereoRectify to get R1,R2,P1,P2,Q matrices which I used in cv2.initUndistortRectifyMap. But I am not getting undistort images correctly.It looks like this:My Matrices are :

Q
[[  1.00000000e+00   0.00000000e+00   0.00000000e+00  -3.23097469e+02]
 [  0.00000000e+00   1.00000000e+00   0.00000000e+00  -2.40008609e+02]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00  -7.47885268e+00]
 [  0.00000000e+00   0.00000000e+00  -1.53249612e-02   0.00000000e+00]]



   cameraMartix Left
[[ 807.24668269    0.          326.78961645]
 [   0.          620.70299534  259.9187458 ]
 [   0.            0.            1.        ]]

camearMatrix Right
[[ 567.37537971    0.          278.76995505]
 [   0.          558.21417195  216.22972643]
 [   0.            0.            1.        ]]

Rotation
[[ 0.99973813 -0.02260904  0.00353613]
 [ 0.02269951  0.99934817 -0.02807079]
 [-0.00289917  0.0281437   0.99959968]]

Tranlation
[[-93.46968934]
 [ -1.48741179]
 [ 24.98692133]]

我读了很多答案,但没有什么能解决我的问题.

I have read many answers ut nothing solve my problem.

关于立体声的直截了当的解决方案校准和校正OpenCV?

* 在Python中使用OpenCV(cv2)的不正确点错误的结果

cv2.remap不会创建任何内容

使用initUndistortRectifyMap对图像点进行未失真

opencv cv2.remap创建奇怪的图像 *

该问题的解决方案是什么?

What is the solution for this problem?

谢谢.

推荐答案

尝试了许多事情之后,我找到了解决此特定问题的方法.比较使用Python OpenCV和Matlab获得的相机旋转矩阵和失真矩阵后,我发现使用 Python和Matlab 获得的矩阵之间存在很大差异.因此,我再次校准相机,并使用Python和OpenCV进行立体声校准.下图显示了旧矩阵(从Python获取)与新矩阵(在再次使用Python进行校准后获取)之间的区别.

After trying out many things I found out a solution to this particular problem. After comparing camera rotation matrices and distortion matrices obtained using Python OpenCV and Matlab, I found out that there is much difference between matrices obtained using Python and Matlab. Therefore, I again calibrate my cameras and do stereo calibration using Python and OpenCV. Below figure shows the difference between old matrices (got from Python) and new matrices (got after calibrate again using Python).

在此图中,我们可以看到,旧案例的左右摄像机的失真系数矩阵(dist coeff. right old and left old)与新案例(dist coeff. right new and left new)的左右摄像机的失真系数矩阵相比有很大差异.右相机,旧系数分别为83.85和-19.18,分别为0.78和-0.61;类似地,左相机,旧系数分别为123.7和-1641.4,分别为-0.38和0.73;此外,旧相机的第一个元素(1,1)矩阵是807.24,与右摄像机旧矩阵相比有很大不同.新左摄像机矩阵的元素值为552.41,我认为是正确的.当我使用所有这些新矩阵并将我的alpha值更改为0 (alpha = 0)时,以下结果.

In this figure, we can see that there is much difference between distortion coefficient matrix for right and left camera for old case (dist coeff. right old and left old) as compare to distortion coefficient matrix for right and left camera for new case (dist coeff. right new and left new). For right camera, old coefficients were 83.85 and -19.18 against 0.78 and -0.61 respectively. Similarly, for left camera, old coefficients were 123.7 and -1641.4 against -0.38 and 0.73 respectively. Also, first element (1,1) of old left camera matrix was 807.24 which differ much as compare to right camera old matrix. New left camera matrix has this element value as 552.41 which I think is correct. When I used all these new matrices and change my alpha value to 0 (alpha = 0) I got the following result.

对于stereoCalbirate,我使用了以下输入

For stereoCalbirate I used following inputs

retVal, cm1, dc1, cm2, dc2, r, t, e, f = cv2.stereoCalibrate(objpoints, imgpointsL, imgpointsR, cm1, dc1, cm2, dc2, (640, 480), None, None, cv2.CALIB_FIX_INTRINSIC, criteria)

其中criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)

这篇关于Python 2.7/OpenCV 3.3:cv2.initUndistortRectifyMap中的错误.没有显示未经校正的校正图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 23:26