gpt4 book ai didi

c - OpenCV Error -211 指定的宽高比不正确,cvStereoCalibration (C)

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:00 25 4
gpt4 key购买 nike

我正在尝试编写一个使用存储的图像对来校准立体相机的函数。代码是用 C 语言编写的。我收到错误消息

OpenCV Error: One of arguments' values is out of range (The specified aspect ratio (= cameraMatrix[0][0] / cameraMatrix[1][1]) is incorrect) in cvCalibrateCamera2, file /home/daniel/opencv/opencv-2.4.13/modules/calib3d/src/calibration.cpp, line 1602 terminate called after throwing an instance of 'cv::Exception' what(): /home/daniel/opencv/opencv-2.4.13/modules/calib3d/src/calibration.cpp:1602: error: (-211) The specified aspect ratio (= cameraMatrix[0][0] / cameraMatrix[1][1]) is incorrect in function cvCalibrateCamera2

这是我对 cvStereoCalibrate 的调用引发的,表明我的一个参数未正确初始化/填充。我似乎无法弄清楚我做错了什么。

我使用的宏扩展如下:

BOARD_H_INNER 6

BOARD_W_INNER 9

NUM_SAVED_IMAGES 17

代码:

 int calibrate_camera_from_saved_images() {
IplImage *leftFrame, *rightFrame;
char imageName[20];

//create objectPoints equal to N-by-3, N being the number of squares on the chess board
//in use (BOARD_W * BOARD_H) times the number of images provided
CvMat *objectPoints = cvCreateMat(
BOARD_W_INNER * BOARD_H_INNER * NUM_SAVED_IMAGES, 3,
CV_32FC1);

//initialize objectPoints for cvStereoCalibrate
int count = 0;
int i, j, k;

for (i = 0; i < NUM_SAVED_IMAGES; i++) {
for (j = 0; j < BOARD_W_INNER; j++) {
for (k = 0; k < BOARD_H_INNER; k++) {
//alternatively use (i*BOARD_W)+(j*BOARD_H)+k instead of count -> efficiency ?

CV_MAT_ELEM(*objectPoints, float, (i * BOARD_W_INNER) + (j * BOARD_H_INNER) + k, 0 ) =
SQUARE_SIZE * k;
CV_MAT_ELEM(*objectPoints, float, (i * BOARD_W_INNER) + (j * BOARD_H_INNER) + k, 1 ) =
SQUARE_SIZE * j;
CV_MAT_ELEM(*objectPoints, float, (i * BOARD_W_INNER) + (j * BOARD_H_INNER) + k, 2 ) = 0;
}
}
}

CvPoint2D32f *corners1 = new CvPoint2D32f[ BOARD_W_INNER * BOARD_H_INNER];
CvPoint2D32f *corners2 = new CvPoint2D32f[ BOARD_W_INNER * BOARD_H_INNER];

CvMat *imagePoints1 = cvCreateMat(
BOARD_W_INNER * BOARD_H_INNER * NUM_SAVED_IMAGES, 2,
CV_32FC1);
CvMat *imagePoints2 = cvCreateMat(
BOARD_W_INNER * BOARD_H_INNER * NUM_SAVED_IMAGES, 2,
CV_32FC1);

CvMat *nPoints = cvCreateMat(NUM_SAVED_IMAGES, 1, CV_32SC1);

//load images and find their corners, corners are then stored
for (i = 0; i < NUM_SAVED_IMAGES; i++) {
sprintf(imageName, "imageL%02u.png", i);
leftFrame = cvLoadImage(imageName);
find_chess_board(leftFrame, BOARD_W_INNER, BOARD_H_INNER, corners1);
sprintf(imageName, "imageR%02u.png", i);
rightFrame = cvLoadImage(imageName);
find_chess_board(rightFrame, BOARD_W_INNER, BOARD_H_INNER, corners2);

for (j = 0; j < (BOARD_W_INNER * BOARD_H_INNER); j++, count++) {
CV_MAT_ELEM( *imagePoints1, float, count, 0 ) = corners1[j].x;
CV_MAT_ELEM( *imagePoints1, float, count, 1 ) = corners1[j].y;
CV_MAT_ELEM( *imagePoints2, float, count, 0 ) = corners2[j].x;
CV_MAT_ELEM( *imagePoints2, float, count, 1 ) = corners2[j].y;
}

CV_MAT_ELEM(*nPoints, int, i, 0) = (BOARD_W_INNER * BOARD_H_INNER);
}

static CvMat *cameraMatrix1 = cvCreateMat(3, 3, CV_64F);
static CvMat *distCoeffs1 = cvCreateMat(5, 1, CV_32FC1);

static CvMat *cameraMatrix2 = cvCreateMat(3, 3, CV_64F);
static CvMat *distCoeffs2 = cvCreateMat(5, 1, CV_32FC1);

static CvSize imageSize = cvGetSize(leftFrame);

static CvMat* R = cvCreateMat(3,3,CV_64FC1);
static CvMat* T = cvCreateMat(3,1,CV_64FC1);
static CvMat* E = cvCreateMat(3,3,CV_64FC1);
static CvMat* F = cvCreateMat(3,3,CV_64FC1);


printf("Attempting Calibration");
cvStereoCalibrate(objectPoints, imagePoints1, imagePoints2, nPoints,
cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, imageSize,
R, T, E, F,
cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 100, 1e-5), CV_CALIB_ZERO_TANGENT_DIST
+CV_CALIB_SAME_FOCAL_LENGTH);

