1) Unzip these archives, you can use default unzip path or create your own. Better separate them.

2) Open cmake-gui. Or if you are fairly familiar with camke cmdlines, skip this step.


(资料图)

3) Choose compiler you want to use, MinGW can be used if you would like to compile your projects in vscode. 

If you are using Microsoft visual studio, don't bother to compile opencv from source codes. 

Releases, marked by windows icon and text you can download straight from "https://opencv.org/releases/" are already compiled by MSVC.

Just google how to config it.

In this case, MinGW is used.

4) Config your Gcc and G++ compilers, before which gcc and g++ shall be added system env path. Make sure the system recognises these compilers.

Choose your Gcc and G++.

5) Choose one folder to store generated files. Doesn't have to be where your want store it finally.

6) Click 'configurate'.

7) Configuration takes several minuates, during which downloading components from github repo will be conducted. Surely 'SSL connect error' failures like these occur.

Don't worry and look into each error info in red. 

8) Configuration done, red items will be displayed within cmake gui. These are not actually errors but some items you have to configure for a second time, of course third, fourth... .

Some experiences I can share:

-- search python, uncheck all 'cause we are compiling for C/C++;

-- search java, uncheck all for the same reason.

-- search allo, uncheck it for I solved my 'fatal error' which crashed compilation of opencv.

-- search extra, fill or choose your "opencv_contrib/modules" path, containing some 3rd-party non-free algorithms. I just want a complete opencv.

-- click 'add entry', input 'ENABLE_CXX11' to enable new standards.

-- click 'add entry', input 'OPENCV_VS_VSVERSIONINFO_SKIP', make it a bool and value is true. Solved my compilation error by it.

9) In some regions due to internet traffic, downloading components failed. 

Quite normal, try to switch to proxy or turn proxy, again and again and again and so on, by click 'configurate' until downloading succeeds.

11) Before clicking 'generate', uncheck 'BUILD_aruco'. Also crushed if checked for me.

12) Click 'generate'.

13) Open cmd or powershell, cd to the path in step 5).

14) Input 'mingw32-make', if you would like to enable multiple threads compiling, input 'mingw32-make -j8'. 

8 is how many threads enables, depending on your CPU. I, personally use '-j14' on a 8-core cpu.

15) Wait, preying not a single fatal error.

16) Wait, preying not a single fatal error.

17) Wait, preying not a single fatal error.

18) Keep calm and wait. Keep calm and prey until 100%.

19) Looked into errors, which shouldn't occur. Ignore all warnings.

20) In the cmd opened in step 13), input 'mingw32-make install'.

21) Wait, no need to prey for I haven't seen error in this step.

22) Complete compilation. DO NOT delete any codes.

23) Explorer-open the step 5) folder. Add 'bin' path to system env path.

24) Find the folder named 'install' in the step 5) folder, click 'x64', click 'mingw', add another 'bin' to sys env path.

23 and 24 'bin' in my case contains different contents.

25) Configure in vscode the 'c_cpp_properties.json', find the 'install' folder in 24), go into it and add 'include' path to includePath in the 'c_cpp_properties.json'.

And add 'include/openCV2' to it.

26) Configure in vscode the 'tasks.json'. You shall have at least one building task such as g++, clang++. If you don't write a hello world, vscode will tell you to configure one.

In the 'arg', add "-I", " the-25)-include-path","-I", " the-25)-include/openCV2-path".

Also in the section, find 24) folder, where there are one 'bin' and one 'lib'. Add the 'lib' path like "-L"," the-lib-path ".

27) Continuing 26). Add "-l","libopencv_xxxddd", where 'xxx' stands for module name, 'ddd' for opencv version. For example "-l","libopencv_core470", "-l","libopencv_imgcodecs470".

Opencv version remains the same for all '.a' files in 'lib'. You should add them all to avoid linking issues or add what your needs if they are perfectly clear to you.

28) Try to write an example. 

推荐内容