reference resources Official documents
MinGW-W64
Note that threads-posix MinGW-W64 in the parent directory. Here I download x86_64-posix-seh .
Then add the bin folder to the environment variable:
CMake
What I download here is cmake-3.21.2-windows-x86_64.msi Install the package.
Download opencv
I downloaded 4.5 Version 3.
Download opencv_contrib
Corresponding download 4.5 Version 3.
Compile using CMake
After selecting the source code file path and build path, click Configure to pop up a dialog box and select MinGW Makefiles. In fact, just like dockerfile, select a construction method. After execution, a red list appears because some relevant files are not downloaded or cannot be downloaded for some reason.
At this time, go to the build directory and find cmakedownloadlog Txt, this file will tell you what to download, where to download and where to put it.
After downloading some software, click Configure again. If there is no red alarm, it indicates that the configuration is correct. At this time, set {OpenCV_ EXTRA_ modules_ The path is OpenCV_ modules directory of contrib.
After performing a configuration, if a red alarm appears, continue to find cmakedownloadlog Txt download the corresponding file. If there is no red, click the Generate button. Output after success:
Detected processor: AMD64 Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "2.7") Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "3.2") 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 libjpeg-turbo: VERSION = 2.1.0, BUILD = opencv-4.5.3-libjpeg-turbo 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.3-openjp2-2.4.0 OpenJPEG libraries will be built from sources: libopenjp2 (version "2.4.0") 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. VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) OpenCV Python: during development append to PYTHONPATH: D:/go-opencv/build/python_loader Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) Module opencv_alphamat disabled because the following dependencies are not found: Eigen freetype2: NO harfbuzz: NO Julia not found. Not compiling Julia Bindings. Module opencv_ovis disabled because OGRE3D was not found No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available. Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake. Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components. Failed to find gflags - Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h Module opencv_sfm disabled because the following dependencies are not found: Eigen Glog/Gflags Tesseract: NO Consider adding OPENCV_ALLOCATOR_STATS_COUNTER_TYPE=int/int64_t according to your build configuration Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx512_skx.cpp Excluding from source files list: <BUILD>/modules/core/test/test_intrin256.avx512_skx.cpp Excluding from source files list: <BUILD>/modules/core/test/test_intrin512.avx512_skx.cpp Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': D:/go-opencv/opencv-4.5.3/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake opencv_dnn: filter out cuda4dnn source code Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx512_skx.cpp highgui: using builtin backend: WIN32UI rgbd: Eigen support is disabled. Eigen is Required for Posegraph optimization data: Download: face_landmark_model.dat General configuration for OpenCV 4.5.3 ===================================== Version control: unknown Extra modules: Location (extra): D:/go-opencv/opencv_contrib-4.5.3/modules Version control (extra): unknown Platform: Timestamp: 2021-08-28T13:13:30Z Host: Windows 10.0.19042 AMD64 CMake: 3.21.2 CMake generator: MinGW Makefiles CMake build tool: D:/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:/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:/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: aruco barcode bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto Disabled: world Disabled by dependency: - Unavailable: alphamat cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf java julia matlab ovis python2 python3 sfm viz Applications: tests perf_tests apps Documentation: NO Non-free algorithms: NO Windows RT support: NO GUI: Win32 UI: YES VTK support: NO Media I/O: ZLib: build (ver 1.2.11) JPEG: build-libjpeg-turbo (ver 2.1.0-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: 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.134.100) avformat: YES (58.76.100) avutil: YES (56.70.100) swscale: YES (5.9.100) avresample: YES (4.0.0) GStreamer: NO DirectShow: YES Parallel framework: none Trace: YES (built-in) Other third-party libraries: Lapack: NO Eigen: NO Custom HAL: NO Protobuf: build (3.5.1) OpenCL: YES (no extra features) Include path: D:/go-opencv/opencv-4.5.3/3rdparty/include/opencl/1.2 Link libraries: Dynamic load Python (for build): NO Java: ant: NO JNI: F:/java/include F:/java/include/win32 F:/java/include Java wrappers: NO Java tests: NO Install to: D:/go-opencv/build/install ----------------------------------------------------------------- Configuring done Generating done
Mingw32 make thread compilation
Open the cmd window under the build directory and execute the command:
mingw32-make -j%NUMBER_OF_PROCESSORS%
When you see 100%, you can continue to execute the instruction:
mingw32-make install
At this point, the compilation is completed, and an install directory is added under the build directory:
4.5. Version 3 does not have any problems with thread compilation, but in 4.5 During the compilation of version 2 thread: undefined reference to`__ imp_ GetACP`.
Reason: opencv_contrib # 4.5 Version 2 introduces wechat_qr_code module caused.
Solution: Reference Issues
Find opencv_contrib-4.5.2\modules\wechat_qrcode\src\zxing\zxing.hpp file, change line 30 to:
#if defined(__ANDROID_API__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
Remove:
#if defined(_MSC_VER) #ifndef NO_ICONV #define NO_ICONV #endif #endif
Custom environment variables
The default location is C:\opencv\build\install\include, but it can be modified:
set CGO_CXXFLAGS=--std=c++11 set CGO_CPPFLAGS=-IC:\opencv\build\install\include set CGO_LDFLAGS=-LC:\opencv\build\install\x64\mingw\lib -lopencv_core453 -lopencv_face453 -lopencv_videoio453 -lopencv_imgproc453 -lopencv_highgui453 -lopencv_imgcodecs453 -lopencv_objdetect453 -lopencv_features2d453 -lopencv_video453 -lopencv_dnn453 -lopencv_xfeatures2d453 -lopencv_plot453 -lopencv_tracking453 -lopencv_img_hash453 -lopencv_calib3d453
Testing
Download gocv package:
go get -u -d gocv.io/x/gocv
Copy to build sibling:
Go to D: \ go opencv \ gocv io\x\ gocv@v0.28.0 \CMD \ version path, run the command:
go run main.go
So far, the environment has been successfully built.
Note: if there are Python environment variables, please clear the python environment variables first, or uninstall completely before installing.
The compilation process is a little long, Here are the compiled files.
gocv picture test
package main import ( "gocv.io/x/gocv" ) func main() { // Read picture test // Build a new window window := gocv.NewWindow("Test") // Reset the window size. The default build window is too small window.ResizeWindow(1366, 768) // Read picture location img := gocv.IMRead("D:\\GoFiles\\my-opencv\\images\\001.jpg", gocv.IMReadColor) // Show pictures window.IMShow(img) // Window time window.WaitKey(1000 * 5) }
effect: