Install Gocv on MAC

Jimmy (xiaoke) Shen
2 min readSep 11, 2020

--

You can follow the official instruction from [1]. Except checking 1 and 2 in my article to make everything work.

1. Go to the correct path then run the following code

jimmy@jimmys-MacBook-Pro ~ % cd $GOPATH/src/gocv.io/x/gocvjimmy@jimmys-MacBook-Pro gocv % go run ./cmd/version/main.gogocv version: 0.24.0opencv lib version: 4.4.0

2. Fix the Package opencv4 was not found issue

Issue

jimmy@jimmys-MacBook-Pro x % go run ./gocv/cmd/version/main.go# pkg-config --cflags  -- opencv4Package opencv4 was not found in the pkg-config search path.Perhaps you should add the directory containing `opencv4.pc'to the PKG_CONFIG_PATH environment variableNo package 'opencv4' foundpkg-config: exit status 1

How to fix: find the opencv4.pc file

find / -name opencv4.pc/usr/local/Cellar/opencv/4.4.0_2/lib/pkgconfig/opencv4.pc

How to fix: add it to the PKG_CONFIG_PATH

vi ~/.zshrc

Add the following at the end of the opened file

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/Cellar/opencv/4.4.0_2/lib/pkgconfig"

source

source ~/.zshrc

Then the problem is fixed

cd $GOPATH/src/gocv.io/x/gocvjimmy@jimmys-MacBook-Pro gocv % go run ./cmd/version/main.gogocv version: 0.24.0opencv lib version: 4.4.0

See yourself from gocv

package mainimport (
"gocv.io/x/gocv"
)
func main() {
webcam, _ := gocv.VideoCaptureDevice(0)
window := gocv.NewWindow("Hello")
img := gocv.NewMat()
for {
webcam.Read(&img)
window.IMShow(img)
window.WaitKey(1)
}
}

How to fix this issue

/usr/local/go/pkg/tool/darwin_amd64/link: running clang++ failed: exit status 1ld: warning: directory not found for option '-L/usr/local/Cellar/opencv/4.4.0_2/lib'ld: library not found for -lopencv_freetypeclang: error: linker command failed with exit code 1 (use -v to see invocation)

When update the opencv from 4.4.0 to 4.5.0, I got the problem. Although I can run some simple GOCV code. Try this and it works[2]

export CGO_CPPFLAGS="-I/usr/local/Cellar/opencv/4.5.0_2/include"
export CGO_LDFLAGS="-L/usr/local/Cellar/opencv/4.5.0_2/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core"

For the above command, run it by each line and verify by echo

If it is not working, you should reopen the terminal.

If everything is correct, and cheers. You can say hi to yourself.

Reference

[1]https://gocv.io/getting-started/macos/

[2] https://github.com/hybridgroup/gocv/issues/504

--

--

Responses (1)