mingw编译opencv

我这里是msys2

这个是msys2的教程
https://blog.csdn.net/qq_39942341/article/details/105931335?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167821146216800197067008%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=167821146216800197067008&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-1-105931335-null-null.blog_rank_default&utm_term=msys2&spm=1018.2226.3001.4450

msys2

这里用msys2直接装
配置一下msys2的环境变量(教程里有)
装qt6,将C:\Qt\6.4.2\mingw_64\bin加入环境变量(不知道msvc的能不能用)
(qt6这个可以极端一点,直接想办法下载dll,但是我懒得尝试)

pacman -Syu
pacman -S mingw-w64-x86_64-opencv

mingw编译opencv_第1张图片
接着测试
main.cpp

#include 
#include 
#include 

int main() {
	cv::Mat img = cv::imread("E:\\opencv_test\\test.jpg");
	cv::imshow("img", img);
	cv::waitKey(0);
	cv::destroyAllWindows();
	return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(OpenCV_test VERSION 0.1.0)

IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE Release)
ENDIF()

add_executable(${PROJECT_NAME} main.cpp)

# Where to find CMake modules and OpenCV
#set(OpenCV_DIR "D:\\opencv-4.5.5\\opencv-4.5.5\\build")
find_package(OpenCV REQUIRED)

message(STATUS "OpenCV Include: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV Libs: ${OpenCV_LIBRARIES}")

INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME}  ${OpenCV_LIBS})

打开cmd/powershell

mkdir build
cd build
cmake -G "MinGW Makefiles" ..
mingw32-make
.\OpenCV_test.exe

懒人版

https://gitee.com/kirigaya/opencv_built_by_gcc_on_-windows
https://github.com/huihut/OpenCV-MinGW-Build

下了之后,需要设置环境变量D:\opencv-4.5.5\opencv-4.5.5\build\x64\mingw\bin
(就是\x64\mingw\bin)

接着测试
除了cmake,剩下的和上面一样
CMakeLists.txt
与上一个的区别就是set(OpenCV_DIR)这一行

cmake_minimum_required(VERSION 3.0.0)
project(OpenCV_test VERSION 0.1.0)

IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE Release)
ENDIF()

add_executable(${PROJECT_NAME} main.cpp)

# Where to find CMake modules and OpenCV
set(OpenCV_DIR "D:\\opencv-4.5.5\\opencv-4.5.5\\build")
find_package(OpenCV REQUIRED)

message(STATUS "OpenCV Include: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV Libs: ${OpenCV_LIBRARIES}")

INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME}  ${OpenCV_LIBS})

源码

我这里mingw是用msys2装的

