使用opencv进行相机标定(断言失败故障)

发布于 2024-12-19 14:27:18 字数 2544 浏览 3 评论 0原文

我正在尝试使用 opencv 相机校准函数来获取相机校准参数。我有一个视频,试图找到校准参数并找到视频中的点,其中包括不同位置的棋盘。但我还无法通过校准阶段。我可以找到棋盘的一角并在 openCV 窗口中显示它们,但是当涉及到线条时:

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

它会抛出异常并停止。

我收到以下错误: OpenCV 错误:断言失败 0 &&nimages==int imagePoints1.total ()&

这是我的代码:

#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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-12-26 14:27:18

打印所有 std::vector 的大小,我怀疑您正在向该函数传递一个空向量。

编辑:

我在这个答案中分享了一些关于如何进行相机校准的说明。这些参考资料包括工作源代码。您可能需要对这些程序进行一些小调整,以便它们可以使用视频。

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.

梦与时光遇 2024-12-26 14:27:18

你应该看看这个:
http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#bool findChessboardCorners(InputArray image, Size patSize, OutputArraycorners, int flags)

它说你的源棋盘视图必须是 8 位灰度或彩色图像。
所以你必须使用这个:
bool find = cv::findChessboardCorners(gray_image, boardSize, imageCorners);

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文