Upload image using openCV Mat C ++

I want to upload an image using Mat in openCV

My code is:

Mat I = imread("C:/images/apple.jpg", 0); namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display. imshow( "Display window", I ); 

The following error appears in the message box:

 Unhandled exception at 0x70270149 in matching.exe: 0xC0000005: Access violation reading location 0xcccccccc. 

Please note that I include:

 #include <cv.h> #include <cxcore.h> #include <highgui.h> #include <iostream> #include <math.h> 

Please, help. Thanks.

+3
source share
4 answers

I’ve already talked about this so many times , I think it’s pointless to do it again, but to protect the code : if the method / function call may fail, make sure you know when it will happen:

 Mat I = imread("C:\\images\\apple.jpg", 0); if (I.empty()) { std::cout << "!!! Failed imread(): image not found" << std::endl; // don't let the execution continue, else imshow() will crash. } namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display. imshow( "Display window", I ); waitKey(0); 

Note that the Windows path uses the backslash \ instead of the standard / used in * nix systems. You need to avoid backslash when transferring the file name: C:\\images\\apple.jpg

A call to waitKey() is required if you use imshow() .

EDIT

If it is cv::imread() that throws an exception , the only solution I know to work is to load OpenCV sources and create it on the machine, since reinstalling OpenCV does not fix the problem.

+4
source

Have you checked that I exist after imread? Possibly file read failure

After reading the file, run if ( I.empty() ) to see if it worked

+1
source

I do not know why you have no problem with the problem, because usually it is a .hpp file, so you should do

 #include <opencv2/highgui/highgui.hpp> #include <opencv2\core\eigen.hpp> 

But your code seems good, but add cv::waitKey(0); after your imshow.

+1
source

Are you using Visual Studio 2010 to run OpenCV code? If so, try compiling in Release mode.

0
source

Source: https://habr.com/ru/post/976480/


All Articles