配置一下msys2的环境变量(教程里有)
装qt6,将C:\Qt\6.4.2\mingw_64\bin加入环境变量(不知道msvc的能不能用)
cmake加入环境变量(要比msys高

4.5.4

这里我WITH_QT=OFF,因为我检测不出来5,只能检测6,但是4.5.4好像搞不了qt6

mkdir build
mkdir mingw_build
cd mingw_build
cmake -G "MinGW Makefiles" .. -DCMAKE_INSTALL_PREFIX="../build" -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_python_tests=OFF -DOPENCV_DOWNLOAD_MIRROR_ID=gitcode -DBUILD_TIFF=ON -DOPENCV_GENERATE_PKGCONFIG=ON -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=OFF -DWITH_CUDA=OFF -DWITH_VTK=OFF -DWITH_MATLAB=OFF -DCMAKE_CXX_COMPILER=g++ -DWITH_QT=OFF -DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_MSMF=OFF -DWITH_IPP=OFF -DWITH_OPENGL=ON -DBUILD_JAVA=OFF -DOPENCV_ENABLE_ALLOCATOR_STATS=OFF -DWITH_MSMF=OFF -DWITH_ZLIB=ON -DBUILD_ZLIB=ON

检查一下他的输出,没问题就可以编译了
其中12是线程数量,你核多,可以多开一点

mingw32-make clean
mingw32-make -j 12

没有问题就可以安装

mingw32-make install

设置环境变量D:\opencv-4.5.4\opencv-4.5.4\build\x64\mingw\bin
接着是测试,和懒人版差不多

4.5.5

暂时没成功

我这里mingw是用msys2装的

去官网下sources
https://opencv.org/releases/
选择sources
我这里选的4.5.5
mingw编译opencv_第2张图片

mingw编译opencv_第3张图片

在这个目录打开cmd/powershell

mkdir build
mkdir mingw_build
cd mingw_build
cmake -G "MinGW Makefiles" .. -DCMAKE_INSTALL_PREFIX="../build" -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_python_tests=OFF -DOPENCV_DOWNLOAD_MIRROR_ID=gitcode -DBUILD_TIFF=ON -DOPENCV_GENERATE_PKGCONFIG=ON -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=OFF -DWITH_CUDA=OFF -DWITH_VTK=OFF -DWITH_MATLAB=OFF -DCMAKE_CXX_COMPILER=g++ -DWITH_QT=ON -DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_MSMF=OFF -DWITH_IPP=OFF -DWITH_OPENGL=ON -DBUILD_JAVA=OFF -DOPENCV_ENABLE_ALLOCATOR_STATS=OFF -DWITH_MSMF=OFF -DWITH_ZLIB=ON -DBUILD_ZLIB=ON

接着

cmake-gui

不要tests
mingw编译opencv_第4张图片
不要java
mingw编译opencv_第5张图片
要opengl
mingw编译opencv_第6张图片
不要ENABLE_PRECOMPILED_HEADERS
mingw编译opencv_第7张图片
不要WITH_MSMF
mingw编译opencv_第8张图片
不要WITH_IPP
mingw编译opencv_第9张图片
不要OPENCV_ENABLE_ALLOCATOR_STATS
mingw编译opencv_第10张图片
不要java
mingw编译opencv_第11张图片

要qt
mingw编译opencv_第12张图片
点configure直到没有红的
然后点generate

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

  Platform:
    Timestamp:                   2023-03-08T03:26:07Z
    Host:                        Windows 10.0.19045 AMD64
    CMake:                       3.26.0-rc1
    CMake generator:             MinGW Makefiles
    CMake build tool:            C:/msys64/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 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (16 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (5 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                C:/msys64/mingw64/bin/g++.exe  (ver 12.2.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:                  C:/msys64/mingw64/bin/cc.exe
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -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=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 video videoio
    Disabled:                    java_bindings_generator python_bindings_generator python_tests world
    Disabled by dependency:      -
    Unavailable:                 java python2 python3 ts
    Applications:                apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI:                           QT6
    QT:                          YES (ver 6.4.2 )
      QT OpenGL support:         YES (Qt6::OpenGL )
    Win32 UI:                    YES
    OpenGL support:              YES (opengl32 glu32)

  Media I/O: 
    ZLib:                        zlib (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.1.2-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     OpenEXR::OpenEXR (ver 3.1.5)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   YES (1.22.1)
    DirectShow:                  YES

  Parallel framework:            none

  Trace:                         YES (built-in)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       YES (ver 3.4.0)
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)

  OpenCL:                        YES (no extra features)
    Include path:                D:/opencv-4.5.5/opencv-4.5.5/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            NO

  Install to:                    D:/opencv-4.5.5/opencv-4.5.5/build
-----------------------------------------------------------------
mingw32-make clean
mingw32-make -j 12

然后我就死在这了

mingw32-make[2]: *** No rule to make target 'zlib', needed by 'bin/libopencv_imgcodecs455.dll'.  Stop.
mingw32-make[1]: *** [CMakeFiles\Makefile2:1823: modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2
mingw32-make: *** [Makefile:165: all] Error 2

https://zhuanlan.zhihu.com/p/606873516
https://zhuanlan.zhihu.com/p/488401407

你可能感兴趣的:(安装与配置,opencv,计算机视觉,人工智能)