16

我试图让 OpenCV 2.4.5 从我的网络摄像头识别棋盘图案。我无法让它工作,所以我决定尝试使用“完美”图像来让它工作:

带有白色边框的棋盘

但它仍然不起作用——patternFound 每次都返回 false。有谁知道我做错了什么?

#include <stdio.h>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
    Size patternsize(8,8); //number of centers
    Mat frame = imread("perfect.png"); //source image
    vector<Point2f> centers; //this will be filled by the detected centers

    bool patternfound = findChessboardCorners(frame,patternsize,centers);

    cout<<patternfound<<endl;
    drawChessboardCorners(frame, patternsize, Mat(centers), patternfound);

    cvNamedWindow("window");
    while(1){
        imshow("window",frame);
        cvWaitKey(33);
    }
}
4

3 回答 3

27

通过反复试验,我意识到图案大小应该是 7x7,因为它计算的是内角。这个参数必须准确——8x8 不起作用,但任何小于 7x7 的都不会。

于 2013-07-16T00:14:29.310 回答
13

而不是使用

Size patternsize(8,8); 

利用

Size patternsize(7,7);  
于 2015-03-04T22:14:58.340 回答
4

棋盘的宽高不能等长,即需要不对称。这可能是您问题的根源。 是一个关于使用 OpenCV 进行相机校准的非常好的教程。

下面是我用于校准的代码(经过测试且功能齐全,但是我在自己的某个处理线程中调用它,您应该在处理循环或用于捕获帧的任何内容中调用它):

void MyCalibration::execute(IplImage* in, bool debug)
{
    const int CHESSBOARD_WIDTH = 8;
    const int CHESSBOARD_HEIGHT = 5;
    const int CHESSBOARD_INTERSECTION_COUNT = CHESSBOARD_WIDTH * CHESSBOARD_HEIGHT;

    //const bool DO_CALIBRATION = ((BoolProperty*)getProperty("DoCalibration"))->getValue();
    if(in->nChannels == 1)
        cvCopy(in,gray_image);
    else
        cvCvtColor(in,gray_image,CV_BGR2GRAY);

    int corner_count;
    CvPoint2D32f* corners = new CvPoint2D32f[CHESSBOARD_INTERSECTION_COUNT];
    int wasChessboardFound = cvFindChessboardCorners(gray_image, cvSize(CHESSBOARD_WIDTH, CHESSBOARD_HEIGHT), corners, &corner_count);

    if(wasChessboardFound) {
        // Refine the found corners
        cvFindCornerSubPix(gray_image, corners, corner_count, cvSize(5, 5), cvSize(-1, -1), cvTermCriteria(CV_TERMCRIT_ITER, 100, 0.1));

        // Add the corners to the array of calibration points
        calibrationPoints.push_back(corners);

        cvDrawChessboardCorners(in, cvSize(CHESSBOARD_WIDTH, CHESSBOARD_HEIGHT), corners, corner_count, wasChessboardFound);
    } 
}

以防万一您想知道班级成员,这是我的班级(在我写它的时候,IplImage 还在):

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>

class MyCalibration
{
private:
    std::vector<CvPoint2D32f*> calibrationPoints;

    IplImage *gray_image;

public:
    MyCalibration(IplImage* in);
    void execute(IplImage* in, bool debug=false);
    ~MyCalibration(void);
};

最后是构造函数:

MyCalibration::MyCalibration(IplImage* in)
{
    gray_image = cvCreateImage(cvSize(in->width,in->height),8,1);
}
于 2013-07-18T02:05:05.277 回答