load image with openCV Mat c++
Asked Answered
T

5

11

I want to load 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 ); 

I am getting the following error in a message box:

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

Please note that I am including:

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <math.h>
Throttle answered 19/4, 2013 at 16:38 Comment(0)
G
15

I've talked about this so many times before, I guess it's pointless to do it again, but code defensively: if a method/function call can fail, make sure you know when it happens:

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 Windows' path uses backslash \ instead of the standard / used on *nix systems. You need to escape the backslash when passing the filename: C:\\images\\apple.jpg

Calling waitKey() is mandatory if you use imshow().

EDIT:

If it's cv::imread() that is throwing the exception the only solution I know to work is downloading OpenCV sources and building it on the machine, since re-installing OpenCV doesn't fix the issue.

Guthrey answered 19/4, 2013 at 17:9 Comment(6)
@ karlphillip - even with your answer I am having the same problem.Throttle
Unhandled exception at 0x70270149 in matching.exe: 0xC0000005: Access violation reading location 0xcccccccc.(displayed in a message box)Throttle
If it's cv::imread() I've seen this kind of exception being thrown before. The only solution was to download OpenCV sources and COMPILE it on the machine. Re-installing OpenCV didn't fix the problem.Guthrey
@Guthrey If we want to read many images from different folder than what should we do for that ?Michiko
@Wish_2_fly If we want to store several different numbers, how do we do that in a program? int n1 = 10; int n2 = 20; int n3 = 30; ...Guthrey
@Guthrey i know in a loop , for(int n1=0;n1<=10;n1++) but when i did it in my program , like ; Mat T=imread("C:\Pick") and than loop it , it didn't work , here is my question <a>#17972030Michiko
L
2

Have you checked that I exists after imread? Perhaps the file read failed

After reading a file do if ( I.empty() ) to check if it failed

Leopold answered 19/4, 2013 at 16:42 Comment(3)
@ Martin Beckett yes, there is image "apple.jpg"in the link C:/images/apple.jpgThrottle
Yes but what does "I.empty()" return? You might not have permission, the file migth be corrupt, you might not have the jpeg.dll etc etcvLeopold
@ Martin Beckett I am getting the above mentioned error- May be I do not have the library or I am wrongly performing the includes. please help me.Throttle
M
2

I don't know why you don't have include problem because normally it .hpp file so you're suppose to do

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

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

Maffei answered 19/4, 2013 at 16:43 Comment(3)
As @Martin Beckett said, can be just because C:/Images/apple didn't exist so your Mat is empty. So do if(!I.empty()).Maffei
@ Alexandre Bizeau when I copy and paste your include I am getting this error "fatal error C1083: Cannot open include file: 'opencv2/highgui/highgui.hpp'"Throttle
Does not necessarily copy exactly what I put because your Include Directories is maybe not set as mine. As I can see with your. You maybe just need to do #include <highgui.hpp> or maybe #include <highgui/highgui.hpp>. Depend how you set the folder and your project propertiesMaffei
O
0

Are you using visual studio 2010 to run the OpenCV code? If so, try compiling in Release mode.

Outbalance answered 9/5, 2013 at 9:44 Comment(0)
G
0

As pointed out by @karlphillip, however trivial it may sound but this statement " You need to escape the backslash when passing the filename: C:\images\apple.jpg" is really important.

Gonzalo answered 16/11, 2019 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.