gpt4 book ai didi

c++ - OpenCV stereoRectify 扭曲图像

转载 作者:太空狗 更新时间:2023-10-29 19:57:12 27 4
gpt4 key购买 nike

我们有一台 ELP 1.0 百万像素双镜头 USB 立体相机,我们正在尝试使用 C++ 中的 OpenCV 3.1 对其进行校准。然而,校准的结果是完全不可用的,因为调用 stereoRectify 完全扭曲了图像。这就是我们所做的:

在两个相机中找到校准(棋盘)图案,棋盘大小为 5x7,无论拍摄的图像数量如何,结果几乎相同

findChessboardCorners(img[k], boardSize, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE)
cornerSubPix(img[k], corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01));

所有棋盘都被正确检测到并使用

验证
drawChessboardCorners(img[k], boardSize, corners, bFound);

然后我们分别对每个摄像头进行标定(但这一步对于立体标定似乎并不重要),但是我们可以用它来分别对每个摄像头进行验证

calibrateCamera(objectPoints, imagePoints[k], Size(320, 240), cameraMatrix[k], distCoeffs[k], rvecs, tvecs, 0)

然后我们进行立体标定

stereoCalibrate(objectPoints, imagePoints[0], imagePoints[1], cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1],
Size(320, 240), R, T, E, F, CALIB_USE_INTRINSIC_GUESS);

计算整流变换

stereoRectify(cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1], Size(320, 240), R, T, R1, R2, P1, P2, Q,
CALIB_ZERO_DISPARITY, 1, Size(320, 240), &validRoI[0], &validRoI[1]);

为重新映射初始化 map

Mat rmap[2][2];
initUndistortRectifyMap(cameraMatrix[0], distCoeffs[0], R1, P1, Size(FRAME_WIDTH, FRAME_HEIGHT), CV_16SC2, rmap[0][0], rmap[0][1]);
initUndistortRectifyMap(cameraMatrix[1], distCoeffs[1], R2, P2, Size(FRAME_WIDTH, FRAME_HEIGHT), CV_16SC2, rmap[1][0], rmap[1][1]);
...
remap(img, rimg, rmap[k][0], rmap[k][1], INTER_LINEAR);
imshow("Canvas", rimg);

结果是完全扭曲的图像。正如我在开头所说,所有校准/棋盘图案都被正确检测到,如果我们不调用 stereoRectify 函数,未失真的图像(重新映射后)看起来很完美。如果我们调用 stereoRectify 函数,问题就来了。

我们错过了什么吗?校准图像的数量似乎没有任何影响(有时拍摄 2 张​​图像比 10 张图像提供更好的结果(但仍然无法使用)

这是校准图案的例子。我们采用几种不同的方向:

enter image description here

如果我们不调用 stereoRectify,这是校准的结果: enter image description here

如果我们调用 stereoRectify,这是错误的结果(但大多数情况下它会变得更糟): enter image description here

提前感谢您提供任何可能出错的帮助。

最佳答案

只是总结一下是否有人需要类似的帮助,这就是我为获得最佳外观所做的工作:

在角点检测之前放大棋盘图像:

Mat resized;
resize(img[k], resized, Size(FRAME_WIDTH * 2, FRAME_HEIGHT * 2), 0.0, 0.0, INTER_LINEAR);
findChessboardCorners(resized, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE

缩小检测到的角点:

for (int i = 0; i < corners.size(); ++i) {
corners[i].x /= 2.0;
corners[i].y /= 2.0;
}

分别校准每个相机:

double rms = calibrateCamera(objectPoints, imagePoints[k], Size(FRAME_WIDTH, FRAME_HEIGHT), cameraMatrix[k], distCoeffs[k], rvecs, tvecs,
CALIB_FIX_PRINCIPAL_POINT | CALIB_FIX_ASPECT_RATIO | CALIB_ZERO_TANGENT_DIST | CALIB_RATIONAL_MODEL | CALIB_FIX_K3 | CALIB_FIX_K4 | CALIB_FIX_K5);

校准立体相机:

stereoCalibrate(objectPoints, imagePoints[0], imagePoints[1], cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1],
Size(FRAME_WIDTH, FRAME_HEIGHT), R, T, E, F,
CALIB_FIX_INTRINSIC | CALIB_SAME_FOCAL_LENGTH,
TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, 0));

计算校正(alpha = 0.0):

stereoRectify(cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1], Size(FRAME_WIDTH, FRAME_HEIGHT), 
R, T, R1, R2, P1, P2, Q,
CALIB_ZERO_DISPARITY, 0.0, Size(FRAME_WIDTH, FRAME_HEIGHT), &validRoI[0], &validRoI[1]);

这些是校准结果矩阵

内部函数:

M1: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 2.6187262304487734e+02, 0., 1.5950000000000000e+02, 0.,
2.6187262304487734e+02, 1.1950000000000000e+02, 0., 0., 1. ]
D1: !!opencv-matrix
rows: 1
cols: 5
dt: d
data: [ -4.6768074176991381e-01, 2.0221327568191746e-01, 0., 0., 0. ]
M2: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 2.6400975025525213e+02, 0., 1.5950000000000000e+02, 0.,
2.6400975025525213e+02, 1.1950000000000000e+02, 0., 0., 1. ]
D2: !!opencv-matrix
rows: 1
cols: 5
dt: d
data: [ -4.5713211677198845e-01, 2.8855737500717565e-01, 0., 0., 0. ]

外部因素:

R: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 9.9963073433190641e-01, 4.6310793035473068e-04,
2.7169477545556639e-02, -6.9475632716349024e-04,
9.9996348636555088e-01, 8.5172324905818230e-03,
-2.7164541091274301e-02, -8.5329635354663789e-03,
9.9959455592785362e-01 ]
T: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ -6.1830090720273198e+01, 1.6774590574449604e+00,
1.8118983433925613e+00 ]

enter image description here

我的另一个问题是是否对变量初始化有任何特殊要求,或者这是否足够?

Mat cameraMatrix[2] = { Mat::eye(3, 3, CV_64F), Mat::eye(3, 3, CV_64F) };
Mat distCoeffs[2], R, T, E, F, R1, R2, P1, P2, Q;

关于c++ - OpenCV stereoRectify 扭曲图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39852273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com