Unhandled exception Microsoft C++ exception: cv::Exception at memory location - c++

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

Related

Load raw image data in Mat object in opencv c++ [duplicate]

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.

Show image on windows using imagemagick

I am using ImageMagick for a few modifications in images. My requirement is to capture desktop, update the captured image and show in the window (Canvas, Form or simple Win32 API image).
Everything working perfectly except showing a converted image on the window.
As per this discussion, display functionality of image magick is only supported by Linux and Mac (Please correct me if anything wrong).
The same forum suggested to use im_display to show the image on window. However, I am not able to locate any function related im_display() in the image magick library (Please correct me here if any additional include required).
When I am trying to call following code from visual studio then getting "delegate library support not built-in '' (X11) # error/display.c/DisplayImages/16224" error:
int main(int argc, char **argv)
{
try
{
Magick::InitializeMagick(NULL);
Image screen("screenshot:");
screen.display();
}
catch (exception &error_)
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
So the following are two primary questions:
Is it possible to show an image using image magick on windows (Form or canvas or win32)
If it's not possible then HBITMAP will help to render an image on windows. However, I am not able to find a way to convert Image Magick data to BITMAP. Can you please provide suggestions?

Loading and displaying image using opencv in vs2012

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

load image with openCV Mat c++

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.

imread not working in Opencv

I'm trying to use the imread function from OpenCV2.2.
My code is very simple.
cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);
After that, the host matrix became filled by zeros pointers, i.e. an image has not loaded.
If I use cvLoadImage then it all works properly.
The file exists, and I am not mixing the release and debug libraries. Why imread doesn't work?
Reproduced with opencv 2.4.8.
If you are running in Debug, check that you are also using debug libraries, it fixed our problem. : OpenCV imread(filename) fails in debug mode when using release libraries.
i was facing the same problem with 2.4.6 . The reason was that while selecting the library , i had selected both the debug version and the release version. When i selected only the debug version for the library everything worked fine
I can confirm, that there are some problems with imread in OpenCV 2.2. However the problems only occurred on a Windows 32bit system. On a linux and on a mac it worked. I can't tell why it didn't work, but we had a small workaround for that.
We fixed this problem with the following macros, maybe you could try this out and use "ourImread" from then on.
#ifdef WIN32
#define ourImread(filename, isColor) cvLoadImage(filename.c_str(), isColor)
#else
#define ourImread(filename, isColor) imread(filename, isColor)
#endif
I've had the same problem
cv::Mat image= cv::imread("immagine12.jpg"); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
cv::waitKey(5000);
return -1;
}
cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );
//non so perchè ma senza il waitKey e lasciando solo il system pause non carica l'immagine...
cv::waitKey(5000);
system("pause");
but I fixed it when I inserted the cv::waitKey(5000);
I don't know why but with system pause it can't load the image and it goes on pause after it loads the image!
I have similar problem on Linux, when reading 32 bit tiff images only.
Mat mat= imread(filename, CV_LOAD_IMAGE_ANYDEPTH);
Problem was because OpenCV was not build with Tiff support for some reason.
If you think that it is an OpenCV bug then, please, post your image and instructions for reproducing to the OpenCV bugtracker.
I know it is late but someone might found this helpful. I was facing the same problem while using imread using OpenCV-3.0. I tried all solutions but actually I was not adding the library opencv2/imgcodecs.hpp. Although imshow was working without it, but after I add this I was able to read the image.
Opencv4.3 Windows10 x64 Visual Studio 2019 I finally solved that problem!!!
Ok, now, when you meet the problem, you can try:
Check your settings, make sure you don't mix include .lib file in "Project->Properties->Linker->Input" for Release/Debug, that opencv_xxx430d.lib(note the d) is for Debug, and opencv_xxx430.lib(no d) is for Release, and the platform is x64.
And in C/C++->General, you need to set the Additional include directory that X:/(opencv_build/install)/include and X:/(opencv_build/install)/include/opencv2 respectively for Release/Debug.
Maybe you should #include <opencv2/imgcodecs.hpp>.
Finally, the Unicode Problem, which is also my problem that, the unicode symbol in the image file path, fuck! I don't know why because my code is very normal string path = "D:/kk.jpg(note that, it is / not \) but you can try to debug your code, maybe you can find there is something like path = "?D:/kk.jpg", what the fuck! So don't directly copy/paste your image path, even you've already use the absolute path, you can still meet the unicode problem, so, what you need to do is delete the line of code(string path = "D:/kk.jpg), and then just retype each character one by one! If you're lucky, you will see that image with imshow. Good luck.
see related question here
Please make sure your path is correct ,
According to the Opencv's API , I'd try this call:
arrayMat[i]=imread("1.jpg" , 1 );
The parameters for imread :
Mat imread(const string& filename, int flags=1)
Loads an image from a file.
Parameters:
filename – Name of file to be loaded.
flags – Specifies color type of the loaded image:
>0 the loaded image is forced to be a 3-channel color image
=0 the loaded image is forced to be grayscale
<0 the loaded image will be loaded as-is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB if ).
Good luck
S
I have the same problem. I solved it. The key is whether the filename has jpg.
If the filename is p1, you should use something like this imread("p1.jpg"). But we often set the filename as p1.jpg, here we should use something like this imread("p1.jpg.jpg").
This also happened to me, my simple solution was to use the C API and then convert to Mat:
IplImage* img = cvLoadImage("c://frame_201.bmp");
Mat mat = Mat(img);
The reverse is also true: if you are building Release and you have Debug libraries, then imread() quietly fails (errno is 0 after imread(), but the image object is not populated).
Another possibility:
If you're on OS X and statically link OpenCV be sure to use libjpeg which is bundled with OpenCV, not the system's one.
I had similar problem with OpenCV 3.0, except that cvLoadImage was not working as well. So, this might not really answer your question, but maybe it will help someone else.
I also had the very same problem that imread didn't work and cvLoadImage did work.
I decided to create a new Visual Studio from scratch and now it works.
There is no general problem with imread in OpenCV 2.4.3 under win32.
I know you want use "imread" & "CV_LOAD_IMAGE_GRAYSCALE" and convert automatically.
But this other way for load one picture and convert to gray scale:
define CV_NO_BACKWARD_COMPATIBILITY
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(){
/* load the image */
IplImage* img = cvLoadImage("yourPicture.bmp"); //jpg - bmp
/* retrieve properties */
int width = img->width;
int height = img->height;
int nchannels = img->nChannels;
int step = img->widthStep;
IplImage* img2 = cvCreateImage(cvSize(img->height, img->width),IPL_DEPTH_8U,1);
/* setup the pointer to access image data */
uchar *data = ( uchar* )img->imageData;
uchar *data2= ( uchar* )img2->imageData;
/* convert to grayscale manually */
int i, j, r, g, b, byte;
for( i = 0 ; i < height ; i++ ) {
for( j = 0 ; j < width ; j++ ) {
r = data[i*step + j*nchannels + 0];
g = data[i*step + j*nchannels + 1];
b = data[i*step + j*nchannels + 2];
byte = ( r + g + b ) / 3;
int v0=0, v1=0, v2=0;
data2[i*(img2->widthStep)+j*(img2->nChannels)+0] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+1] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+2] = byte;
}
}
cvNamedWindow("ImagenColor", 1);
cvShowImage("ImagenColor", img);
cvNamedWindow("Gray", 1);
cvShowImage("Gray", img2);
}