-1

我试图通过网络摄像头同时检测不同的颜色。以下是我的代码,但出现错误:

错误:未知函数中的错误参数<数组应为 CvMat 或 IplImage>。

随之而来的是:

0x7c812fd 处未处理的异常: cv:: 内存位置 0x0011fb70 处的异常

我认为错误可能是由

IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); 

但我不确定并且知道如何解决它。有人可以给我一些提示吗?

    #include "stdafx.h"

    #include <cv.h>
    #include <highgui.h>

// from here to main function is my part.
    IplImage* GetThresholdedImage(IplImage* imgHSV, CvScalar lower, CvScalar upper)
    {       
        IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
        cvInRangeS(imgHSV, lower, upper, imgThresh); 
        return imgThresh;
    }

    IplImage* hsv;

    CvScalar blue_lower = cvScalar(110,60,10);
    CvScalar blue_upper = cvScalar(120,256,256);
    CvScalar green_lower = cvScalar(40,60,10);
    CvScalar green_upper = cvScalar(71,256,256);

    /* Get the images thresholded for blue and green. */
    IplImage* blue_mask = GetThresholdedImage(hsv, blue_lower, blue_upper);
    IplImage* green_mask = GetThresholdedImage(hsv, green_lower, green_upper);

    int main(){
          CvCapture* capture =0;       

          capture = cvCaptureFromCAM(0);
          if(!capture){
                printf("Capture failure\n");
                return -1;
          }

          IplImage* frame=0;
          cvNamedWindow("Video");      
          cvNamedWindow("Ball");


          while(true){

                frame = cvQueryFrame(capture);            
                if(!frame) break;

                frame=cvCloneImage(frame); 
                cvSmooth(frame, frame, CV_GAUSSIAN,3,3); 

                IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); 
                cvCvtColor(frame, imgHSV, CV_BGR2HSV); 
                IplImage* imgThresh = GetThresholdedImage(imgHSV, blue_lower, blue_upper);
                IplImage* imgThresh2 = GetThresholdedImage(imgHSV, green_lower, green_upper);
                cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3);
                cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3);
                cvShowImage("Ball", imgThresh);  
                cvShowImage("Ball2", imgThresh2);
                cvShowImage("Video", frame);


                cvReleaseImage(&imgHSV);
                cvReleaseImage(&imgThresh);   
                cvReleaseImage(&imgThresh2);
                cvReleaseImage(&frame);


                int c = cvWaitKey(10);
                if((char)c==27 ) break;      
          }

          cvDestroyAllWindows() ;
          cvReleaseCapture(&capture);     

          return 0;
    }
4

1 回答 1

1

看起来您可能想阅读pre-main initialization。您的问题不在此列:

IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);

但在这个:

IplImage* blue_mask = GetThresholdedImage(hsv, blue_lower, blue_upper);

blue_mask(而且green_mask,也是)必须在您调用main(). 但是,您也声明IplImage* hsv;了没有初始化它,因此错误说您有一个错误的论点。hsv要么是垃圾,要么是NULL一些未初始化的垃圾,这两者都是非常糟糕的事情。

要解决此问题,只需删除声明hsvblue_maskgreen_mask. 然后您的程序将按您的预期运行。

于 2013-07-08T22:46:59.690 回答