OpenCV问题与findChessboardCorners [英] OpenCV Issue with findChessboardCorners

查看:611
本文介绍了OpenCV问题与findChessboardCorners的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也在OpenCV论坛上问过这个问题,我在其他地方试着我的运气。我在Visual Studio Professional 2013中使用OpenCV 3.0。

I also asked this on the OpenCV forum, am trying my luck elsewhere. I'm using OpenCV 3.0 in Visual Studio Professional 2013.

所以我试图校准一个摄像机使用教程代码calib3d和教程。我一直得到相同的错误(std :: length_error在内存位置),我已经跟踪到我尝试,并添加从findChessboardCorners给我的代码的最后一行的image_points向量的角向量。 p>

So I'm trying to calibrate a camera using the tutorial code in calib3d and this tutorial. I keep getting the same error over and over (std::length_error at memory location) and I've traced it to where I try and add the corner vector given from findChessboardCorners to the image_points vector in the last line of my code.

image_points.push_back(corners);

在调试窗口中,角的大小列为:corners {size = 2305843009213050645}显然太大了(我使用的校准图像中只有35个角)。

In the debug window, the size of corners is listed as: corners { size=2305843009213050645 }, which is obviously way too big (there are only 35 corners in the calibration image I'm using).

下面的教程代码如下,虽然我已经隔离了问题找到ChessboardCorners给一个看似无意义的角矢量。奇怪的部分是,在我使用的校准图像上绘制角落没有问题 - 它看起来好像角校准完美。那么这里的问题是什么?我真的不知道为什么findChessboardCorners会给我这样一个大的角向量,我甚至不能添加到一个向量列表。

Stripped down tutorial code is below, though again I've isolated the problem to findChessboardCorners giving a seemingly nonsensical corner vector. The strange part is that there's no problem drawing the corners on the calibration image I'm using-- it appears as though the corners were calibrated perfectly. So what is the problem here? I really don't know why findChessboardCorners would be giving me such a large corner vector that I can't even add it to a list of vectors.

using namespace cv;
using namespace std;

int main(int argc, char** argv){

int numBoards = 1;
int numCornersHor=7;
int numCornersVer=5;

int numSquares = numCornersHor * numCornersVer;
Size board_sz = Size(numCornersHor, numCornersVer);

vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;   

vector<Point2f> corners;

int successes = 0;

Mat large_image;
Mat image;
Mat gray_image;
large_image = imread(argv[1], IMREAD_COLOR);
resize(large_image, image, Size(), .5, .5);

vector<Point3f> obj;
for (int j = 0; j<numSquares; j++)
    obj.push_back(Point3f((j / numCornersHor)*29, (j%numCornersHor)*29, 0.0f));

if (image.empty())
        return(0);
else if (image.channels()>1)
    cvtColor(image, gray_image, CV_BGR2GRAY);
else gray_image = image;

bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);


if (found)
{
    cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));

    drawChessboardCorners(gray_image, board_sz, corners, found);

}

imshow("win1", image);      
imshow("win2", gray_image);

int key = waitKey(1);
if (key == 27)
    return 0;

image_points.push_back(corners);
} 


推荐答案

问题是findChessboardCorners中的一个错误 - 角落在函数本身内不正确地调整大小,导致它爆炸。找到问题并从此处获得解决方案,但我必须将角点转换回向量一次我运行了更正的函数。

Figured it out. Problem was a bug in findChessboardCorners-- corners was being improperly resized within the function itself, causing it to blow up. Found the issue and got the fix from here, although I had to convert corners back to a vector once I ran the corrected function.

更新代码:

Mat pointBuf;

found = actually_findChessboardCorners(image, board_sz, pointBuf,
            CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE); 

corners.assign((Point2f*)pointBuf.datastart, (Point2f*)pointBuf.dataend);

使用此功能:

bool actually_findChessboardCorners(Mat& frame, Size& size, Mat& corners, int flags) {
int count = size.area() * 2;
corners.create(count, 1, CV_32FC2);
CvMat _image = frame;
bool ok = cvFindChessboardCorners(&_image, size,
    reinterpret_cast<CvPoint2D32f*>(corners.data),
    &count, flags) > 0;
return ok;

}

这篇关于OpenCV问题与findChessboardCorners的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