简体   繁体   中英

OpenCV unhandled exception error

I am running the code I am using the opencv functions imread() and the data structure Mat.

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main(){
    int x;
    Mat img = imread("D:/OwnResearch/photo2.jpg");
    std::cout << img << std::endl;
    std::cin >> x;
    return 0;


}

And I keep receiving the error Unhandled exception at 0x0000000000000000 in opencvtest.exe: 0xC0000005: Access violation executing location 0x0000000000000000. It seems like nothing is being loaded. I checked the directory of the file and it seems to be correct. I am not sure what the problem is.

The issue is probably in the DLLs you are linking against. Make sure you use the proper ones - release dlls for a release build and debug dlls for a debug build. This is a very common mistake so I suggest you look at it first. Second as mentioned in the comments and in the reply by @1nflktd you are also trying to print you image in the terminal. First of all I don't think this is indeed a defined in the library and second of all I don't really see the point in doing that (<- It seem I was wrong - the new C++ interface allows printing a cv::Mat directly ). If you want to print the image's attributes you can use cv::Mat::row, cv::Mat::col etc. (see here or simply write img.[TRIGGER AUTOCOMPLETE] to get all the things you can access in a cv::Mat object). If you really want to print the data (pixel array) of your image you need to call cv::Mat::data and cast it accordingly if needed. Careful though since you will get a huge number of values. A 10x10 image has 100 values, a 100x100 has 10000 values and a 1000x1000 has 1000000 values in its pixel array.

If the DLLs are okay, try doing the following:

  1. Generate an image using Mat img(X,Y,CV_XXXX,Scalar(...)) , where X and Y are the dimensions of the image, CV_XXXX is the memory unit used to store the pixel data (for example CV_32FC2) and Scalar(...) represents the values of each color channel you want to be used for all the pixels in your image.
  2. Try to display the generated image. I had bad experience in Windows with OpenCV not a long time ago where a similar error appeared because I deleted the stdafx.h in Visual Studio, which led to incorrect interpretation of the string I passed to the cv::imread(...) function. This was the way I used to actually see where the problem was coming from. If you are working on a Windows machine and have this issue this is also a good place too look at. If the procedurally generated image does load and is show correctly, then this is probably the issue (if you have deleted the above mentioned header).

If you want to display your image, just use imgshow

Mat img = imread("D:/OwnResearch/photo2.jpg"); 
if(!img.data) // check if it is loaded
{
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}

namedWindow("TestWindow", WINDOW_AUTOSIZE); // Create a window for display.
imshow("TestWindow", img); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;

For more information see the docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM