Ask Your Question

Revision history [back]

Potential Error in Tutorial

I am quite new to OpenCV. I have been using C for 20 years, but am very new to C++ as the object-oriented approach has always seemed awkward, but I'm getting there.

From the tutorials in:

http://docs.opencv.org/2.4/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html#table-of-content-introduction

I decided to work on the one at:

http://docs.opencv.org/2.4/doc/tutorials/introduction/load_save_image/load_save_image.html#load-save-image

but it wouldn't compile. I'm nowhere familiar enough with these libraries, or C++, to determine what went wrong. But the disheartening reality is that I only cut-n-pasted the source code and adapted the CMakeLists.txt from a prior tutorial.

So here is what I have. I'll start with the CMakeLists.txt that is required.

cmake_minimum_required(VERSION 2.8)
project( Change )
find_package( OpenCV REQUIRED )
add_executable( Change Change.cpp )
target_link_libraries( Change ${OpenCV_LIBS} )

This is the code, I called is Change.cpp.

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

using namespace cv;

int main( int argc, char** argv )
{
 char* imageName = argv[1];

 Mat image;
 image = imread( imageName, 1 );

 if( argc != 2 || !image.data )
 {
   printf( " No image data \n " );
   return -1;
 }

 Mat gray_image;
 cvtColor( image, gray_image, CV_BGR2GRAY );

 imwrite( "../../images/Gray_Image.jpg", gray_image );

 namedWindow( imageName, CV_WINDOW_AUTOSIZE );
 namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );

 imshow( imageName, image );
 imshow( "Gray image", gray_image );

 waitKey(0);

 return 0;
}

When I execute make to initiate the compilation, I get a number of errors:

$ make
Scanning dependencies of target Change
[100%] Building CXX object CMakeFiles/Change.dir/Change.cpp.o
/home/legg/code/image/Change/Change.cpp: In function ‘int main(int, char**)’:
/home/legg/code/image/Change/Change.cpp:11:31: error: ‘imread’ was not declared in this scope
  image = imread( imageName, 1 );
                               ^
/home/legg/code/image/Change/Change.cpp:20:43: error: ‘cvtColor’ was not declared in this scope
  cvtColor( image, gray_image, CV_BGR2GRAY );
                                           ^
/home/legg/code/image/Change/Change.cpp:22:53: error: ‘imwrite’ was not declared in this scope
  imwrite( "../../images/Gray_Image.jpg", gray_image );
                                                     ^
/home/legg/code/image/Change/Change.cpp:24:45: error: ‘namedWindow’ was not declared in this scope
  namedWindow( imageName, CV_WINDOW_AUTOSIZE );
                                             ^
/home/legg/code/image/Change/Change.cpp:27:27: error: ‘imshow’ was not declared in this scope
  imshow( imageName, image );
                           ^
/home/legg/code/image/Change/Change.cpp:30:11: error: ‘waitKey’ was not declared in this scope
  waitKey(0);
           ^
make[2]: *** [CMakeFiles/Change.dir/Change.cpp.o] Error 1
make[1]: *** [CMakeFiles/Change.dir/all] Error 2
make: *** [all] Error 2

It appears that the imread() function isn't found (and the following lines are likely derived from the same issue). I hope that I'm not naive in assuming the source code is correct, but it appears that a header file is missing. I don't have enough experience to formulate a scientific solution to this problem, but instead I made a guess by prefixing:

#include <stdio.h>
#include <opencv2/opencv.hpp>

which is from a separate tutorial and it worked according to spec. So in a way, I resolved this myself, but not only through guessing, not understanding. That's not my preferred path to mastery. I would be very interested in other's feedback on this.