cvSave("cameraMatrix1.xml", cameraMatrix1);
cvSave("distCoeffs1.xml", distCoeffs1);
cvSave("cameraMatrix2.xml", cameraMatrix2);
cvSave("distCoeffs2.xml", distCoeffs2);

return 0;
}

编辑:经过进一步测试,我发现使用 CV_CALIB_FIX_ASPECT_RATIO 是导致错误的原因。其他标志不会导致此问题。然而,矩阵的输出看起来并没有起作用。

cameraMatrix1.xml:

    <?xml version="1.0"?>
<opencv_storage>
<cameraMatrix1 type_id="opencv-matrix">
<rows>3</rows>
<cols>3</cols>
<dt>d</dt>
<data>
.Nan 0. .Nan 0. .Nan .Nan 0. 0. 1.</data></cameraMatrix1>
</opencv_storage>

最佳答案

在尝试了很多不同的东西并将版本从 2.4.13 切换到 2.4.7 并返回后,我现在有了能够拍摄图像、校准相机并将校准值保存到 .xml 文件中的代码。

下面的代码是拍摄我能找到所有角落的图像:

cvNamedWindow("CameraTestL", CV_WINDOW_AUTOSIZE);
cvNamedWindow("CameraTestR", CV_WINDOW_AUTOSIZE);
CvCapture *captureL, *captureR;
IplImage *frameL, *frameR;

char imageNameL[20];
strcpy(imageNameL, "imagel00.png");

char imageNameR[20];
strcpy(imageNameR, "imageR00.png");

int i = 0;

captureL = cvCreateCameraCapture(1);
captureR = cvCreateCameraCapture(0);

//resize capture from standard 640/480 to 320/240 due to USB bandwith constraints
int res = cvSetCaptureProperty(captureL, CV_CAP_PROP_FRAME_WIDTH,
FRAME_WIDTH);
res = cvSetCaptureProperty(captureL, CV_CAP_PROP_FRAME_HEIGHT,
FRAME_HEIGHT);

