Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

gocv

> 开发工具
Open source

Go package for computer vision using OpenCV 4 and beyond. Includes support for DNN, CUDA, OpenCV Contrib, and OpenVINO.

7.5K stars0 likes1 views
WebsiteGitHub

About

Go package for computer vision using OpenCV 4 and beyond. Includes support for DNN, CUDA, OpenCV Contrib, and OpenVINO.

GoCV

The GoCV package provides Go language bindings for the OpenCV 4 computer vision library.

The GoCV package supports the latest releases of Go and OpenCV (v4.12.0) on Linux, Docker, macOS, and Windows. Our ongoing mission is help the Go programming language be a "first-class" client compatible with the latest developments in the OpenCV ecosystem.

GoCV supports CUDA for hardware acceleration using Nvidia GPUs. Check out the CUDA README for more info on how to use GoCV with OpenCV/CUDA.

GoCV also supports Intel OpenVINO. Check out the OpenVINO README for more info on how to use GoCV with the Intel OpenVINO toolkit.

How to use

Hello, video

This example opens a video capture device using device "0", reads frames, and shows the video in a GUI window:

go
package main

import (
	"gocv.io/x/gocv"
)

func main() {
	webcam, _ := gocv.OpenVideoCapture(0)
	window := gocv.NewWindow("Hello")
	img := gocv.NewMat()

	for {
		webcam.Read(&img)
		window.IMShow(img)
		window.WaitKey(1)
	}
}

Face detect

This is a more complete example that opens a video capture device using device "0". It also uses the CascadeClassifier class to load an external data file containing the classifier data. The program grabs each frame from the video, then uses the classifier to detect faces. If any faces are found, it draws a green rectangle around each one, then displays the video in an output window:

…

More examples

There are examples in the cmd directory of this repo in the form of various useful command line utilities, such as capturing an image file, streaming mjpeg video, counting objects that cross a line, and using OpenCV with a DNN for face tracking.

How to install

To install GoCV, you must first have the matching version of OpenCV installed on your system. The current release of GoCV requires OpenCV 4.12.0.

We have instructions for Linux, macOS, Windows, and other platform options as well.

Linux

Please see our web site at https://gocv.io/getting-started/linux/

macOS

Please see our web site at https://gocv.io/getting-started/macos/

Windows

Please see our web site at https://gocv.io/getting-started/windows/

Docker

Please see our web site at https://gocv.io/getting-started/docker/

Android

There is some work in progress for running GoCV on Android using Gomobile. For information on how to install OpenCV/GoCV for Android, please see: https://gist.github.com/ogero/c19458cf64bd3e91faae85c3ac8874120

See original discussion here: https://github.com/hybridgroup/gocv/issues/235

Profiling

Since memory allocations for images in GoCV are done through C based code, the go garbage collector will not clean all resources associated with a Mat. As a result, any Mat created must be closed to avoid memory leaks.

To ease the detection and repair of the resource leaks, GoCV provides a Mat profiler that records when each Mat is created and closed. Each time a Mat is allocated, the stack trace is added to the profile. When it is closed, the stack trace is removed. See the runtime/pprof documentation.

In order to include the MatProfile custom profiler, you MUST build or run your application or tests using the -tags matprofile build tag. For example:

go run -tags matprofile cmd/version/main.go

You can get the profile's count at any time using:

go
gocv.MatProfile.Count()

You can display the current entries (the stack traces) with:

go
var b bytes.Buffer
gocv.MatProfile.WriteTo(&b, 1)
fmt.Print(b.String())

This can be very helpful to track down a leak. For example, suppose you have the following nonsense program:

go
package main

import (
	"bytes"
	"fmt"

	"gocv.io/x/gocv"
)

func leak() {
	gocv.NewMat()
}

func main() {
	fmt.Printf("initial MatProfile count: %v\n", gocv.MatProfile.Count())
	leak()

	fmt.Printf("final MatProfile count: %v\n", gocv.MatProfile.Count())
	var b bytes.Buffer
	gocv.MatProfile.WriteTo(&b, 1)
	fmt.Print(b.String())
}

Running this program produces the following output:

