windows+vscode+opencv源码安装配置

一、参考资料

VScode搭建OpenCV环境

OpenCV使用CMake和MinGW-w64的编译安装

win10下VSCode配置opencv4.4.0(超详细教程,亲测有效)

VSCODE中配置C++的OPENCV库

Windows10下Opencv4+CMake+MinGW64+VSC安装教程

VSCODE中配置C++的OPENCV库

 

二、步骤

1. vscode安装并配置

2. MinGW安装并配置

参考博客 【Windows版】配置C/C++开发环境

3. 官网下载OpenCV源码并安装

OpenCV官网

解压OpenCV源码
opencv-4.5.2-vc14_vc15.exe

windows+vscode+opencv源码安装配置_第1张图片

 

windows+vscode+opencv源码安装配置_第2张图片

windows+vscode+opencv源码安装配置_第3张图片

4. CMake安装并配置(配置编译选项)

(1)打开 cmake-gui,设置源码和生成路径:
Where is the source code: 
D:/360Downloads/Software/opencv/sources

Where to build the binaries: 
D:/360Downloads/Software/opencv/build/x64/MinGw

MinGw是新建的文件夹,用来存放编译好的OpenCV

windows+vscode+opencv源码安装配置_第4张图片

(2)点击 Configure,设置编译器

Specify the generator for this project: 
MinGW Makefiles

Compilers C: E:\MinGW-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\bin\gcc.exe
Compilers C++: E:\MinGW-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\bin\g++.exe

(3)勾选配置

windows+vscode+opencv源码安装配置_第5张图片

windows+vscode+opencv源码安装配置_第6张图片

 

windows+vscode+opencv源码安装配置_第7张图片

windows+vscode+opencv源码安装配置_第8张图片

 

windows+vscode+opencv源码安装配置_第9张图片

(4)生成的配置信息

windows+vscode+opencv源码安装配置_第10张图片

 

windows+vscode+opencv源码安装配置_第11张图片

Detected processor: AMD64
Could NOT find PythonInterp: Found unsuitable version "2.7.16", but required is at least "3.2" (found D:/Anaconda2/python.exe)
Looking for ccache - not found
AVX_512F is not supported by C++ compiler
AVX512_COMMON is not supported by C++ compiler
AVX512_SKX is not supported by C++ compiler
Dispatch optimization AVX512_SKX is not available, skipped
Cleaning INTERNAL cached variable: ZLIB_LIBRARY
Cleaning INTERNAL cached variable: ZLIB_INCLUDE_DIR
Found ZLIB: D:/Anaconda2/Library/lib/z.lib (found suitable version "1.2.11", minimum required is "1.2.3") 
libjpeg-turbo: VERSION = 2.0.6, BUILD = opencv-4.5.2-libjpeg-turbo
Cleaning INTERNAL cached variable: TIFF_LIBRARY
Cleaning INTERNAL cached variable: TIFF_INCLUDE_DIR
Found TIFF: D:/Anaconda2/Library/lib/tiff.lib (found version "4.0.10")  
Cleaning INTERNAL cached variable: WEBP_LIBRARY
Cleaning INTERNAL cached variable: WEBP_INCLUDE_DIR
Could NOT find OpenJPEG (minimal suitable version: 2.0, recommended version >= 2.3.1). OpenJPEG will be built from sources
OpenJPEG: VERSION = 2.4.0, BUILD = opencv-4.5.2-openjp2-2.4.0
OpenJPEG libraries will be built from sources: libopenjp2 (version "2.4.0")
Cleaning INTERNAL cached variable: PNG_LIBRARY
Cleaning INTERNAL cached variable: PNG_INCLUDE_DIR
Found ZLIB: D:/Anaconda2/Library/lib/z.lib (found version "1.2.11") 
Found PNG: D:/Anaconda2/Library/lib/libpng.lib (found version "1.6.37") 
Looking for D:/Anaconda2/Library/include/libpng/png.h
Looking for D:/Anaconda2/Library/include/libpng/png.h - not found
Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
Could NOT find BLAS (missing: BLAS_LIBRARIES) 
Could NOT find LAPACK (missing: LAPACK_LIBRARIES) 
    Reason given by package: LAPACK could not be found because dependency BLAS could not be found.

Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
OpenCV Python: during development append to PYTHONPATH: D:/360Downloads/Software/opencv/build/x64/MinGw/python_loader
Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 
Consider adding OPENCV_ALLOCATOR_STATS_COUNTER_TYPE=int/int64_t according to your build configuration
Excluding from source files list: /modules/core/test/test_intrin128.avx512_skx.cpp
Excluding from source files list: /modules/core/test/test_intrin256.avx512_skx.cpp
Excluding from source files list: /modules/core/test/test_intrin512.avx512_skx.cpp
Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': D:/360Downloads/Software/opencv/sources/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
opencv_dnn: filter out ocl4dnn source code
opencv_dnn: filter out cuda4dnn source code
Excluding from source files list: /modules/dnn/layers/layers_common.avx512_skx.cpp
Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
OpenCL samples are skipped: OpenCL SDK is required
SYCL/OpenCL samples are skipped: SYCL SDK is required
   - check configuration of SYCL_DIR/SYCL_ROOT/CMAKE_MODULE_PATH
   - ensure that right compiler is selected from SYCL SDK (e.g, clang++): CMAKE_CXX_COMPILER=D:/360Downloads/Software/MinGW/mingw64/bin/g++.exe
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
  CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
  directory
Call Stack (most recent call first):
  CMakeLists.txt:1010 (include)



