I am trying to display a basic image that is loaded off disk using the highgui module within the cv2 library.
I am able to do this when making a Qt Widgets application but the Qt window becomes redundant; however when using a console application I obtain this error:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp, line 269
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow
Here is my code:
int main ()
{
cv::Mat inputImg = cv::imread("/home/pi/scrot1.png");
cv::imshow("Display Image", inputImg);
}
I have written a script in python that can be run via terminal and it calls the high gui module. I would like to write this in C++ but I cannot seem to be able to load a basic image!
EDIT:
The path was actually invalid. However, even with the correct path I cannot see the image with HighGui.
The error means that the image is not loaded correctly.
You can check this using:
if(image.empty()) {
// not loaded correctly
}
You need to be sure that:
the path is valid
the image is not corrupted
you have the privileges to access that folder
To actually display the image, you need to use cv::waitKey() after the imshow.
Related
I have a C++ program using openCV that opens a webcam.
The lines that do it are the followings:
cout<<"camera initializing\n";
VideoSettings cam(camNum + CAP_V4L);
cout<<"camera initialized\n";
cout<<"Ch3ck c4m3ra is 0p3n3d\n";
if ( !cam.isOpened())
{
cout << "Could not open reference " << sourceReference << endl;
return -1;
}
I compiled the program and it runs without problems.
However I would like sometimes to launch the same program on Ubuntu18.04 twice with the same cam (whose id is passed as bash argument).
The error I get when launching the second instance that opens the same cam is:
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Unable to stop the stream: Device or resource busy
VIDEOIO ERROR: V4L: can't open camera by index 0
Of course the code is stopping becasue cam.isOpened() is not verified.
The same program (removing CAP_V4L) on OSX can be launched many times without any problem.
Why the behaviour is different? And there is any simple workaround to have the same behaviour on Ubuntu?
I have simple OpenCV code that I am using to load an image and convert it to grayscale. I have a folder with images that I have replicated. It's just the same frame over and over again with different file names, the content of the files are exactly the same.
I run a loop and try to convert the images and it runs for 1020 frames and stops on that specific frame every time with the error:
"error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor"
popping up every time. I don't understand this, if the code works for the first image, in theory it should work for all the other images in the folder as they are the same file just with different file names. I am running OpenCV 3.0 and also receive the specific error that says:
"OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor,file/home/blah/OpenCV3.0/opencv/modules/imgproc/src/color.cpp, line 7453"
when the program stops.
My code is:
char * baseImagePath; //these are updated as the program iterates
char * nextImagePath; //they contain the full path of the image
Mat baseImage, nextImage;
Mat grayImage1,grayImage2;
baseImage = imread(baseImagePath, CV_LOAD_IMAGE_COLOR);
if(baseImage.empty()){
printf("%s EMPTY!\n", baseImagePath);
}
cvtColor(baseImage, grayImage1, COLOR_BGR2GRAY);
nextImage = imread(nextImagePath, CV_LOAD_IMAGE_COLOR);
if(nextImage.empty()){
printf("%s EMPTY!\n", nextImagePath);
}
cvtColor(nextImage, grayImage2, COLOR_BGR2GRAY);
So, this is very odd. When I run my code, I confirm that my program thinks the image at a specific frame is empty, but when I run:
display frame.jpg
were frame.jpg is the frame that my program claims is empty, imageMagick displays the video just fine.
The problem was not with the OpenCV code, but with the C code I was using to iterate through the files. I had too many files open and that caused the program to fail. This was confirmed with the errno code I received upon program termination.
I'm trying to get the background image from BackgroundSubtractorMOG2:
bg->getBackgroundImage(back);
but I get a Thread 1 SIGABRT (which as a c++ n00b puzzles me)
and this error:
OpenCV Error: Assertion failed (nchannels == 3) in getBackgroundImage, file /Users/hm/Downloads/OpenCV-2.4.4/modules/video/src/bgfg_gaussmix2.cpp, line 579
libc++abi.dylib: terminate called throwing an exception
(lldb)
I'm not sure what the problem is, suspecting it's something to do with the nmixtures paramater, but I've left that as the default(3). Any hints ?
It looks like you need to use 3 channel images rather than grayscale. Make sure the image type you are using is CV_8UC3 or if you are reading from a file use cv::imread('path/to/file') with no additional arguments.
My MFC app runs various API from OpenCV2. Everything else is working fine. But when my program runs
cv::Mat result;
cv::equalizeHist(m_cvImage,result);
I get following runtime exception.
Unhandled exception at 0x7727fbae in OpenCVTest.exe: Microsoft C++ exception: cv::Exception at memory location 0x0029e944..
"C:\slave\WinInstallerMegaPack\src\opencv\modules\imgproc\src\histogram.cpp:2430: error: (-215) CV_ARE_SIZES_EQ(src, dst) && CV_ARE_TYPES_EQ(src, dst) && CV_MAT_TYPE(src->type) == CV_8UC1"
According to debugger, the exception was thrown in the middle of processing (about 40%) the image in equalizeHist. Is there anything I need to do? FYI: I am using binary OpenCV provided by its web site.
UPDATE:
I've resolved this issue by converting images to gray-level before equalizing it. I just didn't know
the function only works with gray-level image
images that look like gray-level can be non-gray.
I imagine the problem you are encountering is that m_cvImage is a 3-channel image. So, you need to convert it to a grayscale image before you can call equalizeHist.
cvtColor(m_cvImage, m_cvImage, CV_BGR2GRAY);
cv::Mat result;
cv::equalizeHist(m_cvImage, result);
Also, have a look at the EqualizeHist_Demo.cpp tutorial sample to see how it is used.
I have successfully written a video processing program. I used ubuntu and Netbeans for programming. When I run this program on netbeans it runs perfectly and gives expected output.
I built executable file of this program both in debug and release mode and tried to run them in the command line. Now I get the following error. But Netbeans doesn't complain about this. Could someone point out what might be the problem?
OpenCV Error: Bad argument (Array should be CvMat or IplImage) in cvGetSize, file /home/<user>/trunk/opencv/modules/core/src/array.cpp, line 1238
terminate called after throwing an instance of 'cv::Exception'
what(): /home/<user>/trunk/opencv/modules/core/src/array.cpp:1238: error: (-5) Array should be CvMat or IplImage in function cvGetSize
thank you in advance
Can you check if the input argument to cvGetSize is:
a NULL pointer? What is the result of querying/retrieving the frame?
a CvSeq?
a 1- or 3-dimensional array?
Usually it is the first.
That's the way OpenCV talks to you - it's more often the runtime exception than a compiler error.