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
Hi, please mention that where we can find "extra libraries of the modules"?
ReplyDeleteIs it in install folder?
did you got any reply !
Deletei want the same thing !!
Hi Rida! I am Shahab From Islamabad. Need a favor about OpenCV. Please contact
Deleteshahabhassam7@gmail.com
thanjs
I need to use SURF and SIFT opencv methods. but my program can not find non-free library,.
ReplyDeleteKindly please mention that where we can find extra modules libraries.
Its urgent. Thanks
Yes its in the install folder
ReplyDeleteAll you've got to do is copy paste the ' xfeatures2d' folder from the opencv_contrib's module into the main opencv's modules folder and build it. Once thats done you'll find all the libraries in the 'install' folder
It looks something like this"
C:\opencv\build\install\x86\vc11\lib
Hi Chavan! I am Shahab From Islamabad. Need a favor about OpenCV. Please contact
Deleteshahabhassam7@gmail.com
thanks
This comment has been removed by the author.
ReplyDeletehow to rebuild the lib into .jar? can you make the tutorial? thanks
ReplyDeleteHow can i build the whole libray, including the extra modules (such as face) into a .jar file that can be used in Java ?
ReplyDeleteThank you !
Hello I get this error : error LNK1107:invlaid or corrupt file: cannot read at xxxxxx, and I did everything as you said.
ReplyDeleteCan you help me?
Anyone knows how to do this with Opencv 3.1.0 and MSV 2015? Thank you in advance! ;)
ReplyDeleteThe above mentioned steps works with MSVS 2015 too!!
DeleteMann would you please upload all dll files and library files, my cmake cannot able to create xfeaure2d.dll and othes too.
ReplyDeleteCMake Error: The source directory "C:/opencv300" does not appear to contain CMakeLists.txt.
ReplyDeleteSpecify --help for usage, or press the help button on the CMake GUI.
I tried to build "xfeatures2d" for SIFT feature detection. But when I build it using 'ALL_BUILD-->build', I got lot of error messages and build is failed.
ReplyDeleteError 1 error C1083: Cannot open include file: 'opencv2/core/hal/hal.hpp': No such file or directory C:\opencv3\opencv\modules\xfeatures2d\src\sift.cpp 108 1 opencv_xfeatures2d
Error 2 error LNK1104: cannot open file '..\..\lib\Debug\opencv_xfeatures2d300d.lib' C:\opencv3\opencv\build\modules\xfeatures2d\LINK (sample) bagofwords_classification
Error 9 error LNK1104: cannot open file '..\..\lib\Debug\opencv_stitching300d.lib' C:\opencv3\opencv\build\samples\cpp\LINK (EXAMPLE) cout_mat
Can you help me to correct them? please...
Mister audhoot chavan hello, first of all thank you a lot for all your posts i think that you are amazing. Could you please make a video on how installing SIFT and SURF modules from Opencv_Contrib and opencv 3 on python 3 in windows. I spent a lot of time trying to get it work but without success PLEASE A HELP.
ReplyDeleteI do all but with visual studio 2013 every thing be ok but my install folder have not bin folder
ReplyDeleteThere is a bin folder in your build folder : there are opencv_world310d.dll and opencv_world310.dll in the respective folders. Inside the Install folder is the include folder that you need for the includes.
DeleteThank you very much! I works great!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYou should NEVER copy modules from opencv_contrib to opencv. You should rather add the opencv_contrib module path to your opencv cmake configuration. The cmake variable to do that is OPENCV_EXTRA_MODULES_PATH. If this is set to the path opencv_contrib/modules, cmake will automatically add all contrib modules to your current opencv configuration. Then you can quickly turn on and off any contrib module you desire for your opencv build.
ReplyDeletecan u tell me exactly how you do it, i am trying the surf and sift now, but the error shows the xfeatures2d not found, and this is my email bbraptor.lee@gmail.com if u wanna help.
DeleteThank you very much!It helps me.
ReplyDeleteHi. I've been following your tutorial since morning and I'm trying to use SURF.
ReplyDeleteAfter following your full tutorial, it is giving me error on this line
Ptr detector = SURF::create(minHessian);
and the error is :
Severity Code Description Project File Suppression State Line
Error LNK2019 unresolved external symbol "public: static struct cv::Ptr __cdecl cv::xfeatures2d::SURF::create(double,int,int,bool,bool)" (?create@SURF@xfeatures2d@cv@@SA?AU?$Ptr@VSURF@xfeatures2d@cv@@@3@NHH_N0@Z) referenced in function _main OpencvSURF C:\Users\Hassam\documents\visual studio 2015\Projects\OpencvSURF\OpencvSURF\Source.obj 1
CMake Error at C:/opencv/sources/cmake/OpenCVUtils.cmake:1005 (file):
ReplyDeletefile MD5 failed to read file "C:/opencv/build/3rdparty/protobuf/":
Permission denied
Call Stack (most recent call first):
C:/opencv/opencv_contrib/modules/dnn/cmake/OpenCVFindLibProtobuf.cmake:32 (ocv_download)
C:/opencv/opencv_contrib/modules/dnn/CMakeLists.txt:5 (include)
I get this error. What to do
hello boss,
ReplyDeleteI want to use the VIZ library and it is not in the install folder... please advise how can i use it??
This comment has been removed by the author.
ReplyDelete