1

I am a beginner in image processing with OpenCV and Visual Studio. I have a paragraph of code which I don't understand:

Mat image;
image = imread(filename, IMREAD_COLOR); // Read the file
if (! image.data )                      // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}

In the third line, what do ! and .data mean? How do they check for invalid input?

unwind
  • 391,730
  • 64
  • 469
  • 606
jincong
  • 13
  • 3
  • sorry, i am not familiar with the format. please ignore "
    " at the end of every line. :)
    – jincong Jun 17 '13 at 14:41
  • 2
    It does the same thing as `if (NULL == image.data)` – IronMensan Jun 17 '13 at 14:42
  • 8
    It appears you're also a beginner in C++. This is straightforward usage of the `.` member access operator and the `!` logical negation operator (testing whether the member is a NULL pointer). Read a good C++ book. We have [a list of good ones](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ben Voigt Jun 17 '13 at 14:43
  • And the same thing as any sort of check of a value for null. – Hot Licks Jun 17 '13 at 14:43

1 Answers1

6

cv::Mat::data is a pointer to the data buffer held internally by a cv::Mat object. If it evaluates to false, it means no data has been loaded, and the pointer cannot be de-referenced.

It is the equivalent of comparing image.data against NULL, 0, or nullptr in C++11.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480