环境

  1. golang 1.16.2
  2. MinGW-W64 8.1.0
  3. cmake 3.20.4
  4. open-cv 4.5.2
  5. open-cv_opencv_contrib

安装

MinGW-W64(无坑)

下载地址:​​ MinGW-W64下载​​ 如下图

gocv(go mod)安装opencv4.5.2 !!!-Win10环境_python

安装CMake(无坑)

下载地址:​​Cmake下载​

安装OpenCV4.5.2

若安装到 c:/opencv 参考官网即可:​​https://gocv.io/getting-started/windows/​​​ 官网使用 gopath,若使用go mod需要进入go path下的 pkg\mod\gocv.io\x\gocv@v0.27.0 目录执行
使用 go mod 下载文件不会下载到 gopath/src 目录

下载gocv

go get -u -d gocv.io/x/gocv

选择4.5.2 zip 下载
opencv下载:​​​https://github.com/opencv/opencv​

opencv_contrib下载:​​https://github.com/opencv/opencv_contrib​

将下载的包解压到自定义目录:D:\develop\go-opencv
在该创建目录下创建build目录

使用win10命令行(管理员

进入刚才建立的D:\develop\go-opencv\build (一定是build目录
执行以下几条命令:(下面的几条命令失败后从此重新开始执行

cmake D:\develop\go-opencv\opencv-4.5.2 -G "MinGW Makefiles" -BD:\develop\go-opencv\build -DENABLE_CXX11=ON -DOPENCV_EXTRA_MODULES_PATH=D:\develop\go-opencv\opencv_contrib-4.5.2\modules -DBUILD_SHARED_LIBS=ON -DWITH_IPP=OFF -DWITH_MSMF=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_java=OFF -DBUILD_opencv_python=OFF -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=OFF -DBUILD_DOCS=OFF -DENABLE_PRECOMPILED_HEADERS=OFF -DBUILD_opencv_saliency=OFF -DBUILD_opencv_wechat_qrcode=OFF -DCPU_DISPATCH= -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_OPENCL_D3D11_NV=OFF -DOPENCV_ALLOCATOR_STATS_COUNTER_TYPE=int64_t -Wno-dev
mingw32-make -j%NUMBER_OF_PROCESSORS%
mingw32-make install

配置环境变量(必须)
D:\develop\go-opencv\build\install\x64\mingw\bin

等待以上成功后关闭命令窗口进入测试

测试

使用管理员执行命令行
执行以下命令

set CGO_CXXFLAGS=--std=c++11
set CGO_CPPFLAGS=-ID:\develop\go-opencv\build\install\include
set CGO_LDFLAGS=-LD:\develop\go-opencv\build\install\x64\mingw\lib -lopencv_core452 -lopencv_face452 -lopencv_videoio452 -lopencv_imgproc452 -lopencv_highgui452 -lopencv_imgcodecs452 -lopencv_objdetect452 -lopencv_features2d452 -lopencv_video452 -lopencv_dnn452 -lopencv_xfeatures2d452 -lopencv_plot452 -lopencv_tracking452 -lopencv_img_hash452 -lopencv_calib3d452

进入go path 下 pkg\mod\gocv.io\x\gocv@v0.27.0
执行:

go run -tags customenv cmd\version\main.go

若以上命令后续抛出错误使用:

go run cmd\version\main.go

显示:

gocv(go mod)安装opencv4.5.2 !!!-Win10环境_2d_02


为方便后续代码的使用

将以下临时变量添加作为永久环境变量,将=号左边作为变量名,右边作为变量值添加到环境变量中

set CGO_CXXFLAGS=--std=c++11
set CGO_CPPFLAGS=-ID:\develop\go-opencv\build\install\include
set CGO_LDFLAGS=-LD:\develop\go-opencv\build\install\x64\mingw\lib -lopencv_core452 -lopencv_face452 -lopencv_videoio452 -lopencv_imgproc452 -lopencv_highgui452 -lopencv_imgcodecs452 -lopencv_objdetect452 -lopencv_features2d452 -lopencv_video452 -lopencv_dnn452 -lopencv_xfeatures2d452 -lopencv_plot452 -lopencv_tracking452 -lopencv_img_hash452 -lopencv_calib3d452

惊天大坑

把python的环境变量注释掉!!!!否则无法使用!!!