21

I want to use OpenCV in Visual Studio 2017 to do c++ program.

To do it, I followed Setting Up OpenCV in Visual Studio video. In that, he said vc14 should be set as environmental variables for VS2015. There is no vc16 for VS2017. So how do I do it for VS2017? And are there any other video instruction changes to be made if I am to use VS2017?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59

3 Answers3

41

Since the Doc from official OpenCV tutorial is outdated, I will suggest:

  1. get VS-2017 with full compiler like:

    enter image description here

  2. get the newest CMake (uninstall older versions)

  3. get from github the sourceCode here and here and place those in C:/openCV folder, create a BUILD folder there too

  4. open Cmake, select the opencv master folder... in the build write the build directory like:

enter image description here

  1. click configure, select visual studio 15 2017 and leave selected "use default native compilers", then press "finish"...

enter image description here

6.1 now you should see cmake doing its job :)

enter image description here

  1. once configuration is done set the opencv extra modules path and click configure again, and when is done click generate! :)

    enter image description here

  2. when generation is done click "open project", this will open a huge project in visual studio named "opencv", now you can close cmake, we dont need that anymore.

  3. now, when the solution is ready, be sure you are in "debug" conf and do right click on "ALL_BUILD" and then "build" (that can take a while) once build succeed change to "release" config and do right click on "ALL_BUILD" and then "build" again.

  4. now,be sure you are in "debug" conf and do right click on "INSTALL" and then "build" (that can take a while) once build succeed change to "release" config and do right click on "INSTALL" and then "build" again.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I just had too many bugs from 3rd party libs and tried to make with PCL (Point Clouds Library). So got back to Visual Studio 2015, since i am beggining to learn. https://stackoverflow.com/questions/43902133/use-cmake-with-pcl-and-opencv – danieltakeshi Jun 21 '17 at 14:01
  • And got this comment http://unanancyowen.com/en/pcl18/#comment-1221 Did you choose “Visual Studio 14 2015 Win64”? The latest (3.2.0) pre-built OpenCV package includes only libraries for Visual Studio 2015 x64. Therefore, If you want to use only pre-built packages of PCL and OpenCV, You should be use Visual Studio 2015 and x64 target for development environment. If you want to more information, I recommended that you post to User Q&A forum of OpenCV. – danieltakeshi Jun 21 '17 at 14:05
  • I get an error when executing cmake..CMake Error: The source directory "C:/Program Files/opencv" does not appear to contain CMakeLists.txt. – SarahData Jul 28 '17 at 18:29
  • Hi @Sarah... please look inside of that folder... you must have the cake file there – ΦXocę 웃 Пepeúpa ツ Jul 28 '17 at 18:42
  • in both opencv3.2_contrib and opencv3.2 ? I don't have a cake file. I found CMakeLists file and cmake folder with cmake files insite all of that in opencv3.2 folder. – SarahData Jul 28 '17 at 18:44
  • Is that folder directly from what you cloned in the repository?? – ΦXocę 웃 Пepeúpa ツ Jul 28 '17 at 18:47
  • when I chose only the folder which I cloned from https://github.com/opencv/opencv , cmake worked. but in your explanation, I had to choose as source the folder that contains both (opencv and opencv_contrib), no? – SarahData Jul 28 '17 at 18:54
  • well I finished all the steps of generating with cmake, but then in debug configurations I didn't find x86 (only win32), so that I create x64 config from it. and the build failed..Note in other projects there is debug x86 option.. – SarahData Jul 28 '17 at 20:34
  • i had an error in cmake related to import error, i went back to visual studio and install visual c++ desktop environment and it solved the problem – Omran Aug 12 '17 at 16:53
  • what do i need to include at the top? i have highgui.h gives me not found error, but the file highgui exists on the OpenCV folder in C drive – aero Dec 11 '17 at 17:51
  • 1
    had to downvote both answers, i spend 40 mins following instruction but no one gave an idea of the name/ path of things to include, and now my IDE is full of "not found" error messages, what do we include?! (i.e. #include <> or "" or opencv or opencv2 ....???) – aero Dec 11 '17 at 18:26
  • down vote, this way not working, followed all steps, still highgui in build folder not found. – bim Jul 20 '18 at 03:16
  • What highgui are you talking about?? – ΦXocę 웃 Пepeúpa ツ Jul 20 '18 at 03:19
  • 1
    What about linking and including the path to `\include`-path? Do I have to do that anyway? Because in VS I get error "E1696: cannot open source file "opencv2/opencv.hpp" " – sequoia Oct 28 '18 at 11:01
  • 1
    If installing with CUDA in step 5 select x64 – Anerudhan Gopal Apr 18 '19 at 20:43
  • 1
    Followed the steps and everything was right. Then, running a program that needs opencv, got the following error:https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1083?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(C1083)%26rd%3Dtrue&view=vs-2019 "cannot open include file 'opencv2/opencv.hpp':no such file or directory" – dtr43 Nov 04 '19 at 17:17
3

Additonal tip to the accepted answer - if you get an error like this:

Invalid escape sequence \ o

when you copy and paste the filesystem paths, make sure you use forward slash ( / ) instead of backward slash, so

C:\openCV\opencv_contrib-3.2.0\modules

Must be

C:/openCV/opencv_contrib-3.2.0/modules

Instead. Otherwise, you will get errors that escape sequence \o does not exist.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
1

Previous post is just perfect. VS 2017 has lib version VS141.

Just extension of previous.

8 Step. After the cmake generate the Opencv.sln in build folder. Open this project in VS 2017 Opencv visual studio 2017

I am using approach first, just DEBUG x64 version

Opencv visual studio 2017

The output looks like this

Opencv visual studio 2017

After Debug, switch to Release and Build whole solution again. Same for 32 bit version. Your final solution is under install location under build folder. With contrib libs whole process took almost 30 minutes on I7. Just shaking with CMAKE until the zero defects, include what ever FFMPEG, Contrib and other path. Final build in VS 2017.

The whole approach and install of built libs you can find on my blog Here

globalex
  • 549
  • 3
  • 11
  • 1
    I don't know why I don't find x64 option in debug like in this step you mentioned in your blog: "FIRST just select DEBUG, x64 version like on picture, click right mouse on Entire solution and hit BUILD solution like on picture." – SarahData Jul 28 '17 at 21:03
  • Hi @globalex. I can successfully build opencv with VS2017. And my project compile perfectly. But videocapture doesn't function normally in debug mode. In release mode, everything works fine. – Tim Hsu Nov 27 '17 at 12:18
  • had to downvote both answers, i spend 40 mins following instruction but no one gave an idea of the name/ path of things to include, and now my IDE is full of "not found" error messages, what do we include?! (i.e. #include <> or "" or opencv or opencv2 ....???) – aero Dec 11 '17 at 18:26
  • @aero. How we can cover all your mistakes in this steps? It works.. Tim Hsu, you need to build opencv with ffmpeg to let video capture works.. Depends also capture of what and the platform. But in general FFMPEG – globalex Dec 18 '17 at 20:00