0

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.

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

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;
    }
MollieVX
  • 311
  • 1
  • 3
  • 10
  • This assertion failure indicates that the input image of some function does not have either 3 or 4 channels. The code you have posted works for me without errors. So this isn't whole code right? – OpenMinded Oct 15 '13 at 09:06

1 Answers1

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.

Adi Shavit
  • 16,743
  • 5
  • 67
  • 137