OpenCV is an open source image processing library which have copious functions to help accelerate the implementation. Moreover, OpenCV supports not only C but also C++ and python which makes it easier for the programmer to migrate from one platform to another. In this tutorial series, we will be dealing with the C and C++ implementation of OpenCV code. This means that we will try our maximum to include the OpenCV and OpenCV2 implementation of the same program. Moreover a beginner level approach will always be taken throughout the tutorials.
Setting Up OpenCV in Visual Studio2012Before you startThis guide assumes that you have already installed Microsoft Visual Studio 2012 in your computer. The same method is applicable till 2010 also. If you haven't installed Visual Studio yet download it from the following link
Microsoft Visual Studio 2012. Download OpenCVAfter properly setting up Visual Studio in your computer, The next step is to download OpenCV from the sourceforge site. Its always recommended to download the latest stable release. The beta versions may be under development and so you may download those versions only under your risk.
Now follow the link given below to download openCV for Windows. Download OpenCV Extract OpenCVNow run the EXE file that you just downloaded and extract OpenCV. Personally, I recommend that you extract it to the C drive.
The extraction will take quite a while to complete. Once completed, you may go to the installation directory and make sure that it looks something like this.
Setting the OpenCV PathAdd the following environment path variables. To add environment path variables follow the below mentioned steps:
1) Right click Computer and take properties. 2) Navigate to advanced system settings and open environment variables. 3) Edit the system variable named Path and append the following to the line: C:\opencv\build\x86\vc11\bin (for 86 bit visual studio) C:\opencv\build\x64\vc11\bin (for 64 bit visual studio) Always keep in mind that the 64 bit here doesn't mean the version of the installed visual studio. Instead, you have to add the 64 bit system variable only if you are developing an application for a 64 bit system. for typical Win32 console projects in Visual Studio you may add the x86 path. Now add a new system variable with name OPENCV_DIR and path the same as the one to which you extracted OpenCV as shown below.
Configure Visual StudioOnce the system path and new system variable has been added, we can continue to configuring OpenCV in Visual Studio. To get started, open a new project in VS2012 and select Win32 Console Application in the proceeding step. Name the project as you wish and you can select your workspace as you wish. Now click some Next make sure you are creating an empty project. i.e, tick empty project in the wizard as shown below.
Once you have created the new project, go to the Property Manager and open the sheet named Microsoft.Cpp.Win32.user.
Now expand the property named the same as your project and double click on release.
Go to Linker -> General -> Additional Library Directories. Add this path over there: $(OPENCV_DIR)\lib;Next go to Linker -> Input -> Additional Dependencies and add the following: NOTE: Here "246"is the installed version of opencv. for eg., we use"246" in the case of OpenCV 2.4.6Also opencv_calib3d246d.lib opencv_contrib246d.lib opencv_core246d.lib opencv_features2d246d.lib opencv_flann246d.lib opencv_gpu246d.lib opencv_haartraining_engined.lib opencv_highgui246d.lib opencv_imgproc246d.lib opencv_legacy246d.lib opencv_ml246d.lib opencv_nonfree246d.lib opencv_objdetect246d.lib opencv_ocl246d.lib opencv_photo246d.lib opencv_stitching246d.lib opencv_superres246d.lib opencv_ts246d.lib opencv_video246d.lib opencv_videostab246d.lib Now go to C/C++ -> General -> Additional Include Directories and add the following path: C:\OpenCV\build\include\opencv;C:\OpenCV\opencv\build\include;
|
Setting Up OpenCV in CodeBlocksBefore you startThis tutorial is meant for the Linux users who wants too configure OpenCV in Codeblocks platform. I assume that you have already installed codeblocks from the Software Centre or by extracting and making using the Makefile. You can download Code Blocks from the following link
CodeBlocks Download OpenCVAfter properly setting up Visual Studio in your computer, The next step is to download OpenCV from the sourceforge site. Its always recommended to download the latest stable release. The beta versions may be under development and so you may download those versions only under your risk.
Now follow the link given below to download openCV for Windows. Download OpenCV Extract OpenCVAfter having downloaded the tarball file(tar.gz file), untar it. I am making an assumption that the OpenCV tarball name is opencv-2.4.6.tar.gz, and that my tarball is in the Desktop.In order to untar the file open terminal and navigate to the Desktop and use the tar command
cd ~/Desktop tar -zxvf opencv-2.4.6.tar.gz cd Opencv-2.4.6 The extraction will take quite a while to complete. Once completed, you may go to the installation directory and make sure that it looks something like this. For that type ls in the terminal. You must see something like this.
Before making opencv, we have to ensure that all the necessary libraries and softwares have been installed. For that run the following command in the terminal. sudo apt-get install build-essential cmake pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev libpngwriter0-dev libpngwriter0c2 zlib1g-dbg zlib1g zlib1g-dev libjasper-dev libjasper-runtime libjasper1 pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev python-numpy remove any library or software which is creating problem in the above step.Now recheck whether you are in the Opencv-2.4.6 folder.Now we have to create a folder named "release". mkdir release
cd release
Now build the OpenCV using the following command. cmake -D CMAKE_BUILD_ -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON .. Now make and install OpenCV using the following commands. make sudo make install This step will take quite a long time to complete. Its better to ensure that you keep your laptop plugged while making OpenCV. Now we have to add the library path of OpenCV to linux. Inorder to do that follow these commands: cd /etc/ld.so.conf.d
touch opencv.conf
sudo gedit opencv.conf
Add the following line to the file, save and close it.
/usr/local/lib Now run the following command to complete the installation.
sudo ldconfig -v export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH Inorder to check whether the opencv has been properly installed, you can type in the following in the terminal:
pkg-config opencv --libs pkg-config opencv --cflags You must get an output looking sompething like this:
Configure Code BlocksOnce you have ensured that the OpenCV has been properly installed in your system, the next step is to integrate it into an IDE. In this tutorial I am using Code Blocks but it also works for any other IDE. I prefer IDE because the auto complete option makes coding simplified.
Now open CodeBlocks. I am using CodeBlocks 12.11.
Now go to settings -->compiler Under the compiler settings tab select the other options tab and copy the following command into the pane.Make sure that you are not using the apostrophe but the symbol just next to the number 1 on your keyboard. `pkg-config opencv --cflags` Now open the Linker Settings Tab and copy the following command int the other linker options pane:
`pkg-config opencv --libs` Now open the search directories tab and add the following locations :
/usr/local/include/ Click OK to save the settings.
The last step is to enable autocomplete feature of CodeBlocks. Auto complete is a helpful feature of Code Blocks, as we dont have to remember the numerous functions and its parameters when coding in OpenCV. Now Inorder to enable auto complete open the editor options from the settings menu. and make the selections as shown below: |
Test your installation
Now Inorder to test whether the installation was proper, you may copy the following code to your project either in Visual Studio in the case of windows or CodeBlocks in the case of Linux. Build and run the code.
#include <cv.h> #include <highgui.h> int main() { IplImage *im; CvCapture *cam = cvCaptureFromCAM(0); while(1) { im = cvQueryFrame(cam); cvShowImage("image", im); if(cvWaitKey(10) == 27) break; } }