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>
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.
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.
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
Are you using visual studio 2010 to run the OpenCV code? If so, try compiling in Release mode.
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.
Related
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>
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.
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.
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
Are you using visual studio 2010 to run the OpenCV code? If so, try compiling in Release mode.
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.
I am newbie to OPENCV.
Now I am trying to display an image using imread(), but imread() doesn't work(keep returning empty mat.) But when I try to draw lines, circles and so on by imread(), it works well.
So I thought there would be something wrong with addressing. And I tried everything I can but it still doesn't work.
And I also have googled a lot of things to solve it, but I can't get any answer to this problem.
What should I do? Is there something wrong I have missed?
I currently use Windows 10, Visual Studio 2017, and openCV 4.0.0 alpha.
Here is my code.
#include <iostream>
#include <opencv2/opencv.hpp>
#ifdef _DEBUG
#pragma comment(lib,"opencv_world400d.lib")
#else
#pragma comment(lib,"opencv_world400.lib")
#endif
using namespace std;
using namespace cv;
int main()
{
Mat image;
image =imread("C:/Users/ymin/source/repos/Project1/Project1/BENZ.bmp",IMREAD_ANYCOLOR);
if (image.empty())
{std::cerr << "Could not open file" << std::endl; return (1);}
imshow("image", image);
waitKey();
return 0;
}
From the OpenCV documentation for imread:
If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix.
This indicates that the file is not there (maybe typo?), you don't have permission to read it, it is in a format not recognized by OpenCV, or it is corrupted.
Make sure the file is where you think it is, and that it is readable, and that OpenCV is compiled to support whatever format the file is in.
(Note that the file extension does not determine its format, you can take a JPEG file and rename it to have a .bmp extension, but it is still a JPEG file.)
Thanks for all the replies!
I've added the ..\wavFile.wav in the command argument.
But I still cant use the command window.
It still pops up and close immediately.
Maybe its because I use the console application to run this program?
Or are there other reasons?
I am new to opencv and I tried the following code to load and display an image
(using visual studio 2012)
I ran it using the debug mode, but I always get a window shows that
Usage: display_image ImageToLoadAndDisplay,and the window close immediately
(seems like argc is always equal to 2?)
The window wont stay there and wait for a command to load my image.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return 0;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
cvNamedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Might be a really stupid question but I really cant figure it out for a long time.
Hope someone can help me! THANKS A LOT!
Right click your project in Solution Explorer and select Properties from the menu
Go to Configuration Properties -> Debugging
Set the Command Arguments in the property list.
source : https://stackoverflow.com/a/3697320/4499919
OR
The Mozilla.org FAQ on debugging Mozilla on Windows is of interest here.
In short, the Visual Studio debugger can be invoked on a program from the command line, allowing one to specify the command line arguments when invoking a command line program, directly on the command line.
This looks like the following for Visual Studio 8 or 9
devenv /debugexe 'program name' 'program arguments'
It is also possible to have an explorer action to start a program in the Visual Studio debugger.
source : Debugging with command-line parameters in Visual Studio
OR
https://stackoverflow.com/questions/24202291/opencv-imread-from-command-line-argv1
I just started with OpenCV. I downloaded OpenCV 2.4.9, and installed MSVS 2010. My Windows is X64. I followed the following steps:
a. Under Configuration Properties, click Debugging -> Environment and copy paste: PATH=C:\opencv\build\x86\vc10\bin
b. VC++ Directories -> Include directories and add the entries: C:\opencv\build\include
c. VC++ Directories -> Library directories and add the entries: C:\opencv\build\x86\vc10\lib
d. Linker -> Input -> Additional Dependencies and add the following:
opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_nonfree249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_objdetect249.lib;opencv_ts249.lib;opencv_video249.lib;
I ran the following code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
cv::waitKey(1000);
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::waitKey(50);
return 1;
}
To get the error:
Unhandled exception at 0x76d2b727 in BTP1.exe: Microsoft C++ exception: cv::Exception at memory location 0x003af414
I figured this might be because of the X64 and x86 mismatch. On changing the entries in a. to PATH=C:\opencv\build\ x64 \vc10\bin and in c. to C:\opencv\build\ x64 \vc10\lib, I get the following error:
The application was unable to start correctly (0xc000007b). Click OK to close the application.
Any tips on how I can get over this issue?
This is probably happening because the image you are trying to display is empty, perhaps because the image isn't in the right folder. To confirm this, change your code to
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream> // std::cout
int main() {
// read an image
cv::Mat image= cv::imread("img.jpg");
// add the following lines
if(image.empty())
std::cout << "failed to open img.jpg" << std::endl;
else
std::cout << "img.jpg loaded OK" << std::endl;
... // the rest of your code
Resolved the problem. On some tinkering, I found that the program was running in the Release mode, and not the Debug mode.
It was a problem with the Additional Dependencies. Did not add the Debug versions of the same. (XYZ249d.lib)
To add to the other answers, this also commonly occurs if you pass a color image into a tool that requires a grayscale image (ie single channel).
You can convert it to grayscale using the following code:
cv::Mat img_gray;
cv::cvtColor(img_color, img_gray, cv::COLOR_BGR2GRAY);
You can extract and combine individual color channels using the following code:
cv::Mat img_bgr[3];
cv::split(img_color, img_bgr);
//Note: OpenCV uses BGR color order
//img_bgr[0] = blue channel
//img_bgr[1] = green channel
//img_bgr[2] = red channel
cv::Mat img_gray = img_bgr[2] - img_bgr[1]; //laser line extraction is typically red channel minus green channel
I had a similar problem, I just had to give the path of the image file
for example -
D:\image.png
Mat img=imread("box.png",1);
Mat img=cvLoadImage("box.png",1);
When i tried the former one, the project couldn't load the file but when i used the latter one, it did. So.. and box.png was in the project folder. Can you help me what is wrong with my imread? or should i add a directory where box.png is located?
what im trying to make is this code pulling out mser regions from the image. And the error occured running the code when compiler couldn't load the image file, and also even if i had changed imread to cvLoadImageM the code stopped at
ms(box, regions, Mat());
so am i doing something wrong????????here
#include <stdio.h>
#include <iostream>
#include <vector>
//#include <string.h>
#include "opencv\cv.h"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat box = imread("01a.png",1);
if(box.empty())
{
fprintf(stderr, "Can not load image" );
return -1;
}
MSER ms;
vector<vector<Point>> regions;
ms(box, regions, Mat());
for (int i = 0; i < regions.size(); i++)
{
ellipse(box, fitEllipse(regions[i]), Scalar(255));
}
imshow("mser", box);
waitKey(0);
return 0;
}
I had the same problem yesterday and I found a solution. Make sure the opencv library used matches your build mode (e.g. Release -> cvcore242.lib (dll) Debug -> cvcore242d.lib (or dll)) for each opencv library. Make sure to change your project deps->linker->dependencies to load the correct (Debug or Release) opencv libraries.
An alternative is to switch your build mode Debug <-> Release but I imagine you want to set up each one of the build modes with the proper opencv libraries, for the long run.
imread() is part of the new OpenCV C++ interface and is used with a Mat structure. cvLoadImage() is a C function that returns a IplImage* (pointer to IplImage)
like Cricketer said cvLoadImage() returns a pointer to the image.
Change this in your code
- Mat box = imread("01a.png",1);
+ IplImage* pBox = cvLoadImage("01a.png");
+ Mat box = pBox;
The code worked fine for me both ways putting in the full path to the image
"C:\\Users\\noob\\Pictures\\01a.png"
so, imread failed, and you tried to resolve that using cvLoadImage ? bah, no, don't do that.
please go and check, if you're mixing debug & release code, like *d.libs linked to release build or the other way. it's most likely related to that