OpenCV – get up and running quickly (using open source C compiler)

opencv_codeblocks_mingw

OpenCV is an excellent open-source computer vision library. How to use it quickly on a Windows operating system out-of-the-box?

1. Download ‘Code::Blocks‘, an open-source C/C++ IDE that comes with the open-source ‘MinGW‘ C/C++ compiler:
Visit ‘http://www.codeblocks.org‘, and download ‘codeblocks-13.12mingw-setup.exe‘ (Code::Blocks v13.12 with integrated MinGW v4.7.1, 32 bit compiler).

2. Install ‘Code::Blocks‘ IDE.

3. Download OpenCV library and run it  (it will extract to ‘c:\opencv’):
Visit ‘http://opencv.org/‘, and download ‘OpenCV-2.3.1-win-superpack.exe‘.

4. Download and install ‘CMake 2.8‘.

5. Run ‘cmake-gui.exe’ to generate the OpenCV Makefiles. Choose:

 'Where is the source code' => C:\opencv
 'Where to build the binaries' => C:\opencv/build/x86/mingw

Finally, press ‘Generate’ to generate the Makefiles.

6. Compile OpenCV:
a) Add ‘C:\Programs (x86)\CodeBlocks\MinGW\bin’ to your Windows operating system PATH.
b) Open a Windows command prompt, and compile the OpenCV library:

 cd C:\opencv\build\x86\mingw
 mingw32-make
 mingw32-make install

7. Run CodeBlocks, choose ‘File->New->From template…->Console application’.

8. Right-click Workspace->Your project and choose ‘Build options…’, and configure the project for OpenCV:

Linker settings: Links libraries: Choose all OpenCV DLLs

 c:\opencv\build\x86\mingw\lib\libopencv_calib3d231.dll.a
 c:\opencv\build\x86\mingw\lib\libopencv_contrib231.dll.a
 c:\opencv\build\x86\mingw\lib\libopencv_core231.dll.a
 etc.

Search directories (Compiler): Choose ‘c:\opencv\build\include’

9. Enter some OpenCV example code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

VideoCapture cap(-1); // open the default camera
Mat frame;
Mat img;

int main()
{
  if (!cap.isOpened()){ // check if camera is opened
    printf("cannot open camera");
    return -1;
  }
  printf("press ESC to exit\n");
  namedWindow("test_opencv", CV_WINDOW_AUTOSIZE ); // create window
  for(;;){
    cap >> frame; // capture camera frame
    cvtColor(frame, img, CV_BGR2GRAY); // convert to grayscale
    GaussianBlur(img, img, Size(7,7), 1.5, 1.5); // apply gaussian filter
    imshow("filtered", img); // show filtered image

    int key = waitKey(30); // read key
    if (key >= 0) {
        printf("key=%d\n", key);
        switch (key) {
            case 27: return 0; // exit app
        }
    }
  }
  return 0;
}

10. Right-click Workspace->Your project and choose ‘Properties…’ -> ‘Build tagets’, and set ‘Execution working dir:’ to ‘c\opencv\build\x86\mingw\bin’ (so it will find the OpenCV DLLs when your OpenCV application starts).

11. Finally, press ‘Build & run’ to start your first OpenCV application!

12. Additionally, building ‘irrlicht library’: Compile CodeBlocks project ‘irrlicht/source/Irrlicht-gcc.cbp’.

13. Additionally, building ‘boost library’: Run ‘bootstrap.bat mingw’, and ‘b2 toolset=gcc’.

Leave a Reply

Your email address will not be published. Required fields are marked *

IMPORTANT! To be able to proceed, please enter the magic word 'anaegd' so we know hat you are a human)

Enter the magic word:
Please leave these two fields as-is:

A blog on projects with robotics, computer vision, 3D printing, microcontrollers, car diagnostics, localization & mapping, digital filters, LiDAR and more