2

Trying to compile this sample project in order to get familiar with OpenCV:

#include <cv.h>
#include <highgui.h>

using namespace cv;

int main(int argc, char** argv)
{
    Mat image = imread(argv[1], 1);
    if (argc != 2 || !image.data) {
         printf("No image data \n");
         return -1;
    }

    namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
    imshow("Display Image", image);

    waitKey(0);

    return 0;
} 

I get the following compilation errors:

Description Resource    Path    Location    Type
Field 'data' could not be resolved  imageloader.cpp ‪/Session4‬ line 8  Semantic Error
Invalid arguments '
Candidates are:
void imshow(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, const ? &)
void imshow(const ? &, ?)
'   imageloader.cpp ‪/Session4‬ line 14 Semantic Error

Why can't I access the fields inside the Mat object? Not only the data field, but all fields. I build the opencv library using c-make and MinGW + I included the relevant header files and lib path in the project properties.

Any help would be most appreciated.

karlphillip
  • 92,053
  • 36
  • 243
  • 426

1 Answers1

0

There's a specific method in Mat that tests whether the data was successfully loaded:

// returns true if matrix data is NULL

bool empty() const;

So in your code, change the if statement to:

if (argc != 2 || image.empty()) 
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks for the swift answer, but the project still not compiling. now the error is : Method 'empty' could not be resolved. seems like i cant access any of the fields/methods inside a Mat object, but how can it be possible if the other lines of code in the beginning of the program compiling just fine? Thanks again.. – Ron Barabash Nov 25 '11 at 16:39
  • What opencv version are you using, and what are the paths you are including? For a more detailed [installation procedure check our guide for VStudio.](http://stackoverflow.com/questions/7011238/opencv-2-3-c-visual-studio-2010/7014918#7014918). I bet you are missing some includes. – karlphillip Nov 25 '11 at 17:32
  • opencv version 2.3.1 , i added the include and lib folder that was generated after the make and make-install operation, i added all of the libs in the "Libraries" section in project properties in eclipse. when i open the project hierarchy i can see all of the opencv include files and can access them all. the paths are opencvbuild\install\include and opencvbuild\install\lib when opencvbuild is the folder genrated by cmake. thanks again.. – Ron Barabash Nov 25 '11 at 20:04
  • OpenCV 2.3.1 superpack for Windows brings 2 include folders. One for the C interface and the other for the C++ interface of OpenCV. In my projects I always add both folders: `C:\OpenCV2.3\build\include\opencv` and `C:\OpenCV2.3\build\include\opencv2`. It seems that you compiled OpenCV by hand, are you sure you compiled the C++ interface? – karlphillip Nov 25 '11 at 20:21