CGO-free OpenCV for Go with embedded native runtimes, purego loading, and a universal macOS dylib.
go-opencv wraps real OpenCV in a prebuilt native backend and loads it at runtime through purego. Consumers stay on CGO_ENABLED=0, ship a normal Go binary, and still get OpenCV-backed image processing.
The project uses nihui/opencv-mobile 4.13.0 as the upstream OpenCV distribution and embeds platform-specific native artifacts directly into the Go package.
CGO_ENABLED=0for application builds- Embedded native runtimes with automatic cache extraction
- Real OpenCV execution, not a partial pure-Go imitation
- Cross-platform release assets for Windows, Linux, and macOS
- Windows support for both
amd64andarm64 - Universal macOS dylib with both
x86_64andarm64slices - Familiar OpenCV-style API for image I/O, color conversion, filtering, geometry, contours, drawing, and more
| Platform | GOARCH | Artifact | Status |
|---|---|---|---|
| Windows | amd64 |
goopencv.dll |
Supported |
| Windows | arm64 |
goopencv.dll |
Supported |
| Linux | amd64 |
goopencv.so |
Supported |
| Linux | arm64 |
goopencv-linux-arm64.so |
Supported |
| macOS | amd64 |
goopencv.dylib |
Supported via universal dylib |
| macOS | arm64 |
goopencv.dylib |
Supported via universal dylib |
More detail lives in SUPPORTED_PLATFORMS.md.
Add the module to your project:
go get github.com/brainplusplus/go-opencv@v0.3.1Then import it in your code:
import opencv "github.com/brainplusplus/go-opencv"There is no separate native runtime installation step for supported platforms. The package embeds the platform-appropriate backend and extracts it automatically on first use.
package main
import (
"context"
"fmt"
"log"
opencv "github.com/brainplusplus/go-opencv"
)
func main() {
cv, err := opencv.New(context.Background())
if err != nil {
log.Fatal(err)
}
defer cv.Close()
img, err := cv.IMRead("photo.png")
if err != nil {
log.Fatal(err)
}
defer img.Close()
blurred, err := cv.GaussianBlur(img, opencv.Size{Width: 5, Height: 5}, 0)
if err != nil {
log.Fatal(err)
}
defer blurred.Close()
if err := cv.IMWrite("output.png", blurred); err != nil {
log.Fatal(err)
}
rows, _ := blurred.Rows()
cols, _ := blurred.Cols()
fmt.Printf("wrote output.png (%dx%d)\n", cols, rows)
}The repository includes runnable examples in examples.
From the repo root:
cd examples
go run . -demo=allUseful variants:
go run . -demo=basic
go run . -demo=io -in sample.png -out output
go run ./tools/gen-sampleIMReadIMReadBytesIMWrite
CvtColorBlur,GaussianBlur,MedianBlurThreshold,AdaptiveThreshold,CannyResize,Flip,Transpose,WarpAffine,WarpPerspective
FindContours,DrawContours,ContourArea,BoundingRectRectangle,Circle,Line,ArrowedLine,PutTextHoughLines,HoughLinesP,HoughCircles
NewMat,Zeros,Ones,EyeSplit,Merge,Region,Reshape- explicit
Close()andDelete()ownership flow
The complete surface area is documented in API_REFERENCE.md.
opencv.New() follows a simple path:
- select the embedded native library for the active platform
- extract it into the user cache directory
- load it through purego
- bind the exported ABI symbols
You can override the embedded backend explicitly:
rt, err := opencv.New(ctx, opencv.WithDLL("/path/to/goopencv.dylib"))Native backend builds are platform-specific.
build-tools\build-goopencv.batbash build-tools/build-goopencv-linux.shbash build-tools/build-goopencv-linux-arm64.shbash build-tools/build-goopencv-macos.shThe macOS build script produces a universal dist/goopencv.dylib and verifies that both x86_64 and arm64 slices are present.
All current release artifacts are built on top of opencv-mobile 4.13.0.
GitHub Actions builds:
goopencv.dllfor Windowsamd64goopencv.dllfor Windowsarm64goopencv.sofor Linuxamd64goopencv-linux-arm64.sofor Linuxarm64goopencv.dylibas a universal macOS artifact
Release uploads publish the macOS binary as goopencv-macos-universal.dylib.
go test ./...For local validation, scripts/verify-backend.ps1 will run Go tests and report the size of any native backend found in dist/.
- SUPPORTED_PLATFORMS.md
- API_REFERENCE.md
- docs/architecture.md
- docs/backend-abi.md
- docs/interface-contract.md
MIT