使用Open CV时Visual Studio中未处理的异常

时间:2022-02-01 21:23:30

I'm new to Visual Studio. I keep getting an error: Unhadled exception occurred at [some memory location] in Project1.exe The memory location keeps changing each time I hit debug. I have tried really very simple codes, but still I keep getting this error.

我是Visual Studio的新手。我一直收到错误:Project1.exe中[某些内存位置]发生了未处理的异常每次调试时内存位置都会不断变化。我尝试过非常简单的代码,但我仍然遇到这个错误。

The window that opens when I debug also gives an error:

我调试时打开的窗口也会出错:

OpenCV Error: Assertion Failed (scn == 3 || scn == 4) in unknown function, file ..\..\..\src\opencv\modules\imgproc\src\color.cpp, line 3042

OpenCV错误:断言失败(scn == 3 || scn == 4)在未知函数中,文件.. \ .. \ .. \ src \ opencv \ modules \ imgproc \ src \ color.cpp,第3042行

The code I'm running is:

我正在运行的代码是:

    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include "opencv2/core/core.hpp"
    #include "opencv2/features2d/features2d.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/calib3d/calib3d.hpp"
    #include "opencv2/video/video.hpp"
    #include "opencv2/opencv.hpp"
    using namespace cv;
    using namespace std;

    int main (int argc, char *argv[])
    {
      cv::Mat frame;
      cv::Mat back;
      cv::Mat fore;
      cv::Mat edges;
      VideoCapture cap (0);
      BackgroundSubtractorMOG2 bg;
      bg.set ("nmixtures", 3);
      bg.set ("detectShadows", false);
      std::vector < std::vector < cv::Point > >contours;

      cv::namedWindow ("Frame");
      cv::namedWindow ("Background");

      for (;;)
        {
          cap >> frame;
          cvtColor(frame, edges, CV_BGR2GRAY);

          GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);

          Canny(edges, edges, 0, 30, 3);

          imshow("edges", edges);

          if(waitKey(30) >= 0) break;

        }
        return 0;
    }

1 个解决方案

#1


1  

You need to check that frame is valid, i.e. !frame.empty() or instead of for(;;) use while(cap >> frame).
When the capture reaches the end of the file you are getting an empty frame which causes cvtColor() to fail with that error.

您需要检查该帧是否有效,即!frame.empty()或代替(;;)使用while(cap >> frame)。当捕获到达文件末尾时,您将获得一个空帧,导致cvtColor()失败并显示该错误。

#1


1  

You need to check that frame is valid, i.e. !frame.empty() or instead of for(;;) use while(cap >> frame).
When the capture reaches the end of the file you are getting an empty frame which causes cvtColor() to fail with that error.

您需要检查该帧是否有效,即!frame.empty()或代替(;;)使用while(cap >> frame)。当捕获到达文件末尾时,您将获得一个空帧,导致cvtColor()失败并显示该错误。