c++ - 无法绘制棋盘角,因为 cvCvColor 方法中的断言失败

标签 c++ opencv camera-calibration

我正在从两个相机收集好帧以用于立体相机校准目的。

  #include <iostream>
  #include <cv.h>
  #include <highgui.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <sstream>

  using namespace std;

  int main() {

  CvCapture* videocapture_CAM0=cvCreateCameraCapture(0);    
  CvCapture* videocapture_CAM1=cvCreateCameraCapture(1);

  IplImage* img_0;    
  IplImage* img_1;

  int nx=4,ny=3;

  int i, j, nframes, n = nx*ny;

  vector<CvPoint2D32f> temp_0(n);    
  vector<CvPoint2D32f> temp_1(n);

  CvSize imageSize_0 = {0,0};    
  CvSize imageSize_1 = {0,0};

  while(1)  
  {
      int count_0 = 0,count_1 = 0;
      int result_0 = 0,result_1 = 0;

      img_0=cvQueryFrame(videocapture_CAM0);
      img_1=cvQueryFrame(videocapture_CAM1);

      imageSize_0 = cvGetSize(img_0);
      imageSize_1 = cvGetSize(img_1);    

      cvShowImage("camera 1", img_0);
      cvShowImage("camera 2", img_1);

      IplImage* timg_0 = img_0;
      IplImage* timg_1 = img_1;

      result_0 = cvFindChessboardCorners(timg_0, cvSize(nx, ny), &temp_0[0], &count_0,
                            CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);

      result_1 = cvFindChessboardCorners(timg_1, cvSize(nx, ny), &temp_1[0], &count_1, 
                            CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);

      if(result_0&&result_1)
      {
            cout<<"Found"<<endl;

            IplImage* cimg_0 = cvCreateImage(imageSize_0, 8, 3);            

            IplImage* cimg_1 = cvCreateImage(imageSize_1,8,3);

            cvCvtColor(img_0, cimg_0, CV_GRAY2BGR);
            cvCvtColor(img_1, cimg_1, CV_GRAY2BGR);

            cvDrawChessboardCorners(cimg_0, cvSize(nx, ny), &temp_0[0], count_0, 
                                    result_0);

            cvDrawChessboardCorners(cimg_1, cvSize(nx, ny), &temp_1[0], count_1,
                                    result_1);

            cvShowImage("corners", cimg_0);
            cvShowImage("corners", cimg_1);

            cvReleaseImage(&cimg_0);
            cvReleaseImage(&cimg_1);

            cvWaitKey(5);

       }
       char c=cvWaitKey(30);
       if(c==27)
        break;
     }
     return 0;
  }

但是我在运行时遇到了一个奇怪的错误:-

OpenCV Error: Assertion failed (scn == 1 && (dcn == 3 || dcn == 4)) in cvtColor,
 file C:\opencv\modules\imgproc\src\color.cpp, line 3234

此外,我还检查了源图像和目标图像的维度和 nchannel 值是否相等。

提前致谢!!

最佳答案

断言很明确scn == 1意味着您的源图像必须有一个 channel (那是因为您指定了 CV_GRAY2BGR 作为 cvCvtColor 的转换代码)。

关于c++ - 无法绘制棋盘角,因为 cvCvColor 方法中的断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11792091/

相关文章:

opencv calibrateCamera 函数产生不好的结果

c++ - 级联文件加载错误

c 开放式简历;如何将二维数组转换为 IplImage?

matlab - 什么是最好的相机校准方法?

c++ - 关于内存缓存的指针性能

c++ - 使用 cmake 在项目中链接 Opencv

OpenCV:从平移和旋转中获取透视矩阵

c++ - Windows Media Foundation 中的视频捕获可以用于屏幕捕获吗?

c++ - 三元条件结果法

c++ - 未定义对外部类的引用?