[英]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.