gocv是OpenCV4在Go中的绑定,使用它可以在Go里做图像处理。

安装gocv

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

会安装到%GOPATH%\src下。

编译OpenCV

要使用gocv,需要编译OpenCV。编译并不复杂,需要安装MinGW-W64和CMake。

1、安装MinGW-W64

  1. 版本要大于7.0,参数要选posix、seh,如下图;
  2. 下载后解压到任意位置,再将mingw64\bin添加到环境变量。

2、安装CMake

  1. 正常安装,环境变量自动添加。

3、编译

cd %GOPATH%\src\gocv.io\x\gocv
win_build_opencv.cmd

其中gocv库提供的win_build_opencv.cmd为安装过程命令:

  1. 下载opencv-4.1.2.zip并解压到C:\opencv
  2. 下载opencv_contrib-4.1.2.zip并解压到C:\opencv
  3. cmake
  4. mingw32-make
  5. mingw32-make install

其中set PATH那行可以省略。

echo off

if not exist "C:\opencv" mkdir "C:\opencv"
if not exist "C:\opencv\build" mkdir "C:\opencv\build"

echo Downloading OpenCV sources
echo.
echo For monitoring the download progress please check the C:\opencv directory.
echo.

REM This is why there is no progress bar:
REM https://github.com/PowerShell/PowerShell/issues/2138

echo Downloading: opencv-4.1.2.zip [91MB]
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri https://github.com/opencv/opencv/archive/4.1.2.zip -OutFile c:\opencv\opencv-4.1.2.zip"
echo Extracting...
powershell -command "$ProgressPreference = 'SilentlyContinue'; Expand-Archive -Path c:\opencv\opencv-4.1.2.zip -DestinationPath c:\opencv"
del c:\opencv\opencv-4.1.2.zip /q
echo.

echo Downloading: opencv_contrib-4.1.2.zip [58MB]
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri https://github.com/opencv/opencv_contrib/archive/4.1.2.zip -OutFile c:\opencv\opencv_contrib-4.1.2.zip"
echo Extracting...
powershell -command "$ProgressPreference = 'SilentlyContinue'; Expand-Archive -Path c:\opencv\opencv_contrib-4.1.2.zip -DestinationPath c:\opencv"
del c:\opencv\opencv_contrib-4.1.2.zip /q
echo.

echo Done with downloading and extracting sources.
echo.

echo on

cd /D C:\opencv\build
set PATH=%PATH%;C:\Program Files (x86)\CMake\bin;C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin
cmake C:\opencv\opencv-4.1.2 -G "MinGW Makefiles" -BC:\opencv\build -DENABLE_CXX11=ON -DOPENCV_EXTRA_MODULES_PATH=C:\opencv\opencv_contrib-4.1.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 -DCPU_DISPATCH= -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_OPENCL_D3D11_NV=OFF -Wno-dev
mingw32-make -j%NUMBER_OF_PROCESSORS%
mingw32-make install
rmdir c:\opencv\opencv-4.1.2 /s /q
rmdir c:\opencv\opencv_contrib-4.1.2 /s /q
chdir /D %GOPATH%\src\gocv.io\x\gocv

编译好后,添加C:\opencv\build\install\x64\mingw\bin到环境变量。

注意:

1、mingw32-make过程中断,编译失败

    • 可能是由于多线程编译时有的依赖库还没有生成造成的
    • 解决办法:反复执行mingw32-make

2、Python的影响

    • 如果你的电脑装了Python,且环境变量添加了Python安装目录,可能会出现链接错误,这是因为有些lib依赖库按环境变量在Python目录中找到了,而这些库与编译不匹配。
    • 解决办法:环境变量去掉Python目录,更简单的办法是把Python目录改名,编译完成后再改回来。

测试OpenCV

cd %GOPATH%\src\gocv.io\x\gocv
go run cmd\version\main.go

结果

gocv version: 0.21.0
opencv lib version: 4.1.2

安装成功。

问题

按以上过程编译、安装,不用额外配置就能直接使用。如果报错找不到 #include<opencv2/opencv.hpp>,可能是你没在C:\opencv下编译,Go就是这么无情!实际上,Gocv的默认Opencv路径在gocv.io\x\gocv\cgo.go里定义:

// +build !customenv,!openvino

package gocv

// Changes here should be mirrored in contrib/cgo.go and cuda/cgo.go.

/*
#cgo !windows pkg-config: opencv4
#cgo CXXFLAGS:   --std=c++11
#cgo windows  CPPFLAGS:   -IC:/opencv/build/install/include
#cgo windows  LDFLAGS:    -LC:/opencv/build/install/x64/mingw/lib -lopencv_core412 -lopencv_face412 -lopencv_videoio412 -lopencv_imgproc412 -lopencv_highgui412 -lopencv_imgcodecs412 -lopencv_objdetect412 -lopencv_features2d412 -lopencv_video412 -lopencv_dnn412 -lopencv_xfeatures2d412 -lopencv_plot412 -lopencv_tracking412 -lopencv_img_hash412 -lopencv_calib3d412
*/
import "C"

解决办法:如下配置CGO环境变量,然后使用-tags customenv执行go编译和运行

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_core412 -lopencv_face412 -lopencv_videoio412 -lopencv_imgproc412 -lopencv_highgui412 -lopencv_imgcodecs412 -lopencv_objdetect412 -lopencv_features2d412 -lopencv_video412 -lopencv_dnn412 -lopencv_xfeatures2d412 -lopencv_plot412 -lopencv_tracking412 -lopencv_img_hash412

其中路径修改为你的OpenCV目录;如果使用了不同版本的OpenCV,修改412为相应的版本号。使用方法:

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