C++ OpenCV CalibleCamera()断言失败
一段时间以来,我一直在尝试使用Opencv calibrateCamera()函数校准我的相机。我遵循了opencv示例程序中描述的相同过程。我试图首先加载10个9 x 6的棋盘图像。然后找到棋盘角。如果找到角点,则角点的像素位置存储在vectorC++ OpenCV CalibleCamera()断言失败,c++,opencv,visual-c++,camera-calibration,C++,Opencv,Visual C++,Camera Calibration,一段时间以来,我一直在尝试使用Opencv calibrateCamera()函数校准我的相机。我遵循了opencv示例程序中描述的相同过程。我试图首先加载10个9 x 6的棋盘图像。然后找到棋盘角。如果找到角点,则角点的像素位置存储在vectorImagePoints中。对所有映像执行此操作后,将执行runCalibrationAndSave part。在runCalibrationAndSave中,第一个runCalibration部分在填充对象点(类型为vector)的位置执行 使用角点的
#include <iostream>
#include <sstream>
#include <time.h>
#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;
class Settings
{public:
Size boardSize;
float squareSize;
};
bool runCalibrationAndSave(Settings& s, Size imageSize, Mat& cameraMatrix, Mat& distCoeffs,
vector<vector<Point2f> > imagePoints );
int main()
{
Settings s;
s.boardSize.width =9;
s.boardSize.height=6;
s.squareSize=50;
Mat cameraMatrix, distCoeffs;
Size imageSize;
char filename[512];
vector<vector<Point2f> > imagePoints;
for(int counter=0; counter<10; counter++)
{sprintf( filename, "chessboard%d.jpg", counter );
IplImage* img = cvLoadImage(filename);
cv::Mat& m = cv::cvarrToMat(img);
Mat pointBuf = Mat::zeros(54,2,CV_32FC1);
vector<Point2f> pointBuf_vec;
bool found=false;
found = findChessboardCorners( m,s.boardSize, pointBuf,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
if(found)
{
cout<<"check"<<endl;
Mat viewGray;
cvtColor(m, viewGray, CV_BGR2GRAY);
cornerSubPix( viewGray, pointBuf, Size(11 ,11),Size(-1,-1), TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
drawChessboardCorners( m, s.boardSize, Mat(pointBuf), found );
pointBuf_vec.clear();
for(int i=0;i<54;i++)
{
Point2f temp;
temp.x=pointBuf.at<float>(i,0);
temp.y=pointBuf.at<float>(i,1);
pointBuf_vec.push_back(temp);
}
imagePoints.push_back(pointBuf_vec);
}
imshow("Example1",m);
cvWaitKey();
imageSize = m.size();
}
runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints);
return 0;
}
static void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f>& corners)
{
corners.clear();
for( int i = 0; i < boardSize.height; i++ )
for( int j = 0; j < boardSize.width; j++ )
{ corners.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));
}
}
static bool runCalibration( Settings& s, Size& imageSize, Mat& cameraMatrix, Mat& distCoeffs,
vector<vector<Point2f> > imagePoints, vector<Mat>& rvecs, vector<Mat>& tvecs,
vector<float>& reprojErrs, double& totalAvgErr)
{
cameraMatrix = Mat::eye(3, 3, CV_64F);
// if( s.flag & CV_CALIB_FIX_ASPECT_RATIO )
// cameraMatrix.at<double>(0,0) = 1.0;
distCoeffs = Mat::zeros(8, 1, CV_64F);
vector<vector<Point3f> > objectPoints;
Mat object_pointBuf = Mat::zeros(s.boardSize.width*s.boardSize.height,3,CV_32FC1);
vector<Point3f> object_pointBuf_vec;
calcBoardCornerPositions(s.boardSize, s.squareSize, object_pointBuf_vec);
for(int k=0;k<imagePoints.size();k++)
{
objectPoints.push_back(object_pointBuf_vec);
}
// objectPoints.resize(imagePoints.size(),objectPoints[0]);
//Find intrinsic and extrinsic camera parameters
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
distCoeffs, rvecs, tvecs, /*s.flag|* /CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
cout << "Re-projection error reported by calibrateCamera: "<< rms << endl;
bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs);
// totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints,
// rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs);
return ok;
}
bool runCalibrationAndSave(Settings& s, Size imageSize, Mat& cameraMatrix, Mat& distCoeffs,vector<vector<Point2f> > imagePoints )
{
vector<Mat> rvecs, tvecs;
vector<float> reprojErrs;
double totalAvgErr = 0;
bool ok = runCalibration(s,imageSize, cameraMatrix, distCoeffs, imagePoints, rvecs, tvecs,
reprojErrs, totalAvgErr);
cout << (ok ? "Calibration succeeded" : "Calibration failed")
<< ". avg re projection error = " << totalAvgErr ;
return ok;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
班级设置
{公众:
大小板大小;
浮点数;
};
bool运行校准和保存(设置和s、尺寸图像尺寸、Mat和cameraMatrix、Mat和Discoefs、,
矢量成像点);
int main()
{
设置s;
s、 boardSize.width=9;
s、 木板尺寸。高度=6;
s、 平方大小=50;
Mat cameraMatrix,distcoefs;
尺寸图像尺寸;
字符文件名[512];
向量成像点;
对于(int counter=0;counter我发现解决此问题的唯一方法是下载OpenCV源代码,然后使用CMAKE和Visual Studio 2010进行构建。现在使用CMAKE构建的库,消除了所有这些问题。还存在与“imread”和“imshow”相关的问题;这些问题在构建ope时也不会出现来自CMAKE的nCV库。
有关如何从CMAKE构建库,请查看此链接
我希望这会有所帮助。我刚刚遇到了一个类似的问题,在使用MSVC 2013的calibrateCamera中,相同的校准代码崩溃了,尽管一切看起来都正常输入、计数匹配等,但当我跳过异常时,图像确实进行了校准
在我的例子中,问题是我使用的OpenCV编译库被编译为一个共享的/DLL,我的应用程序在静态库模式下使用它,所以在MSVC中更改为多线程调试DLL vs多线程调试修复了它(/MTd vs/MDd)。或者切换到OpenCV的静态版本。从错误描述来看,它似乎与传递给校准函数的图像数有关。是的,似乎如此。但是CalibleCamera()不将图像数作为参数。它将objectPoints和imagePoints作为参数。然后,它可能会将图像数计算为int nimages=(int)objectPoints.total();正如我检查calibrate.cpp文件的第3164行。但当我尝试打印objectPoints.total()时;我知道类vector>没有成员总数()。我不确定这是否是问题所在?使用vector::size()
获取vector
中的元素数,并检查两者的大小是否一致。IplImage*img=cvLoadImage(文件名);cv::Mat&m=cv::cvarrToMat(img);
//为什么不使用imread()
也可以共享您的图像。。。