OpenCV – Free Image Tools :It’s Cool!!

Reference function

Japanese cookbook 

OpenCV is a computer vision library.

 

Set Up on Mac OS X 10.6.8 (Snow Leopard)

0.Cmake install

go to website

download this file

install cmake

Referenced from official website as shown below:

1.OpenCV Source File can be downloaded here

 

スクリーンショット(2013-01-12 20.31.53)

 

2.Expand opencv-master.zip

スクリーンショット(2013-01-12 20.52.01)

 

Building OpenCV from Source Using CMake, Using Terminal

1.Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated Makefiles, project files as well the object files and output binaries. : mkdir release

2.Enter the <cmake_binary_dir> and type

cmake [<some optional parameters>] <path to the OpenCV source directory>

For example:

cd ~/opencv

mkdir release

cd release

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

3. Enter the created temporary directory (<cmake_binary_dir>) and proceed with:

make

スクリーンショット(2013-01-12 20.54.26)

sudo make install

password is required

Using

OpenCV with gcc and CMake

Create a program using OpenCV

Let’s use a simple program such as DisplayImage.cpp shown below.

#include <cv.h>

#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )

{

Mat image;

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;

}

Create a CMake file

Now you have to create your CMakeLists.txt file.

It should look like this:

project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Generate the executable

This part is easy, just proceed as with any other project using CMake:

cd <DisplayImage_directory>
cmake .
make

Result

By now you should have an executable (called DisplayImage in this case).

You just have to run it giving an image location as an argument, i.e.:

Create a CMake file

Now you have to create your CMakeLists.txt file. It should look like this:

Generate the executable

This part is easy, just proceed as with any other project using CMake:

Result

By now you should have an executable (called DisplayImage in this case). You just have to run it giving an image location as an argument, i.e.:

project( DisplayImage ) find_package( OpenCV REQUIRED ) add_executable( DisplayImage DisplayImage ) target_link_libraries( DisplayImage ${OpenCV_LIBS} )

 

cd <DisplayImage_directory>

cmake .

make

./DisplayImage ait_logo.jpg

 

You should get a nice window as the one shown below:

 

スクリーンショット(2013-01-07 10.51.36)

 

 

 

Mac OS X install ref2

Tutorials

 

 

Referenced from official page

Use OpenCV in your source code!

The recommended way of using OpenCV in your code is to use CMake with your code.

Solution

There are many ways to include OpenCV in your projects. I describe my way here which has the following advantages:

  • No need to change anything when porting between Linux and Windows.
  • Can easily be combined with other tools by CMake. (I use QT, ITK and VTK.)

Setup your workstation:

  • Install GCC on linux, or MinGW on Windows http://www.mingw.org/, Visual Studio for Windows.
  • Install CMake http://www.cmake.org, and learn how to use it (takes 15 minutes).
  • Download OpenCV 2.0.0a source code. Unpack it in a folder, I will call this folder MYHOME/src/OpenCV from now on.
  • Start CMake. Choose the source folder as MYHOME/src/OpenCV . Choose a folder for your build. I will use MYHOME/build/OpenCV from now on.
  • Click Configure. Choose the options that you want (such as building Python wrapper, Octave wrapper, …).
  • Keep clicking configure, resolve conflicts until you can click on Generate.
  • Click on Generate.
  • Navigate to MYHOME/build/OpenCV, there should be a solution/make file there.
  • Build OpenCV (in Windows, this means open the OpenCV.sln file and build. In other Makefile-based systems, this means type make -j8).

Test your workstation by compiling a Hello World program

You probably know CMake by now, but I will provide a full hello world program here. You have to make two files: helloworld.cxx is your program code, and CMakeLists.txt defines your configuration for compilation.

helloworld.cxx looks like this:

 

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

int main ( int argc, char **argv )
{
  cvNamedWindow( "My Window", 1 );
  IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
  CvFont font;
  double hScale = 1.0;
  double vScale = 1.0;
  int lineWidth = 1;
  cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
              hScale, vScale, 0, lineWidth );
  cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,
             cvScalar( 255, 255, 0 ) );
  cvShowImage( "My Window", img );
  cvWaitKey();
  return 0;
}

CMakeLists.txt looks like this:

PROJECT( helloworld_proj )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( helloworld helloworld.cxx )
TARGET_LINK_LIBRARIES( helloworld ${OpenCV_LIBS} )

It is assumed that you’ll put the above two files (helloworld.cxx and CMakeLists.txt) in a folder. We will call this folder MYHOME/src/hw.

  1. Start CMake and choose your source folder to be MYHOME/src/helloworld
  2. Choose your build folder. I will choose MYHOME/build/helloworld
  3. Click Configure
  4. You will now have a variable called OpenCV_DIR. This variable is most likely pre-set to C:\OpenCV2.0.0 if you are on Windows. It may be undefined on other systems.
  5. Make sure you set the above variable to your BUILD folder of OpenCV, i.e. MYHOME/build/OpenCV. Since you built OpenCV with CMake, it remembers the path and all necessary stuff so that choosing the build folder will automatically pick up the necessary library path, include the correct include directory, set the correct list of library. In other words, the magic of CMake happens.
  6. Now you can click Generate to create the solution/makefile of your choice.
  7. Open this solution/makefile and build it.

If you get an executable helloworld program, you are ready to move on and have fun with OpenCV!