initial MatProfile count: 0
final MatProfile count: 1
gocv.io/x/gocv.Mat profile: total 1
1 @ 0x40b936c 0x40b93b7 0x40b94e2 0x40b95af 0x402cd87 0x40558e1
#	0x40b936b	gocv.io/x/gocv.newMat+0x4b	/go/src/gocv.io/x/gocv/core.go:153
#	0x40b93b6	gocv.io/x/gocv.NewMat+0x26	/go/src/gocv.io/x/gocv/core.go:159
#	0x40b94e1	main.leak+0x21			/go/src/github.com/dougnd/gocvprofexample/main.go:11
#	0x40b95ae	main.main+0xae			/go/src/github.com/dougnd/gocvprofexample/main.go:16
#	0x402cd86	runtime.main+0x206		/usr/local/Cellar/go/1.11.1/libexec/src/runtime/proc.go:201

We can see that this program would leak memory. As it exited, it had one Mat that was never closed. The stack trace points to exactly which line the allocation happened on (line 11, the gocv.NewMat()).

Furthermore, if the program is a long running process or if GoCV is being used on a web server, it may be helpful to install the HTTP interface )). For example:

go
package main

import (
	"net/http"
	_ "net/http/pprof"
	"time"

	"gocv.io/x/gocv"
)

func leak() {
	gocv.NewMat()
}

func main() {
	go func() {
		ticker := time.NewTicker(time.Second)
		for {
			<-ticker.C
			leak()
		}
	}()

	http.ListenAndServe("localhost:6060", nil)
}

This will leak a Mat once per second. You can see the current profile count and stack traces by going to the installed HTTP debug interface: http://localhost:6060/debug/pprof/gocv.io/x/gocv.Mat.

How to contribute

Please take a look at our CONTRIBUTING.md document to understand our contribution guidelines.

Then check out our ROADMAP.md document to know what to work on next.

Why this project exists

The https://github.com/go-opencv/go-opencv package for Go and OpenCV did not support any version above OpenCV 2.x, and work on adding support for OpenCV 3 had stalled for over a year, mostly due to the complexity of SWIG. That is why we started this project.

The GoCV package uses a C-style wrapper around the OpenCV 4 C++ classes to avoid having to deal with applying SWIG to a huge existing codebase. The mappings are intended to match as closely as possible to the original OpenCV project structure, to make it easier to find things, and to be able to figure out where to add support to GoCV for additional OpenCV image filters, algorithms, and other features.

For example, the OpenCV videoio module wrappers can be found in the GoCV package in the videoio.* files.

This package was inspired by the original https://github.com/go-opencv/go-opencv project, the blog post https://medium.com/@peterleyssens/using-opencv-3-from-golang-5510c312a3c and the repo at https://github.com/sensorbee/opencv thank you all!

License

Licensed under the Apache 2.0 license. Copyright (c) 2017-2026 The Hybrid Group.

Logo generated by GopherizeMe - https://gopherize.me

Issues· 358 open

View all issuesOpen on GitHub
  • #1381

    Add calib3d bindings for `findEssentialMat` and `recoverPose`

    Updated Oct 1, 2026
  • #1380

    Possible use-after-free of the mouse-callback `userdata` freed before the window is destroyed in `(*Window).Close`

    Updated Aug 28, 2026
  • #1379

    Possible memory leak of `c_name_ptr` when `SetMouseHandler` is called more than once on a window

    Updated Aug 28, 2026
  • #1378

    Possible out-of-bounds read in `gocv.GetLastExceptionMessage` via a non-NUL-terminated 1024-byte global

    Updated Aug 28, 2026
  • #1377

    Possible memory leak of the C-allocated `OpenCVResult.Message` in `contrib.OpenCVResult`

    Updated Aug 28, 2026
  • #1376

    Possible memory leak of the C-allocated `OpenCVResult.Message` in `cuda.OpenCVResult`

    Updated Aug 28, 2026
  • #1370

    OpenCV 5 Support

    Updated Aug 11, 2026
  • #1375

    Claude bugreport: `ImageToMatRGBA` produces a BGRA mat despite its name; roundtrip via `ToImage` silently changes alpha semantics — propose deprecation + honestly-named replacements

    Updated Jul 17, 2026
  • #1345

    how to use harware decode h264/265 with gocv(opencv) in win/linux platform?

    Updated Jun 27, 2026
  • #1274

    Fail to install the static version on Ubuntu 24.04

    Updated Jun 27, 2026

> Tags

Gocomputer-visioncomputervisioncudadnn

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具