Beginning OpenCV


I assume that you have installed OpenCV on your Linux machine. If you are having any problems installing OpenCV 2.3.1 you can view my previous post or  http://www.marcolancini.it/other/opencv_install_231.php

I would recommend you to go through OpenCV documentation.

C/C++
Python

C

Load images in C

#include
#include<opencv2/highgui/highgui.hpp>

int main()
{
    IplImage* img = cvLoadImage("/home/jay/Pictures/python.jpg",CV_LOAD_IMAGE_COLOR);
    cvNamedWindow("opencvtest",CV_WINDOW_AUTOSIZE);
    cvShowImage("opencvtest",img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    return 0;
}

To compile C program,
Let’s assume the file is opencvtest.c


$ gcc -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.c .c` opencvtest.c `pkg-config --libs opencv`
$ ./opencvtest

C++

Load images in C++

#include<opencv2/highgui/highgui.hpp>
using namespace cv;

int main()
{

    Mat img = imread("/home/jay/Pictures/python.jpg",CV_LOAD_IMAGE_COLOR);
    imshow("opencvtest",img);
    waitKey(0);

    return 0;
}

To compile C++ program,
Let’s assume the file is opencvtest.cpp


$ g++ -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.cpp .cpp` opencvtest.cpp `pkg-config --libs opencv`
$ ./opencvtest

Note: Always include OpenCV header files in C and C++ as

#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"

Note: Never compile a file in the following manner.
$ gcc -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.c` opencvtest.c `pkg-config --libs opencv`
It will make your .c file to output file.

To avoid any problems like this, I have made a bash script to compile opencv programs.
Making a Bash Script to Compile OpenCV:

It’s kind of boring typing all this stuff. So, I created a bash file to compile OpenCV programs.
Name it .compile_opencv.sh and keep it in your home directory.

echo "compiling $1"
if [[ $1 == *.c ]]
then
    gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`;
elif [[ $1 == *.cpp ]]
then
    g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;
else
    echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"

Add an alias in .bashrc or .bash_aliases

alias opencv="~/.compile_opencv.sh"


$ opencv opencvtest.c
$ ./opencvtest

Python

Loading images in Python

from cv2.cv import *

img = LoadImage("/home/jay/Pictures/python.jpg")
NamedWindow("opencv")
ShowImage("opencv",img)
WaitKey(0)

$ python filename.py

P.S. Finals getting over soon. Excited to go home.

14 thoughts on “Beginning OpenCV

  1. i did the .c sample and when i try to run this part:
    $ gcc -ggdb `pkg-config –cflags opencv` -o `/home/yessy/primer.c` primer.c `pkg-config –libs openc
    it it says…”bash: /home/yessy/primer.c: permission denied”
    ” /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start’:
    (.text+0x18): undefined reference to `main’ 😦 i dont know what i am doing wrong. any idea???

  2. I think there’s some problem with permissions. either give this file proper permissions are compile it using root.
    let’s assume your filename is primer.c

    $ gcc -ggdb `pkg-config --cflags opencv` -o `basename primer.c .c` primer.c `pkg-config --libs opencv`

    Notice the space between primer.c and .c in basename
    you can also do

    $ gcc -ggdb `pkg-config --cflags opencv` -o `basename primer .c` primer.c `pkg-config --libs opencv`

    but don’t do

    $ gcc -ggdb `pkg-config --cflags opencv` -o `basename primer.c` primer.c `pkg-config --libs opencv`

    There has to be space between “primer” and “.c” or “primer.c” and “.c”. If you do “primer.c”, your output file will be primer.c and it will have complications.

  3. Pingback: Install OpenCV 2.4 in Ubuntu 12.04 Precise Pangolin « Paranoid Android

  4. Pingback: Get data from Mat / cv::Mat in OpenCV « Paranoid Android

  5. Hello Jay,

    I am using opencv with codeblocks on Linux Mint 14, do you know how to get the intellisense for OpenCV to work on codeblocks. I am learning OpenCV and new to linux, intellisense is of great use to experiment and know more about the api apart for reading the dodumentations.

    Thanks in advance!

  6. Pingback: Point Cloud Library – Install and Configure – Ubuntu 12.04 | Paranoid Android

  7. Finally after 3 days of false starts, I found your script for installing OpenCV. Nothing could be simpler, thanks.

    I am having trouble compiling the example. Opencv has put highui.hpp under a tonne of directories and highgui calls other files which are not where they are expected. I’m a linux newbie, how do I fix this so I can compile. I am using QT on Ubuntu and OpenCV-2.4.5.

    Thanks.

  8. Hello again,

    I followed all the steps till compiling the program. And now when I run “./opencvtest” this is the output:
    OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/uni/src/OpenCV-2.4.2/modules/core/src/array.cpp, line 2482
    terminate called after throwing an instance of ‘cv::Exception’
    what(): /home/uni/src/OpenCV-2.4.2/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat

    Aborted (core dumped)

    —————
    Any help is appreciated. Thanks

  9. Hello,
    I followed all these steps. This is the error I get. What to do?

    compiling opencvtest.cpp
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found
    opencvtest.cpp:1:38: fatal error: opencv2/highgui/highgui.hpp: No such file or directory
    compilation terminated.
    /home/mollievx/.compile_opencv.sh: line 11: unexpected EOF while looking for matching `”‘
    /home/mollievx/.compile_opencv.sh: line 14: syntax error: unexpected end of file

  10. I have just read images in opencv and want to reconstruct the 3D image from thoses images… i want to use PCL for this purpose… how i can do that.. can you help me in this regard… Thanks in advance…