Saturday, 8 August 2015

How to install OpenCV 3.0 and Opencv_contrib in Visual Studio 2012: From Source



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

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. 

  • 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

Note: These libraries are in the folder- C:\opencv\build\install\x86\vc11\lib
Include extra libraries of the modules you've installed. 

Step 4: Build and Debug




I hope this helps. 



Files for downloading 

28 comments:

  1. Hi, please mention that where we can find "extra libraries of the modules"?
    Is it in install folder?

    ReplyDelete
    Replies
    1. did you got any reply !
      i want the same thing !!

      Delete
    2. Hi Rida! I am Shahab From Islamabad. Need a favor about OpenCV. Please contact
      shahabhassam7@gmail.com
      thanjs

      Delete
  2. I need to use SURF and SIFT opencv methods. but my program can not find non-free library,.
    Kindly please mention that where we can find extra modules libraries.
    Its urgent. Thanks

    ReplyDelete
  3. Yes its in the install folder
    All 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

    ReplyDelete
    Replies
    1. Hi Chavan! I am Shahab From Islamabad. Need a favor about OpenCV. Please contact
      shahabhassam7@gmail.com
      thanks

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. how to rebuild the lib into .jar? can you make the tutorial? thanks

    ReplyDelete
  6. Upon attempting to execute the 'All_Build' build step, the following error occurs:

    Error LNK1181 cannot open input file 'C:\opencv\build\modules\bgsegm\opencv_bgsegm_object.dir\Debug\bgfg_gaussmix.obj' opencv_contrib_world C:\opencv\build\modules\contrib_world\LINK 1

    Should I remove the BUILD_opencv_bgsegm option from the list of cmake options and regenerate the OpenCV.sln file?

    I have tried this and there are several other modules that I need to remove...I would rather not remove them if there is an alternative solution to my problem. Thank you.

    ReplyDelete
  7. How can i build the whole libray, including the extra modules (such as face) into a .jar file that can be used in Java ?
    Thank you !

    ReplyDelete
  8. Hello I get this error : error LNK1107:invlaid or corrupt file: cannot read at xxxxxx, and I did everything as you said.

    Can you help me?

    ReplyDelete
  9. Anyone knows how to do this with Opencv 3.1.0 and MSV 2015? Thank you in advance! ;)

    ReplyDelete
    Replies
    1. The above mentioned steps works with MSVS 2015 too!!

      Delete
  10. Mann would you please upload all dll files and library files, my cmake cannot able to create xfeaure2d.dll and othes too.

    ReplyDelete
  11. CMake Error: The source directory "C:/opencv300" does not appear to contain CMakeLists.txt.
    Specify --help for usage, or press the help button on the CMake GUI.

    ReplyDelete
  12. 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.
    Error 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...

    ReplyDelete
  13. 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.

    ReplyDelete
  14. I do all but with visual studio 2013 every thing be ok but my install folder have not bin folder

    ReplyDelete
    Replies
    1. There 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.

      Delete
  15. Thank you very much! I works great!

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. You 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.

    ReplyDelete
    Replies
    1. can 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.

      Delete
  18. Thank you very much!It helps me.

    ReplyDelete
  19. Hi. I've been following your tutorial since morning and I'm trying to use SURF.
    After 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

    ReplyDelete
  20. CMake Error at C:/opencv/sources/cmake/OpenCVUtils.cmake:1005 (file):
    file 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

    ReplyDelete
  21. hello boss,
    I want to use the VIZ library and it is not in the install folder... please advise how can i use it??

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete