I am trying to get camera calibration parameters by using opencv camera calibration functions. I have a video and trying to find the calibration parameters and find the points in the video which inclused a checkboard in different psoitions. But i couldnt passed the calibration phase yet. I can find the corner of the checkboard and show them in openCV window but when it comes to line:

calibrateCamera(objectPoints,imagePoints..............) 

it throws exception and stops.

I get the following error: OpenCV error: Assertion failed 0 &&nimages==int imagePoints1.total ()&&

Here is my code:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "C:/opencv/include/opencv/cv.h"

#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;

std::vector<cv::Point2f> imageCorners;
std::vector<cv::Point3f> objectCorners;
std::vector<std::vector<cv::Point3f>> objectPoints;
std::vector<std::vector<cv::Point2f>> imagePoints;

void addPoints(const std::vector<cv::Point2f>&imageCorners, const std::vector<cv::Point3f>& objectCorners)
{
// 2D image points from one view
imagePoints.push_back(imageCorners);
// corresponding 3D scene points
objectPoints.push_back(objectCorners);
}
int main()
{

int key;
cv::Mat   image;
cv::Mat   gray_image;

VideoCapture cap("here goes path of the file"); 
   if (!cap.isOpened())  // check if we succeeded
       cout<<"failed";
else
   cout<<"success";

    cvNamedWindow( "video",0);

cv::Size boardSize(8,6);
// output Matrices
cv::Mat cameraMatrix;
std::vector<cv::Mat> rvecs, tvecs;
cv::Mat distCoeffs;

for (int i=0; i<boardSize.height; i++) 
{
    for (int j=0; j<boardSize.width; j++) 
    {
        objectCorners.push_back(cv::Point3f(i, j, 0.0f));
    }
}
int frame=1;
int corner_count=0;
while(1) 
{
    if(cap.read(image))
    {
        frame++;
        if(frame%20==0)
        {
            if(waitKey(30) >= 0) break;

            bool found = cv::findChessboardCorners(image, boardSize, imageCorners);

            cvtColor( image, gray_image, CV_RGB2GRAY );


                addPoints(imageCorners, objectCorners);

            //bool found = cv::findChessboardCorners(image,boardSize, imageCorners);
            cv::drawChessboardCorners(gray_image,boardSize, imageCorners,found);
            imshow( "video",  gray_image );
        }
    }
    else
        break;

}
int flag=0;
std::string text="";

for (int i=1; i<imagePoints.size();i++)
{
    std::stringstream out;
    out << imagePoints[i];
    text=out.str();
    cout<<text<<endl;

}

calibrateCamera(objectPoints,imagePoints,gray_image.size(), cameraMatrix, distCoeffs, rvecs, tvecs, flag);

return 0;

}
有帮助吗?

解决方案

Print the size of all your std::vector, I suspect you are passing an empty vector to that function.

EDIT:

I've shared some instructions in this answer on how to do camera calibration. Those references include working source code. You'll probably have to do a small adaptation on those programs so they work with video instead.

其他提示

you should look at this: http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#bool findChessboardCorners(InputArray image, Size patternSize, OutputArray corners, int flags)

it says your Source chessboard view must be an 8-bit grayscale or color image. so you must use this: bool found = cv::findChessboardCorners(gray_image, boardSize, imageCorners);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top