OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv:: cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3737
From the conversation in the comments to the question, we saw that VideoCapture
gives frame in grayscale. So the call to cvtColor
caused the crash.
...
Mat frame;
cap >> frame; // frame is already CV_8UC1
//cvtColor(frame, edges, CV_BGR2GRAY); // so don't to convert here, or crash!
edges = frame.clone();
...
Joissa.R
Updated on September 06, 2020Comments
-
Joissa.R over 3 years
Hi I am trying to run this sample code from OpenCV:
#include "opencv2\opencv.hpp" using namespace cv; int main(int, char**) { VideoCapture cap(0); // open the default camera if (!cap.isOpened()) // check if we succeeded return -1; Mat edges; namedWindow("edges", 1); for (;;) { Mat frame; cap >> frame; // get a new frame from camera 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; } // the camera will be deinitialized automatically in VideoCapture destructor return 0; }
I am currently using a Windows 7 x64 BootCamp on a Macbook Pro. I'm running this code with Visual Studios 2013 and OpenCV 2.4.9.
This is how I've set up my Config Properties:
VC++ Directories: Include Directories: H:\opencv\build\include;$(IncludePath) Linker:General:Additional Library Directories: H:\opencv\build\x64\vc12\lib;%(AdditionalLibraryDirectories) Linker:Input:Additional Dependencies: opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_nonfree249.lib;opencv_objdetect249.lib;opencv_ocl249.lib;opencv_photo249.lib;opencv_stitching249.lib;opencv_superres249.lib;opencv_ts249.lib;opencv_video249.lib;opencv_videostab249.lib;%(AdditionalDependencies)
When I click on Local Windows Debugger in Release x64 mode I get the following error from Visual Studios:
First-chance exception at 0x000007FEFD21B3DD in Project3.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000019A8A0.
If there is a handler for this exception, the program may be safely continued.
When I click Break instead (scared to press Continue), a window named Edges does pop up and the camera does turn on since the green light turns on. But I also get the following error in the command window:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv:: cvtColor, file ........\opencv\modules\imgproc\src\color.cpp, line 3737
I'm pretty new to C++ and Visual Studios, any help would be appreciated. Thanks in advance!
-
Miki almost 9 yearsworks for me "as is". That error should mean that your frame is not RGB.
-
Joissa.R almost 9 yearsSorry @Miki I'm very new to this. If my frame isn't RGB, what should I do to fix it?
-
Miki almost 9 yearsThe code works ok (it's copy&paste from the opencv docs, right?), so it's not a problem with the code. Try adding
std::cout << frame.channels();std::cout << edges.channels();
before cvtColor, it should print a lot of 31 -
Joissa.R almost 9 years@Miki Yep It was just copy-pasted. I added the code you suggested and it didnt print anything out. It's still producing the same error and breaking out from the program.
-
Miki almost 9 yearsso it didn't enter in the for loop?
-
Joissa.R almost 9 years@Miki Oh actually I didnt notice, it only printed "11" before the error
-
Miki almost 9 yearsSo your camera gives you images in grayscale. So remove cvtColor line, and add
edges = frame.clone();
instead. -
Joissa.R almost 9 years@Miki Thank you! That fixed it
-
Miki almost 9 yearsNice! Let me put this as an answer, so others will find it without reading all comments
-
-
CODError about 8 yearsI think videocapture gives frames in BGR. I am using something similar and it gives same error. If I remove cvtColor, I still get the error.
-
Miki about 8 years
VideoCapture
usually gives frames in BGR. This case was different. If you have another problem, please ask a new question @CODError