0

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.

Amal G Jose
  • 2,486
  • 1
  • 20
  • 35
kingofjong
  • 9
  • 1
  • 3
  • Seems like you dont load dlls at runtime. Make sure you copy them to bin/debug or release – Gilad Feb 19 '15 at 17:11
  • 2
    I'm not sure that `std::cout << img << std::endl;` is well defined. You should use the simple image display function of OpenCV. – Cheers and hth. - Alf Feb 19 '15 at 17:11
  • I agree with @Cheersandhth.-Alf and Gilad, and on top of that I don't see the point in doing something like this. If you print the data (cv::Mat::data), even by a 100x100 image you will get 10000 values. You can easily print the width and height of the image by calling cv::Mat::row and cv::Mat::col and various other parameters if you want to retrieve some addition information about the image and its content. – rbaleksandar Feb 19 '15 at 17:42
  • @Cheersandhth.-Alf It is defined: http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#creating-a-mat-object-explicitly – Dan Feb 19 '15 at 21:35

2 Answers2

1

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).
Community
  • 1
  • 1
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • I was able to print the matrix img (type mat) before this way. I am just trying to view the values of the matrix. When I print the dimensions of the matrix it shows 0. I was wondering why that was? – kingofjong Feb 19 '15 at 19:17
  • Ah, okay. Well, the reason of a matrix's dimensions to be shown as 0 is that it's empty (additional check with cv::Mat::empty() should return TRUE). And an empty matrix in your case means the image has not been loaded properly. Can you write in your question what OS, IDE and compiler you are using? In Windows it's a little bit tricky where you have to place the image so that it is actually seen by the executable. I find this behaviour extremely annoying and it's been there for many, many years. If that's your case, place the image in the project's root folder. I think that was the correct one. – rbaleksandar Feb 19 '15 at 21:13
0

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

hlscalon
  • 7,304
  • 4
  • 33
  • 40
  • I was able to print the matrix img matrix before this way. I am just trying to view the values of the matrix. When I print the dimensions of the matrix it shows 0. I was wondering why that was? – kingofjong Feb 19 '15 at 19:18
  • I still get the Unhandled exception at 0x0000000000000000 in opencvtest.exe: error with your code – kingofjong Feb 19 '15 at 19:19
  • @kingofjong what compiler are you using ? Did you linked libs, include directory, etc. ? Post the complete arguments you are using to compile it. – hlscalon Feb 19 '15 at 19:24
  • Wrong answer: `cout` a `cv::Mat` is defined in OpenCV http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#creating-a-mat-object-explicitly – Dan Feb 19 '15 at 21:37
  • @Dan interesting to know, removed from the answer that part, anyway, OP should have been clear of what he was trying to do in his question – hlscalon Feb 19 '15 at 23:47