res = cvSetCaptureProperty(captureR, CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
res = cvSetCaptureProperty(captureR, CV_CAP_PROP_FRAME_HEIGHT,
FRAME_HEIGHT);

assert(captureL != NULL);
assert(captureR != NULL);

int cornerCount;

CvSize boardSize = cvSize(BOARD_W, BOARD_H);

while (1) {
frameL = cvQueryFrame(captureL);
frameR = cvQueryFrame(captureR);
if (!frameL)
break;
if (!frameR)
break;

CvPoint2D32f* corners = new CvPoint2D32f[ BOARD_W * BOARD_H];
// IplImage *helperL, *helperR;
IplImage *grayImage = cvCreateImage(cvGetSize(frameL), 8, 1);

// cvCopyImage(frameL, helperL);
// cvCopyImage(frameR, helperR);

int found = cvFindChessboardCorners(frameL, boardSize, corners,
&cornerCount,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

cvCvtColor(frameL, grayImage, CV_BGR2GRAY);
cvFindCornerSubPix(grayImage, corners, cornerCount, cvSize(11,11), cvSize(-1,-1), cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1));

cvDrawChessboardCorners(frameL, boardSize, corners, cornerCount, found);

found = cvFindChessboardCorners(frameR, boardSize, corners, &cornerCount, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
cvCvtColor(frameR, grayImage, CV_BGR2GRAY);
cvFindCornerSubPix(grayImage, corners, cornerCount, cvSize(11,11), cvSize(-1,-1), cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1));

cvDrawChessboardCorners(frameR, boardSize, corners, cornerCount, found);

cvShowImage("CameraTestL", frameL);
cvShowImage("CameraTestR", frameR);
//cvWaitKey returns value of key pressed
char c = cvWaitKey(33);
//press ESC key to break (ESC == 27)
if (c == 27)
break;
if (c == 's') {

frameL = cvQueryFrame(captureL);
frameR = cvQueryFrame(captureR);

sprintf(imageNameL, "imageL0%02u.png", i);
sprintf(imageNameR, "imageR0%02u.png", i);

cvSaveImage(imageNameL, frameL, 0);
cvSaveImage(imageNameR, frameR, 0);

printf("Saved image #%d\n", i);
fflush(stdout);
i++;
}
}

cvReleaseCapture(&captureL);
cvReleaseCapture(&captureR);
cvDestroyWindow("CameraTestL");
cvDestroyWindow("CameraTestR");

return 0;

下面的代码读取图像并计算相机矩阵和其他参数

int calibrate_camera_from_saved_images() {
IplImage *leftFrame, *rightFrame;
char imageName[20];

//create objectPoints equal to N-by-3, N being the number of squares on the chess board
//in use (BOARD_W * BOARD_H) times the number of images provided
CvMat *objectPoints = cvCreateMat(
BOARD_W_INNER * BOARD_H_INNER * NUM_SAVED_IMAGES, 3,
CV_32FC1);

//initialize objectPoints for cvStereoCalibrate
int count = 0;
int i, j, k;

for (i = 0; i < NUM_SAVED_IMAGES; i++) {
for (j = 0; j < BOARD_W_INNER; j++) {
for (k = 0; k < BOARD_H_INNER; k++, count++) {
//alternatively use (i*BOARD_W)+(j*BOARD_H)+k instead of count -> efficiency ?

CV_MAT_ELEM(*objectPoints, float, count, 0 ) =
SQUARE_SIZE * k;
CV_MAT_ELEM(*objectPoints, float, count, 1 ) =
SQUARE_SIZE * j;
CV_MAT_ELEM(*objectPoints, float, count, 2 ) = 0;
}
}
}

CvPoint2D32f *corners1 = new CvPoint2D32f[ BOARD_W_INNER * BOARD_H_INNER];
CvPoint2D32f *corners2 = new CvPoint2D32f[ BOARD_W_INNER * BOARD_H_INNER];

CvMat *imagePoints1 = cvCreateMat(
BOARD_W_INNER * BOARD_H_INNER * NUM_SAVED_IMAGES, 2,
CV_32FC1);
CvMat *imagePoints2 = cvCreateMat(
BOARD_W_INNER * BOARD_H_INNER * NUM_SAVED_IMAGES, 2,
CV_32FC1);

CvMat *nPoints = cvCreateMat(NUM_SAVED_IMAGES, 1, CV_32SC1);
count = 0;
//load images and find their corners, corners are then stored
for (i = 0; i < NUM_SAVED_IMAGES; i++) {
sprintf(imageName, "imageL%002u.png", i);
leftFrame = cvLoadImage(imageName);
find_chess_board(leftFrame, BOARD_W_INNER, BOARD_H_INNER, corners1);
sprintf(imageName, "imageR%002u.png", i);
rightFrame = cvLoadImage(imageName);
find_chess_board(rightFrame, BOARD_W_INNER, BOARD_H_INNER, corners2);

for (j = 0; j < (BOARD_W_INNER * BOARD_H_INNER); j++, count++) {
CV_MAT_ELEM( *imagePoints1, float, count, 0 ) = corners1[j].x;
CV_MAT_ELEM( *imagePoints1, float, count, 1 ) = corners1[j].y;
CV_MAT_ELEM( *imagePoints2, float, count, 0 ) = corners2[j].x;
CV_MAT_ELEM( *imagePoints2, float, count, 1 ) = corners2[j].y;
}

CV_MAT_ELEM(*nPoints, int, i, 0) = (BOARD_W_INNER * BOARD_H_INNER);
}

static CvMat *cameraMatrix1 = cvCreateMat(3, 3, CV_64F);
static CvMat *distCoeffs1 = cvCreateMat(5, 1, CV_32FC1);

static CvMat *cameraMatrix2 = cvCreateMat(3, 3, CV_64F);
static CvMat *distCoeffs2 = cvCreateMat(5, 1, CV_32FC1);

for (i = 0; i < 3; i++) {
CV_MAT_ELEM( *cameraMatrix1, float, i, 0) = 1;
CV_MAT_ELEM( *cameraMatrix1, float, i, 1) = 1;
CV_MAT_ELEM( *cameraMatrix1, float, i, 2) = 1;
CV_MAT_ELEM( *cameraMatrix2, float, i, 0) = 1;
CV_MAT_ELEM( *cameraMatrix2, float, i, 1) = 1;
CV_MAT_ELEM( *cameraMatrix2, float, i, 2) = 1;
}

static CvSize imageSize = cvGetSize(leftFrame);
static CvMat* R = cvCreateMat(3, 3, CV_64FC1);
static CvMat* T = cvCreateMat(3, 1, CV_64FC1);
static CvMat* E = cvCreateMat(3, 3, CV_64FC1);
static CvMat* F = cvCreateMat(3, 3, CV_64FC1);

printf("Attempting Calibration\n");
fflush(stdout);
cvStereoCalibrate(objectPoints, imagePoints1, imagePoints2, nPoints,
cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, imageSize,
R, T, E, F,
cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 100, 1e-5),
CV_CALIB_SAME_FOCAL_LENGTH);

cvSave("cameraMatrix1.xml", cameraMatrix1);
cvSave("distCoeffs1.xml", distCoeffs1);
cvSave("cameraMatrix2.xml", cameraMatrix2);
cvSave("distCoeffs2.xml", distCoeffs2);
cvSave("RotationMatrix.xml", R);
cvSave("TransformationVector.xml", T);

return 0;

CV_CALIB_FIX_ASPECT_RATIO 仍然不起作用。

关于c - OpenCV Error -211 指定的宽高比不正确,cvStereoCalibration (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742685/

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