Hey guys, if you’ve read my previous post, I installed OpenCV from prebuilt binaries. But the modules in OpenCV 3.0 are limited and if you want to use other features (which you will eventually) like the face module for face recognition you’ll need the opencv_contrib as well. I didn’t find any reliable sources showing how to install opencv_contrib so after working it out for a lot of time I finally figured it out. The opencv_contrib has many modules and you won’t need all of the features. It takes a lot of memory anyway (and time to install). In this post I’ll show you how to build OpenCV from source and add specific modules from the opencv_contrib which you need.
I
suggest you read my previous post and install OpenCV from prebuilt binaries and
try a few sample programs before you build from source so you understand the
difference. For some reason a lot of my code didn’t work before and threw an
exception when I tried debugging it, but it worked in Release mode. I wasted a
lot of time trying to figure out why it did. But after I built from source, it
worked fine in Debug and Release mode as well. If you have time, go ahead and
try it out yourself.
So let’s
begin. Download the word file and follow the instructions while watching the
video or you can just read it from here and then try it out yourself.
Step 1: Downloading and
Installing
- Download OpenCV 3.0 from: http://opencv.org/downloads.html
- You can even download the opencv-master zip from: https://github.com/Itseez/opencv
- Download opencv_contrib zip from: https://github.com/itseez/opencv_contrib
- Download and install Visual Studio 2012 from: http://www.microsoft.com/en-in/download/details.aspx?id=34673 (I suggest you read my previous post because I won’t be getting into basics in this one)
- Download Cmake from: http://www.cmake.org/files/v2.8/cmake-2.8.11.2-win32-x86.exe (You can download the latest version of cmake if you’re using recent versions of VS)
- Extract OpenCV 3.0
- If you downloaded the zip, extract it and rename the folder as opencv
- Extract opencv_contrib. Choose which modules you want.
Step 2: Building and compiling
- Go to the Opencv folder you extracted. There are two folders inside, ‘Build’ and ‘sources’. Delete everything inside the build folder. Copy everything from the ‘sources’ folder and paste it outside. Note: Opencv3 is the folder where I extracted opencv for this demonstration. You can extract it directly and it’ll save your folder as opencv by default.
- If you extracted opencv-master, create a new folder build inside it.
- Go to the opencv_contrib, from the ‘modules’ folder inside it, choose the modules you want and copy paste them in the Opencv ‘modules’ folder.
- Open Cmake. Browse source to the main folder you extracted Opencv 3.0 and browse build to the build folder inside it.
- Click on configure. It’ll open a new window to select the compiler. If you’re using VS 2012 choose ‘Visual Studio 11’
- Click on configure
- Wait for the analysis to be done.
- After it’s done. Mark appropriate fields as show below.
- After you've marked everything, click Generate.
- This will create an 'OpenCV.sln' file in your build folder. Open it with Visual Studio.
- In the solution explorer, right click on ALL_BUILD and build. This will take time. Once it's built, change the build mode to RELEASE and repeat the same step
- After that, right click on INSTALL and build it, Once it's built, again change the build mode to DEBUG and repeat the same step.
- Everything will be created in the 'install' folder.
Step 3: Creating a system
variable:
- Variable name: OPENCV_BUILD
- Value: C:\opencv3\build
- Path: C:\opencv3\build\install\x86\vc11\bin
Step 4: Configuration to test a
sample project
- Open Visual Studio and create a Console Application
- Right click on the file and click properties
- Choose Configuration Manager and add x64 platform if your system is x64 (only if your system is x64)
- At configuration field, choose all configurations
- At Configuration Properties -> C/C++ -> Additional Include Directories, add opencv include folders
Additional Include Directories:
- C:\opencv3\build\install\include
- C:\opencv3\build\install\include\opencv
- C:\opencv3\build\install\include\opencv2
Note: opencv3 is the folder in
which I extracted the files (Check your folder name and folder location)
- At Configuration Properties -> Linker -> Additional Library Directories, add opencv library folder
Additional Library Directories:
- C:\opencv\build\install\x86\vc11\lib
- At configuration field, choose Debug mode
- At Configuration Properties -> Linker-> Additional dependencies -> add dependence libraries
Additional libraries
opencv_calib3d300d.lib
opencv_core300d.lib
opencv_face300d.lib
opencv_features2d300d.lib
opencv_flann300d.lib
opencv_hal300d.lib
opencv_highgui300d.lib
opencv_imgcodecs300d.lib
opencv_imgproc300d.lib
opencv_ml300d.lib
opencv_objdetect300d.lib
opencv_photo300d.lib
opencv_shape300d.lib
opencv_stitching300d.lib
opencv_superres300d.lib
opencv_ts300d.lib
opencv_video300d.lib
opencv_videoio300d.lib
opencv_videostab300d.lib
Note: These libraries are in the
folder- C:\opencv\build\install\x86\vc11\lib
and if you've installed extra modules you'll have to include those libraries as well. This list contains the default libs and the extra 'opencv_face300d.lib' from the face module.
and if you've installed extra modules you'll have to include those libraries as well. This list contains the default libs and the extra 'opencv_face300d.lib' from the face module.
- At configuration field, choose Release mode
- At Configuration Properties -> Linker -> Additional dependencies -> add dependence libraries
Additional libraries
opencv_calib3d300.lib
opencv_core300.lib
opencv_face300.lib
opencv_features2d300.lib
opencv_flann300.lib
opencv_hal300.lib
opencv_highgui300.lib
opencv_imgcodecs300.lib
opencv_imgproc300.lib
opencv_ml300.lib
opencv_objdetect300.lib
opencv_photo300.lib
opencv_shape300.lib
opencv_stitching300.lib
opencv_superres300.lib
opencv_ts300.lib
opencv_video300.lib
opencv_videoio300.lib
opencv_videostab300.lib
opencv_core300.lib
opencv_face300.lib
opencv_features2d300.lib
opencv_flann300.lib
opencv_hal300.lib
opencv_highgui300.lib
opencv_imgcodecs300.lib
opencv_imgproc300.lib
opencv_ml300.lib
opencv_objdetect300.lib
opencv_photo300.lib
opencv_shape300.lib
opencv_stitching300.lib
opencv_superres300.lib
opencv_ts300.lib
opencv_video300.lib
opencv_videoio300.lib
opencv_videostab300.lib
Note: These libraries are in the
folder- C:\opencv\build\install\x86\vc11\lib
Include extra libraries of the modules you've installed.
Include extra libraries of the modules you've installed.
Step 4: Build and Debug
- Try this code for creating bounding circles and boxes for contours : http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html
- Copy -paste it, build it and Debug.
I hope this helps.
Files for downloading