This page has information on how to install and use GoCV on Microsoft Windows 10, 64-bit.
Install the GoCV package:
go get -u -d gocv.io/x/gocv
In order to use GoCV on Windows you must build and install OpenCV 4.7.0. First download and install MinGW-W64 and CMake, as follows.
Download and run the MinGW-W64 compiler installer from https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/7.3.0/.
The latest version of the MinGW-W64 toolchain is
7.3.0, but any version from
7.X on should work.
Choose the options for “posix” threads, and for “seh” exceptions handling, then install to the default location
C:\Program Files\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev2\mingw64\bin path to your System Path.
Download and install CMake https://cmake.org/download/ to the default location. CMake installer will add CMake to your system path.
The following commands should do everything to download and install OpenCV 4.7.0 on Windows:
chdir %GOPATH%\src\gocv.io\x\gocv win_build_opencv.cmd
It will probably take at least 1 hour to download and build.
C:\opencv\build\install\x64\mingw\bin to your System Path.
Change the current directory to the location of the GoCV repo:
Now you should be able to build or run any of the command examples:
go run cmd\version\main.go
The version program should output the following:
gocv version: 0.32.0 opencv lib version: 4.7.0
That’s it, now you are ready to use GoCV.
If you are running a version of Go older than v1.10 and not modifying GoCV source, precompile the GoCV package to significantly decrease your build times:
go install gocv.io/x/gocv
By default, OpenCV is expected to be in
C:\opencv\build\install\include. This behavior can be disabled by supplying
-tags customenv when building/running your application. When building with this tag you will need to supply the CGO environment variables yourself.
Due to the way OpenCV produces DLLs, including the version in the name, using this method is required if you’re using a different version of OpenCV.
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_core455 -lopencv_face455 -lopencv_videoio455 -lopencv_imgproc455 -lopencv_highgui455 -lopencv_imgcodecs455 -lopencv_objdetect455 -lopencv_features2d455 -lopencv_video455 -lopencv_dnn455 -lopencv_xfeatures2d455 -lopencv_plot455 -lopencv_tracking455 -lopencv_img_hash455 -lopencv_calib3d455
Please note that you will need to run these 3 lines of code one time in your current session in order to build or run the code, in order to setup the needed ENV variables. Once you have done so, you can execute code that uses GoCV with your custom environment like this:
go run -tags customenv cmd\version\main.go