【问题标题】:camera calibration opencv相机校准opencv
【发布时间】:2013-01-28 07:09:51
【问题描述】:

您好,我正在做一个项目来进行图像 3D 重建。我是校准相机的阶段,这需要很长时间才能完成。但是当我编译代码并在相机前显示棋盘格时,它会直接进入未处理的异常错误。

当图片不在框架中时,一进入框架就没有错误,出现未处理的错误,我不知道为什么。

我问了很多人,似乎没有人能帮上忙。

这是我的代码

#include <cv.h>
#include <highgui.h>
#include <vector>
#include <stdlib.h>
#include <stdio.h>

using namespace cv;
using namespace std;

int main()
{
    int numBoards = 0;
    int numCornersHor;
    int numCornersVer;

    printf("Enter number of corners along width: ");
    scanf("%d", &numCornersHor);

    printf("Enter number of corners along height: ");
    scanf("%d", &numCornersVer);

    printf("Enter number of boards: ");
    scanf("%d", &numBoards);

    int numSquares = numCornersHor * numCornersVer;
    Size board_sz = Size(numCornersHor, numCornersVer);
    VideoCapture capture = VideoCapture(0);

    vector<vector<Point3d>> object_points;
    vector<vector<Point2d>> image_points;

    vector<Point2d> corners;
    int successes=0;

    Mat image;
    Mat gray_image;
    capture >> image;

    vector<Point3d> obj;
    for(int j=0;j<numSquares;j++)
        obj.push_back(Point3d(j/numCornersHor, j%numCornersHor, 0.0f));

    while(successes<numBoards)
    {
        cvtColor(image, gray_image, CV_BGR2GRAY);

        bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

        if(found)
        {
            cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
            drawChessboardCorners(gray_image, board_sz, corners, found);
        }

        imshow("win1", image);
        imshow("win2", gray_image);

        capture >> image;

        int key = waitKey(1);

        if(key==27)
            return 0;

        if(key==' ' && found!=0)
        {
            image_points.push_back(corners);
            object_points.push_back(obj);
            printf("Snap stored!\n");

            successes++;

            if(successes>=numBoards)
                break;
        }
    }

    Mat intrinsic = Mat(3, 3, CV_32FC1);
    Mat distCoeffs;
    vector<Mat> rvecs;
    vector<Mat> tvecs;

    intrinsic.ptr<float>(0)[0] = 1;
    intrinsic.ptr<float>(1)[1] = 1;

    calibrateCamera(object_points, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs);

    Mat imageUndistorted;
    while(1)
    {
        capture >> image;
        undistort(image, imageUndistorted, intrinsic, distCoeffs);

        imshow("win1", image);
        imshow("win2", imageUndistorted);

        waitKey(1);
    }

    capture.release();

    return 0;
}

我在控制台上遇到的错误是

OpenCV 错误:未知函数文件中的断言失败(ncorners >=0 && corners.depth() == CV_32F),文件.....\src\opencv\modules\imgproc\src\cornersubpix.cpp,行257.

错误对话框显示

basiccalibration.exe 中 0x769afc16 处未处理的异常:Microsoft C++ 异常:cv::Exception at memory location 0x0021f51c..

我们将不胜感激。 谢谢

【问题讨论】:

  • opencv 包中有一个用于校准相机的示例代码,你在用吗?
  • 不,我想编写自己的代码。我试过了,但它有太多错误,比如未定义的函数。
  • 我正在尝试运行与您相同的代码。实际上我的代码有点不同,但我对其进行了修改,以便让一些东西接近你并尝试在这里解决我的问题。一些问题:在你的最终版本中,你真的使用了两次 numCornersHor 吗?为什么不使用 numCornersVer 作为第二个参数?当您声明它们时,您是否将它们定义为浮动?感谢您的帮助!

标签: opencv camera camera-calibration


【解决方案1】:

使用 Point2f 和 Point3f 代替 Point2d 和 Point3d。请阅读断言文本。它需要一个 CV_32F 深度结构。

【讨论】:

  • 我是新手,所以我不明白这个错误,所以我不明白你所说的要求 CV_32F 深度结构的意思。当我使用点 2f 和 3f 时,它会显示警告 C4244:'argument':从 'int' 转换为 'float',可能会丢失行 'obj.push_back(Point3f(j/numCornersHor, j%numCornersHor, 0.0f) 的数据));'它显示了两次,所以构建失败了,这就是我将其更改为 point3d 和 point2d 的原因。
  • 所以我得到了错误。任何建议,感谢您的回复。希望我能解决这个问题。
  • 您知道,警告就在那里,因为您将一个整数分配到浮点数为(j/numCornersHor,j%cornersHor) 的位置。关于深度,在documentation of cv::Mat.depth() 中解决
  • 所以我应该将 numcornerver 和 hor 更改为浮动。
  • 在我将它们转换为浮点数后,断言是否不再出现,但断言错误中的代码不在我的代码中,它需要什么矩阵作为深度为 CV_32F角落是我程序中的一个向量。关于 int to float 错误,我尝试将其转换为 float,但出现了更多错误。我如何让它浮动
猜你喜欢
  • 2014-10-23
  • 1970-01-01
  • 2012-04-18
  • 2016-12-29
  • 2011-08-03
  • 2014-06-19
  • 2013-02-24
  • 2019-01-05
  • 1970-01-01
相关资源
最近更新 更多