我正在尝试让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);
}
}
答案 0 :(得分:18)
通过反复试验,我意识到patternize应该是7x7,因为它计算内部角落。这个参数必须准确 - 8x8不起作用,但也不会少于7x7。
答案 1 :(得分:10)
而不是使用
Size patternsize(8,8);
使用
Size patternsize(7,7);
答案 2 :(得分:4)
棋盘的宽度和高度不能是相同的长度,即它需要是不对称的。这可能是您问题的根源。 Here是一个关于使用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);
}