Skip to content

Instantly share code, notes, and snippets.

@SSARCandy
Last active April 15, 2024 02:25
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save SSARCandy/fc960d8905330ac695e71e3f3807ce3d to your computer and use it in GitHub Desktop.
Save SSARCandy/fc960d8905330ac695e71e3f3807ce3d to your computer and use it in GitHub Desktop.
Setting up OpenCV(+extra modules) with Cmake step by step tutorial

Setting up OpenCV with Cmake GUI

  1. Download OpenCV and Cmake
  2. Build opencv with cmake image
  • Press configure, choose visual studio 2015, finish
  • Then press generate
  1. Open OpenCV.sln under build/
  2. Build it using Debug, Release
    image
  • right click > build
  • switch to Release mode and build again
  1. [Windows] Setting up environment variable
  • add <opencv>/bin into PATH image
  • add new env named OpenCV_DIR, value as <opencv>/build
  • it may need logout to apply setting, you can check it by echo %PATH%, echo %OpenCV_DIR%

Build with EXTRA MODULES

  1. In step 2. Build opencv with cmake, press configure
  2. Set up OPENCV_EXTRA_MODULES_PATH to proper path(<opencv_contrib>/modules) image
  3. Press configure again, then generate

To see more details instructions, see opencv_contrib README


Travis.yml example

language:
  - cpp

compiler:
  - gcc
  
before_install:
  - sudo apt-get update

install:
  # OpenCV dependencies - Details available at: http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_install/linux_install.html
  - sudo apt-get install -y build-essential
  - sudo apt-get install -y cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  - sudo apt-get install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

  # Download v3.1.0 .zip file and extract.
  - curl -sL https://github.com/Itseez/opencv/archive/3.1.0.zip > opencv.zip
  - unzip opencv.zip
  
  # Download EXTRA MODULES and extract.
  - curl -sL https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip > opencv_contrib.zip
  - unzip opencv_contrib.zip
  
  # Create a new 'build' folder.
  - cd opencv-3.1.0
  - mkdir build
  - cd build
  
  # Set build instructions for Ubuntu distro.
  - cmake -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.1.0/modules CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
  
  # Run 'make' with four threads.
  - make -j5
  
  # Install to OS.
  - sudo make install
  
  # Add configuration to OpenCV to tell it where the library files are located on the file system (/usr/local/lib)
  - sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
  
  - sudo ldconfig
  - echo "OpenCV installed."
  
  # We need to return to the repo "root" folder, so we can then 'cd' into the C++ project folder.
  - cd ../../

script:
  - cmake CMakeLists.txt
  - make
  - ./a.out
  
@mosharaf13
Copy link

followd all the steps above.now after creating a new project on my vs17 how can i link my "opencv" with that project?

@AyOne
Copy link

AyOne commented Dec 16, 2017

Same question as mosharaf13

@momonala
Copy link

Same question as Ay00One

@TILhub
Copy link

TILhub commented Mar 20, 2018

Worst tutorial ever

@MrfksIv
Copy link

MrfksIv commented Mar 31, 2018

@TILhub @momonala after a bit of digging in the microsoft docs I found how to include opencv to a new project.
In the solution explorer, right-click on Solution 'OpenCV' > Add > New Project...

For more info check out this link: https://docs.microsoft.com/en-us/visualstudio/ide/quickstart-projects-solutions

@asadsultanawan
Copy link

This tutorial misses some links. I used the following steps.
Using CMake GUI:

  1. First, download opencv and opencv-contrib and place them into a folder.
  2. Open CMake Gui and select sources folder of opencv in sources path and build in the other.
  3. Now configure for e.g. Visual Studio 2015 and select x64 (or win32 or x86 as per your need) in the second option.
    CheckPoint: If you have a newer version than VS2015 and you don't have build tools for C++ for VS2015, you will get error. Make sure you have build tools for the version of VC you are building opencv.
  4. After configure is done. Set up OPENCV_EXTRA_MODULES_PATH by browsing to opencv-contrib/modules folder.
  5. Configure again and click generate.
  6. Now open opencv folder and click opencv.sln in VS2015 and build INSTALL project in debug as well as release.

Now your opencv directory will have an install folder in your opencv-dir/build/.

Use OPENCV in your VS project

  1. Create a console application in C++.
  2. Open project properties.
  3. Go to VC++ directories and enter opencv-dir/build/install/include in include directories, opencv-dir/build/install/x64/vc14/lib in library directories and opencv-dir/build/install/x64/vc14/bin in your PATH variable in environment variables.
    Checkpoint: For opencv_world.lib file copy from opencv-dir/build/x64/vc14/lib and paste in opencv-dir/build/install/x64/vc14/lib.
  4. Now go to linker and input and enter worldlib and any other needed library for your project.
  5. You are good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment