[英]Stereo rectification problem with OpenCV and Python
import cv2
import glob
import argparse
import math
from numpy import genfromtxt
import matplotlib.pyplot as plt
import numpy as np
import os.path
from scipy import ndimage
import os
left = cv2.imread('D:/input image 1.jpg', cv2.IMREAD_UNCHANGED)
right = cv2.imread('D:/input image 2.jpg', cv2.IMREAD_UNCHANGED)
#left = (left/256).astype('uint8')
#right = (right/256).astype('uint8')
cameraMatrix1 = np.array([[1485.8503101355045, 0, 641.0072474534551], [0, 1486.8249802291273, 454.1981417235667], [0, 0, 1]])
cameraMatrix2 = np.array([[1472.34425902698, 0, 656.7358738783742], [0, 1473.184475795988, 441.016803589085], [0, 0, 1]])
distCoeffs1 = np.array([-0.09236217303671054, 0.15801009565677457, 0.0020679941868083445, -0.0023435708660260184, 0.04491629603683055])
distCoeffs2 = np.array([-0.09949068652688753, 0.22953391558591676, 0.0016749995113326907, -0.0015940937703328348, -0.13603886268508916])
rotationMatrix = np.array([[0.9999169807005986, 0.0026862926847088424, -0.012602203704541104],[-0.002633967055223802, 0.9999878496600472, 0.0041668633079119935],[0.012613243997904163, -0.004133323588458492, 0.9999119069757908]])
transVector = np.array([29.96389633009774, 0.5883268401189343, -5.0370190999346365])
essentialMatrix = np.array([[-0.005846632380824811, 5.0345261532342365, 0.6092635826971343], [-5.4145428656773165, 0.11031957194242471, -29.897779179091888], [-0.6672019134164675, 29.96195184048419, 0.1322696748639909]])
fundMatrix = np.array([[4.567507458136527e-08, -3.930495370357416e-05, 0.010750771532659317], [4.227537878312907e-05, -8.607826196991683e-07, 0.3201405456504413], [-0.010999824926761303, -0.3182113833954986, 1]])
flags = cv2.CALIB_ZERO_DISPARITY
image_size = left.shape[::-1]
R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, image_size, rotationMatrix, transVector, flags = flags)
leftmapX, leftmapY = cv2.initUndistortRectifyMap(cameraMatrix1, distCoeffs1, R1, P1, image_size, cv2.CV_32FC1)
rightmapX, rightmapY = cv2.initUndistortRectifyMap(cameraMatrix2, distCoeffs2, R2, P2, image_size, cv2.CV_32FC1)
left_remap = cv2.remap(left, leftmapX, leftmapY, cv2.INTER_LANCZOS4)
right_remap = cv2.remap(right, leftmapX, rightmapY, cv2.INTER_LANCZOS4)
# For some reason, the images get rotated upside down after remapping, and I have to invert them back
left_remap = ndimage.rotate(left_remap,180)
right_remap = ndimage.rotate(right_remap,180)
for line in range(0, int(right_remap.shape[0] / 20)):
left_remap[line * 20, :] = 0
right_remap[line * 20, :] = 0
cv2.namedWindow('output images', cv2.WINDOW_NORMAL)
cv2.imshow('output images', np.hstack([left_remap, right_remap]))
cv2.waitKey(0)
cv2.destroyAllWindows()
我正在对水平和垂直方向上的 5 个不同镜头进行校正(即,我需要所有 5 个镜头的图像中的所有公共点完全位于相同的 position 中)。 当我用两个水平放置比垂直放置更远的镜头测试立体声校正时,cv2.stereoRectify 总是将它们解释为好像它们是垂直分开的。 我希望能够告诉 function 水平解释它。 我在这里看到了一些类似的问题,但在任何地方都找不到有用的回复。
编辑:为了便于复制,我在代码中添加了所有失真系数和校准矩阵。 这些值是通过使用大量校准图像和 function cv2.stereoCalibrate 获得的,但无法上传所有这些图像和系数提取代码。
在image_size
中修改这个
image_size = left.shape[::-1][1:3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.