General configuration for OpenCV 4.5.2 =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2021-04-09T02:59:22Z
    Host:                        Windows 10.0.18363 AMD64
    CMake:                       3.20.1
    CMake generator:             MinGW Makefiles
    CMake build tool:            D:/360Downloads/Software/MinGW/mingw64/bin/mingw32-make.exe
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (17 files):         + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                D:/360Downloads/Software/MinGW/mingw64/bin/g++.exe  (ver 8.1.0)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  D:/360Downloads/Software/MinGW/mingw64/bin/gcc.exe
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections  
    Linker flags (Debug):        -Wl,--gc-sections  
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
    Disabled:                    python2 world
    Disabled by dependency:      -
    Unavailable:                 java python3
    Applications:                tests perf_tests examples apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        D:/Anaconda2/Library/lib/z.lib (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.0.6-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         D:/Anaconda2/Library/lib/libpng.lib (ver 1.6.37)
    TIFF:                        D:/Anaconda2/Library/lib/tiff.lib (ver 42 / 4.0.10)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.91.100)
      avformat:                  YES (58.45.100)
      avutil:                    YES (56.51.100)
      swscale:                   YES (5.7.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES

  Parallel framework:            none

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                D:/360Downloads/Software/opencv/sources/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            D:/Anaconda2/python.exe

  Java:                          
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    D:/360Downloads/Software/opencv/build/x64/MinGw/install
-----------------------------------------------------------------

Configuring done
Generating done

5. 编译OpenCV
使用多线程编译minGW32-make -j 8

windows+vscode+opencv源码安装配置_第12张图片

6. 安装OpenCV
minGW32-make install

windows+vscode+opencv源码安装配置_第13张图片

7. 测试OpenCV是否安装成功 

测试文件

findContours_demo.cpp

/**
 * @function findContours_Demo.cpp
 * @brief Demo code to find contours in an image
 * @author OpenCV team
 */

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include 

using namespace cv;
using namespace std;

Mat src_gray;
int thresh = 100;
RNG rng(12345);

/// Function header
void thresh_callback(int, void* );

/**
 * @function main
 */
int main( int argc, char** argv )
{
    /// Load source image
    CommandLineParser parser( argc, argv, "{@input | HappyFish.jpg | input image}" );
    Mat src = imread( samples::findFile( parser.get( "@input" ) ) );
    if( src.empty() )
    {
      cout << "Could not open or find the image!\n" << endl;
      cout << "Usage: " << argv[0] << " " << endl;
      return -1;
    }

    /// Convert image to gray and blur it
    cvtColor( src, src_gray, COLOR_BGR2GRAY );
    blur( src_gray, src_gray, Size(3,3) );

    /// Create Window
    const char* source_window = "Source";
    namedWindow( source_window );
    imshow( source_window, src );

    const int max_thresh = 255;
    createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
    thresh_callback( 0, 0 );

    waitKey();
    return 0;
}

/**
 * @function thresh_callback
 */
void thresh_callback(int, void* )
{
    /// Detect edges using Canny
    Mat canny_output;
    Canny( src_gray, canny_output, thresh, thresh*2 );

    /// Find contours
    vector > contours;
    vector hierarchy;
    findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE );

    /// Draw contours
    Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
    for( size_t i = 0; i< contours.size(); i++ )
    {
        Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
        drawContours( drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0 );
    }

    /// Show in a window
    imshow( "Contours", drawing );
}

windows+vscode+opencv源码安装配置_第14张图片

 

三、注意事项

(1)在操作cmake-gui前,如果没有翻过墙的话,在C:\Windows\System32\drivers\etc\hosts文件最后追加:

151.101.72.133 raw.githubusercontent.com

因为cmake-gui会下载一些文件,这些文件都在raw.githubusercontent.com上,如果不修改一下hosts,许多文件很可能会下载失败

windows+vscode+opencv源码安装配置_第15张图片

 

四、可能存在的问题

(1)编译OpenCV出现错误

windows+vscode+opencv源码安装配置_第16张图片

错误原因:
在配置MinGw信息的时候,勾选信息错误


解决办法:

参考上文步骤4,第(3)勾选配置

 

(2)找不到libopencv_core452

windows+vscode+opencv源码安装配置_第17张图片

错误原因:

没有把编译好的OpenCV添加到环境变量中


解决办法:

添加环境变量
D:\360Downloads\Software\opencv\build\x64\MinGw\bin

windows+vscode+opencv源码安装配置_第18张图片

(3)无法找到入口
无法定位程序输入点_ZNSt6thread15_M_start_threadESt10unique_ptrINS_6_StateESt14default_deleteIS1_EEPFvvE

huihut/OpenCV-MinGW-Build
https://github.com/huihut/OpenCV-MinGW-Build/issues/18

windows+vscode+opencv源码安装配置_第19张图片

错误原因:
anaconda 中的 LIBSTDC++-6.DLL 与 MinGW64 的LIBSTDC++-6.DLL 冲突所导致的,在配置环境变量时将 MinGW64 的 bin 路径移到高于 anaconda 的 mingw-w64/bin 路径更高优先级的位置就可以了

解决办法:

windows+vscode+opencv源码安装配置_第20张图片

 

五、其他

(1)opencv 和opencv_contrib配置

(2)修改python版本

在cmake-gui中修改

windows+vscode+opencv源码安装配置_第21张图片

(3)ubuntu编译OpenCV

从源码安装OpenCV,使用python

你可能感兴趣的:(编程工具)