Go package for computer vision using OpenCV 4 and beyond.

GoCV

GoCV

Go Reference CircleCI Build status AppVeyor Build status codecov Go Report Card License

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.5.1) on Linux, macOS, and Windows. We intend to make the Go language 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:

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

GoCV

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:

package main

import (
	"fmt"
	"image/color"

	"gocv.io/x/gocv"
)

func main() {
    // set to use a video capture device 0
    deviceID := 0

	// open webcam
	webcam, err := gocv.OpenVideoCapture(deviceID)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer webcam.Close()

	// open display window
	window := gocv.NewWindow("Face Detect")
	defer window.Close()

	// prepare image matrix
	img := gocv.NewMat()
	defer img.Close()

	// color for the rect when faces detected
	blue := color.RGBA{0, 0, 255, 0}

	// load classifier to recognize faces
	classifier := gocv.NewCascadeClassifier()
	defer classifier.Close()

	if !classifier.Load("data/haarcascade_frontalface_default.xml") {
		fmt.Println("Error reading cascade file: data/haarcascade_frontalface_default.xml")
		return
	}

	fmt.Printf("start reading camera device: %v\n", deviceID)
	for {
		if ok := webcam.Read(&img); !ok {
			fmt.Printf("cannot read device %v\n", deviceID)
			return
		}
		if img.Empty() {
			continue
		}

		// detect faces
		rects := classifier.DetectMultiScale(img)
		fmt.Printf("found %d faces\n", len(rects))

		// draw a rectangle around each face on the original image
		for _, r := range rects {
			gocv.Rectangle(&img, r, blue, 3)
		}

		// show the image in the window, and wait 1 millisecond
		window.IMShow(img)
		window.WaitKey(1)
	}
}

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 Tensorflow for object classification.

How to install

To install GoCV, run the following command:

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

To run code that uses the GoCV package, you must also install OpenCV 4.5.1 on your system. Here are instructions for Ubuntu, Raspian, macOS, and Windows.

Ubuntu/Linux

Installation

You can use make to install OpenCV 4.5.1 with the handy Makefile included with this repo. If you already have installed OpenCV, you do not need to do so again. The installation performed by the Makefile is minimal, so it may remove OpenCV options such as Python or Java wrappers if you have already installed OpenCV some other way.

Quick Install

The following commands should do everything to download and install OpenCV 4.5.1 on Linux:

cd $GOPATH/src/gocv.io/x/gocv
make install

If you need static opencv libraries

make install BUILD_SHARED_LIBS=OFF

If it works correctly, at the end of the entire process, the following message should be displayed:

gocv version: 0.26.0
opencv lib version: 4.5.1

That's it, now you are ready to use GoCV.

Using CUDA with GoCV

See the cuda directory for information.

Using OpenVINO with GoCV

See the openvino directory for information.

Make Install for OpenVINO and Cuda

The following commands should do everything to download and install OpenCV 4.5.1 with CUDA and OpenVINO on Linux:

cd $GOPATH/src/gocv.io/x/gocv
make install_all

If you need static opencv libraries

make install_all BUILD_SHARED_LIBS=OFF

If it works correctly, at the end of the entire process, the following message should be displayed:

gocv version: 0.26.0
opencv lib version: 4.5.1-openvino
cuda information:
  Device 0:  "GeForce MX150"  2003Mb, sm_61, Driver/Runtime ver.10.0/10.0

Complete Install

If you have already done the "Quick Install" as described above, you do not need to run any further commands. For the curious, or for custom installations, here are the details for each of the steps that are performed when you run make install.

Install required packages

First, you need to change the current directory to the location of the GoCV repo, so you can access the Makefile:

cd $GOPATH/src/gocv.io/x/gocv

Next, you need to update the system, and install any required packages:

make deps

Download source

Now, download the OpenCV 4.5.1 and OpenCV Contrib source code:

make download

Build

Build everything. This will take quite a while:

make build

If you need static opencv libraries

make build BUILD_SHARED_LIBS=OFF

Install

Once the code is built, you are ready to install:

make sudo_install

Verifying the installation

To verify your installation you can run one of the included examples.

First, change the current directory to the location of the GoCV repo:

cd $GOPATH/src/gocv.io/x/gocv

Now you should be able to build or run any of the examples:

go run ./cmd/version/main.go

The version program should output the following:

gocv version: 0.26.0
opencv lib version: 4.5.1

Cleanup extra files

After the installation is complete, you can remove the extra files and folders:

make clean

Cache builds

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

Custom Environment

By default, pkg-config is used to determine the correct flags for compiling and linking OpenCV. 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.

For example:

export CGO_CPPFLAGS="-I/usr/local/include"
export CGO_LDFLAGS="-L/usr/local/lib -lopencv_core -lopencv_face -lopencv_videoio -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_objdetect -lopencv_features2d -lopencv_video -lopencv_dnn -lopencv_xfeatures2d"

Please note that you will need to run these 2 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

Docker

The project now provides Dockerfile which lets you build GoCV Docker image which you can then use to build and run GoCV applications in Docker containers. The Makefile contains docker target which lets you build Docker image with a single command:

make docker

By default Docker image built by running the command above ships Go version 1.13.5, but if you would like to build an image which uses different version of Go you can override the default value when running the target command:

make docker GOVERSION='1.13.5'

Running GUI programs in Docker on macOS

Sometimes your GoCV programs create graphical interfaces like windows eg. when you use gocv.Window type when you display an image or video stream. Running the programs which create graphical interfaces in Docker container on macOS is unfortunately a bit elaborate, but not impossible. First you need to satisfy the following prerequisites:

  • install xquartz. You can also install xquartz using homebrew by running brew cask install xquartz
  • install socat brew install socat

Note, you will have to log out and log back in to your machine once you have installed xquartz. This is so the X window system is reloaded.

Once you have installed all the prerequisites you need to allow connections from network clients to xquartz. Here is how you do that. First run the following command to open xquart so you can configure it:

open -a xquartz

Click on Security tab in preferences and check the "Allow connections" box:

app image

Next, you need to create a TCP proxy using socat which will stream X Window data into xquart. Before you start the proxy you need to make sure that there is no process listening in port 6000. The following command should not return any results:

lsof -i TCP:6000

Now you can start a local proxy which will proxy the X Window traffic into xquartz which acts a your local X server:

socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"

You are now finally ready to run your GoCV GUI programs in Docker containers. In order to make everything work you must set DISPLAY environment variables as shown in a sample command below:

docker run -it --rm -e DISPLAY=docker.for.mac.host.internal:0 your-gocv-app

Note, since Docker for MacOS does not provide any video device support, you won't be able run GoCV apps which require camera.

Alpine 3.7 Docker image

There is a Docker image with Alpine 3.7 that has been created by project contributor @denismakogon. You can find it located at https://github.com/denismakogon/gocv-alpine.

Raspbian

Installation

We have a special installation for the Raspberry Pi that includes some hardware optimizations. You use make to install OpenCV 4.5.1 with the handy Makefile included with this repo. If you already have installed OpenCV, you do not need to do so again. The installation performed by the Makefile is minimal, so it may remove OpenCV options such as Python or Java wrappers if you have already installed OpenCV some other way.

Quick Install

The following commands should do everything to download and install OpenCV 4.5.1 on Raspbian:

cd $GOPATH/src/gocv.io/x/gocv
make install_raspi

If it works correctly, at the end of the entire process, the following message should be displayed:

gocv version: 0.26.0
opencv lib version: 4.5.1

That's it, now you are ready to use GoCV.

macOS

Installation

You can install OpenCV 4.5.1 using Homebrew.

If you already have an earlier version of OpenCV (3.4.x) installed, you should probably remove it before installing the new version:

brew uninstall opencv

You can then install OpenCV 4.5.1:

brew install opencv

pkgconfig Installation

pkg-config is used to determine the correct flags for compiling and linking OpenCV. You can install it by using Homebrew:

brew install pkgconfig

Verifying the installation

To verify your installation you can run one of the included examples.

First, change the current directory to the location of the GoCV repo:

cd $GOPATH/src/gocv.io/x/gocv

Now you should be able to build or run any of the examples:

go run ./cmd/version/main.go

The version program should output the following:

gocv version: 0.26.0
opencv lib version: 4.5.1

Cache builds

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

Custom Environment

By default, pkg-config is used to determine the correct flags for compiling and linking OpenCV. 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.

For example:

export CGO_CXXFLAGS="--std=c++11"
export CGO_CPPFLAGS="-I/usr/local/Cellar/opencv/4.5.1/include"
export CGO_LDFLAGS="-L/usr/local/Cellar/opencv/4.5.1/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"

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

Windows

Installation

The following assumes that you are running a 64-bit version of Windows 10.

In order to build and install OpenCV 4.5.1 on Windows, you must first download and install MinGW-W64 and CMake, as follows.

MinGW-W64

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.

Add the C:\Program Files\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev2\mingw64\bin path to your System Path.

CMake

Download and install CMake https://cmake.org/download/ to the default location. CMake installer will add CMake to your system path.

OpenCV 4.5.1 and OpenCV Contrib Modules

The following commands should do everything to download and install OpenCV 4.5.1 on Windows:

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

It might take up to one hour.

Last, add C:\opencv\build\install\x64\mingw\bin to your System Path.

Verifying the installation

Change the current directory to the location of the GoCV repo:

chdir %GOPATH%\src\gocv.io\x\gocv

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.26.0
opencv lib version: 4.5.1

That's it, now you are ready to use GoCV.

Cache builds

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

Custom Environment

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.

For example:

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

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

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/c19458cf64bd3e91faae85c3ac887481

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:

gocv.MatProfile.Count()

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

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:

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:

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 does 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-2020 The Hybrid Group.

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

Owner
The Hybrid Group
The software company that makes your hardware work.
The Hybrid Group
Comments
  • I am getting an error

    I am getting an error "eigenNonSymmetric is not a member of 'cv' " at core.cpp:344:9

    I want to work on Opencv3 with Go-Lang. I have followed step according to http://gocv.io I have installed it successfully but now I am getting an error "eigenNonSymmetric is not a member of 'cv' " at core.cpp:344:9 any help. And I am on Ubuntu 18.04

    Description

    I am getting this error

    gocv.io/x/gocv

    core.cpp: In function ‘void Mat_EigenNonSymmetric(Mat, Mat, Mat)’: core.cpp:345:9: error: ‘eigenNonSymmetric’ is not a member of ‘cv’ cv::eigenNonSymmetric(*src, *eigenvalues, *eigenvectors); ^~~~~~~~~~~~~~~~~

    Your Environment

    • Operating System and version: Ubuntu 18.04
    • OpenCV version used: 3.4.1
    • How did you install OpenCV? according to gocv.io
    • GoCV version used: latest
    • Go version: 1.10.2
    • Did you run the env.sh or env.cmd script before trying to go run or go build? no
  • Use Go-style return values for gocv functions that have result Mat

    Use Go-style return values for gocv functions that have result Mat

    When reviewing #135, I was like "wait, why is this test actually passing?". The test was about flipping a Mat into a brand new Mat, both passed to a function that looks roughly like this:

    func Flip(src Mat, dst Mat, flipCode int) {}
    

    In Go, parameters passed by value (default like above) should not be modified (and in any case, it will never change the value in the parent block). In gocv, this would works though because we manipulate a C pointer under the hood but this is a bit misleading.

    Question is: could we agree on a "convention" stating that all arguments modified should be passed by pointer?

    If we take the Flip() function as an example:

    src := ...
    dst := gocv.NewMat()
    
    // wait, how does `dst` can be updated? No return-value either? Hmmm.
    gocv.Flip(src, dst, 0);
    
    // versus
    
    // oh right so `Flip` takes a `src` Mat and puts the result in `dst`. Lovely <3
    gocv.Flip(src, &dst, 0);
    
  • Building gocv package under visual studio code

    Building gocv package under visual studio code

    Hello, I cannot run my test application under Visual Studio Code. I have the following error: file not found: opencv2/opencv.hpp Any idea? Regards Fooky

  • imgproc.cpp issue on MacOS

    imgproc.cpp issue on MacOS

    Hi! I am getting

    imgproc.cpp:61:28: error: expected '(' for function-style cast or type construction
    

    error, tried running simple Hello World! sample.

  • I am getting error with OpenVino on Windows

    I am getting error with OpenVino on Windows

    Description

    I want to use Openvino and i have installed it on windows 10. Only one peoblem is I am getting error something like this.

    screenshot 86

    • Did you run the env.sh or env.cmd script before trying to go run or go build?

    there is no file at

    source /opt/intel/computer_vision_sdk/bin/setupvars.sh

    and i tried to find it out but it was for ubuntu and i ran it it's not working.

    so can you help me out here. where can i get

    setupvars.sh for window 10

    Your Environment

    • Operating System and version:Windows 10
    • OpenCV version used:3.4.1
    • How did you install OpenCV? according to gocv.io
    • GoCV version used: latest
    • Go version: 10.1.2
  • gocv.NewMat ToImage function memory leak

    gocv.NewMat ToImage function memory leak

    gocv.NewMat ToImage function memory leak

    Description

    func (od *OpenCVDecoder) ReadJpeg() ([]byte, error) {
    	img := gocv.NewMat()
    	defer func() {
    		err := img.Close()
    		fmt.Println("ReadJpeg err :",err)
    		//runtime.GC()
    		//fmt.Println("read gc")
    	}()
    	if ok := od.vc.Read(&img); !ok {
    		return nil, nil
    	}
    
    	if img.Empty() {
    		return nil, nil
    	}
    	_, err := img.ToImage()
    	if err != nil {
    		return nil, err
    	}
    	//i, err := ImageToJpeg(iimg)
    	return nil,nil
    }
    
    

    iimg variable not release.i must use runtime.GC(),but it didn't work well also.i think img.Close() dosen't work.

    after task stop. pprof report

    (pprof) top
    Showing nodes accounting for 13.85MB, 100% of 13.85MB total
          flat  flat%   sum%        cum   cum%
        7.91MB 57.13% 57.13%     7.91MB 57.13%  image.NewRGBA
        5.94MB 42.87%   100%     5.94MB 42.87%  gocv.io/x/gocv._Cfunc_GoBytes
    
    (pprof) list ToImage
    Total: 13.85MB
    ROUTINE ======================== gocv.io/x/gocv.(*Mat).ToImage in /Users/x/go/pkg/mod/gocv.io/x/[email protected]/core.go
             0    13.85MB (flat, cum)   100% of Total
             .          .    753:   }
             .          .    754:
             .          .    755:   width := m.Cols()
             .          .    756:   height := m.Rows()
             .          .    757:   step := m.Step()
             .     5.94MB    758:   data := m.ToBytes()
             .          .    759:   channels := m.Channels()
             .          .    760:
             .          .    761:   if t == MatTypeCV8UC1 {
             .          .    762:           img := image.NewGray(image.Rect(0, 0, width, height))
             .          .    763:           c := color.Gray{Y: uint8(0)}
             .          .    764:
             .          .    765:           for y := 0; y < height; y++ {
             .          .    766:                   for x := 0; x < width; x++ {
             .          .    767:                           c.Y = uint8(data[y*step+x])
             .          .    768:                           img.SetGray(x, y, c)
             .          .    769:                   }
             .          .    770:           }
             .          .    771:
             .          .    772:           return img, nil
             .          .    773:   }
             .          .    774:
             .     7.91MB    775:   img := image.NewRGBA(image.Rect(0, 0, width, height))
             .          .    776:   c := color.RGBA{
             .          .    777:           R: uint8(0),
             .          .    778:           G: uint8(0),
             .          .    779:           B: uint8(0),
             .          .    780:           A: uint8(255),
    
    

    Steps to Reproduce

    Your Environment

    • Operating System and version: macOS
    • OpenCV version used: 4.5.0
    • How did you install OpenCV? brew install
    • GoCV version used: 0.25.0
    • Go version: 1.15.x
    • Did you run the env.sh or env.cmd script before trying to go run or go build? no
  • Tracking

    Tracking

    an attempt to wrap the tracking module from opencv_contrib

    W I P , rfc.

    this is making heavy use of c++ / go interfaces.

    Tracker_Init() and Tracker_Update() only need to be implemented once (in c++), and can be reused for any go implentation of the Tracker interface, saving a ton of boilerplate code

    if we'd use interfaces more often, properly, e.g, there'd be no need to do this again and again , and also have all this boilerplate repetition here and here and so on ..

    @willdurand , may i challenge you / pick your mind on this idea, too ?

    (it still needs a wrapper function on the go side, with the correct implementation type as self, to satisfy the go interface, but all we need to do there is to extract the C.Tracker from the go type, and pass it on to the shared c++ wrapper)

    also tests can be rigorously simplified, if all we need to do is pass a different implementation instance to the same base test

    some things did not make it:

    • GOTURN: needs a XXXmb pre-trained dnn, can't be tested without (throws exception) * CSRT: only available in 3.4.1 .. i'll just add it
    • MultiTracker: i've made an attempt, but there are problems. i'll leave the code in comments (for now), maybe someone has an idea. a: i see no proper way to add new tracker instances to it. the way i tried, worked on my win box, but throws an exception on travis. it's also somewhat unclear, how & when to Close() the added trackers b: i'm unable to release a C.struct_RECTS in cgo, (can't access Rects_Close somehow) c: gocv.toRectangles(ret) would be very useful here, but it's private in gocv all in all, opencv's MultiTracker is just a shallow wrapper around vector<Ptr<Tracker>>, maybe we're better off, building a mock class in pure go ?
  • https://github.com/opencv/opencv/archive/4.0.0.tar.gz not found

    https://github.com/opencv/opencv/archive/4.0.0.tar.gz not found

    brew install hybridgroup/tools/opencv

    Description

    brew install hybridgroup/tools/opencv

    url: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54 Error: Failed to download resource "opencv" Download failed: https://github.com/hybridgroup/homebrew-tools/releases/download/v0.10.1/opencv-4.0.0.high_sierra.bottle.tar.gz Warning: Bottle installation failed: building from source. ==> Downloading https://github.com/opencv/opencv/archive/4.0.0.tar.gz ==> Downloading from https://codeload.github.com/opencv/opencv/tar.gz/4.0.0

    curl: (22) The requested URL returned error: 404 Not Found

    Steps to Reproduce

    Your Environment

    • Operating System and version:
    • OpenCV version used:
    • How did you install OpenCV?
    • GoCV version used:
    • Go version:
    • Did you run the env.sh or env.cmd script before trying to go run or go build?
  • Memory leak in SIFT?

    Memory leak in SIFT?

    Hello there. I have one really strange problem with SIFT detector. I use it in our service and seems like there is memory leak. I use method DetectAndCompute. When I looked at code I found that defer KeyPoints_Close is commented. I was able to make new test binding KeyPoints_Close2 and free memory, but it didn't solve my problem. I have no clue how to reproduce it without using service. I mean simple application doesn't cause such big memory consumption.

    For example here is some code:

    func TestSift(t *testing.T) {
    	gray := gocv.IMRead("path_to_image", gocv.IMReadGrayScale)
    
    	sift := contrib.NewSIFT()
    	defer sift.Close()
    
    	res := make([]point, 0) //this is used as result in service's code.
    	for iter := 0; iter < 50; iter++ {
    		mask := gocv.NewMat()
    		defer mask.Close()
    		k, d := sift.DetectAndCompute(gray, mask)
    		defer d.Close()
    
    		for i, v := range k {
    			var tmp []float64
    			for j := 0; j < d.Cols(); j++ {
    				tmp = append(tmp, float64(d.GetFloatAt(i, j)))
    			}
    			res = append(res, point{keypoint: v, descriptor: tmp})
    		}
    	}
    	utils.ShowImage(gray) //This function just shows image in new window. 
    }
    

    Similar code is used in my service and I have crashes (because of memory problem). Interesting thing - using BRISK (for example) fixes this problem, but using SURF - not.

    I wasn't able to find significant difference between BRISK and SIFT bindings, so maybe there is another problem. I have C++ application and it hasn't such problem, so maybe there is some golang specific things.

    I tried to use pprof, but without any luck (But maybe I didn't use it in right way). Valgrind also shows that there is no memory issues.

    I just have no clue what's happening there. Top shows, that memory usage increases by ~300Mb after each call. Does anybody face this problem?

  • Install issue .... `dnn.cpp:119:102: warning: passing NULL to non-pointer argument 1`

    Install issue .... `dnn.cpp:119:102: warning: passing NULL to non-pointer argument 1`

    Description

    1. I followed the readme to install opencv.
    2. when I finished the install step ... I got this issue...

    Does anyone know what is it means?

    # gocv.io/x/gocv
    dnn.cpp: In function 'cv::Mat* Net_BlobFromImage(Mat, double, Size, Scalar, bool, bool)':
    dnn.cpp:119:102: warning: passing NULL to non-pointer argument 1 of 'cv::Scalar_<_Tp>::Scalar_(_Tp) [with _Tp = double]' [-Wconversion-null]
             return new cv::Mat(cv::dnn::blobFromImage(*image, scalefactor, sz, NULL, swapRB, crop, ddepth));
    

    and then, when I run the gocv, this issue always has happened.

    Your Environment

    PRETTY_NAME="Debian GNU/Linux 10 (buster)"
    NAME="Debian GNU/Linux"
    VERSION_ID="10"
    VERSION="10 (buster)"
    VERSION_CODENAME=buster
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    
    • Operating System and version: debian
    • OpenCV version used: opencv lib version: 4.2.0
    • How did you install OpenCV? use makefile
    • GoCV version used: gocv version: 0.22.0
    • Go version: go version go1.13.1 linux/amd64
    • Did you run the env.sh or env.cmd script before trying to go run or go build? No
  • NewMatFromBytes directly uses an unsafe pointer to the []byte, but no reference preventing garbage collection

    NewMatFromBytes directly uses an unsafe pointer to the []byte, but no reference preventing garbage collection

    Description

    The current NewMatFromBytes (in gocv 0.15) does not copy the data from the slice but instead uses an unsafe pointer to the data when initializing the OpenCV Mat. Furthermore, the gocv Mat maintains no reference or anything to this data to prevent that slice from being garbage collected (the relationship between the Mat and the byte slice may not even be picked up by the escape analysis -- perhaps the byte slices are being allocated on the stack!).

    I don't know if this was expected behavior or not, but I was caught off guard! If this is working as designed, kindly leave a few notes in the documentation for NewMatFromBytes.

    My workaround for now is to clone the Mat after calling NewMatFromBytes (before the []byte goes out of scope).

    Steps to Reproduce

    Here is a test case that reproduces the strange problems I was experiencing (Mat's getting overridden after the byte slice is out of scope).

    import (
    	"crypto/rand"
    	"testing"
    
    	"github.com/stretchr/testify/require"
    	"gocv.io/x/gocv"
    )
    
    // Equal returns whether the images are equal.
    func Equal(img1, img2 gocv.Mat) bool {
    	eqMat := gocv.NewMat()
    	gocv.Compare(img1, img2, &eqMat, gocv.CompareNE)
    	count := gocv.CountNonZero(eqMat)
    	eqMat.Close()
    	return count == 0
    }
    
    func TestMatFromBytes(t *testing.T) {
    	size := 500
    	var img, imgCloned gocv.Mat
    	var err error
    
    	makeImg := func() {
    		b := make([]byte, size*size)
    		_, err = rand.Read(b)
    		img, err = gocv.NewMatFromBytes(size, size, gocv.MatTypeCV8U, b)
    		require.NoError(t, err)
    		imgCloned = img.Clone()
    	}
    
    	useMemory := func() {
    		b := make([]byte, size*size)
    		_, err = rand.Read(b)
    	}
    
    	makeImg()
    
    	// This assertion will pass.
    	require.True(t, Equal(img, imgCloned))
    	for i := 0; i < 100; i++ {
    		useMemory()
    	}
    
    	// This assertion will fail!
    	require.True(t, Equal(img, imgCloned))
    }
    
    

    Your Environment

    • Operating System and version: Mac OSX 10.13.6
    • OpenCV version used: 3.4.2
    • How did you install OpenCV? (homebrew)
    • GoCV version used: 0.15.0
    • Go version: 1.10.3
    • Did you run the env.sh or env.cmd script before trying to go run or go build? (N/A?)

    P.S. OpenCV in go is awesome and I look forward to this project becoming mature! Thanks for all the work you guys do!

  • GoCV DockerHub image problem

    GoCV DockerHub image problem

    docker image from dockerhub

    I see docker image from https://hub.docker.com/r/gocv/opencv/tags. I intended to use image 4.7.0-gpu-cuda-11.2.2 to cross build my onnx image detection problem used in windows.

    sudo docker run --gpus all -it  -v /home/xxx/Document/apex:/data gocv/opencv:4.7.0-gpu-cuda-11.2.2
    docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "go version": executable file not found in $PATH: unknown.
    ERRO[0000] error waiting for container: context canceled
    

    But here comes the problem, the golang path in the image is no properly configured.

    exec: "go version": executable file not found in $PATH: unknown.
    

    I hope there may be some docs.

  • Remove opencv2/aruco.hpp include

    Remove opencv2/aruco.hpp include

    opencv/aruco.hpp is still part of opencv_contrib in OpenCV 4.7.0. (related to #1045)

    We only use core functions and don't have opencv_contrib as part of our build process. When attempting to update to gocv 0.32.1/OpenCV 4.7.0 we encountered the error: aruco.h:6:10: fatal error: opencv2/aruco.hpp: No such file or directory. Including only opencv2/opencv.hpp appears sufficient.

    Though we don't use the aruco, make test passes with this change.

  • gocv.NewPointVectorFromMat(img) throwing Exception

    gocv.NewPointVectorFromMat(img) throwing Exception

    While converting a gocv.Mat into point vector using NewPointVectorFromMat func panic/exception is occuring

    exception: what(): OpenCV(4.6.0) /tmp/opencv/opencv-4.6.0/modules/core/src/copy.cpp:320: error: (-215:Assertion failed) channels() == CV_MAT_CN(dtype) in function 'copyTo' Screenshot from 2022-12-07 16-27-54

    Steps to Reproduce

    1. load a image from dir img:=gocv.IMRead("dir",color)
    2. pv := gocv.NewPointVectorFromMat(img)

    Your Environment

    • Operating System and version: ubuntu 20.4 focal
    • OpenCV version used: 4.0 and above
    • How did you install OpenCV? sudo apt instal libopencv-dev and make install for gocv
    • GoCV version used: 0.30.0
    • Go version: go1.18.4 linux/amd64
    • Did you run the env.sh or env.cmd script before trying to go run or go build? No
  • Why is my camera changing when i use VideoCaptureDevice

    Why is my camera changing when i use VideoCaptureDevice

    I want to get the camera content via VideoCaptureDevice and when I pass in my virtual camera ID, it always turns on the virtual camera on the first run and the computer camera on the second run.

    Description

    my code: func main() { webcam, _ := gocv.VideoCaptureDevice(1) // my virtual camera id is 1 defer webcam.Close() window := gocv.NewWindow("test") defer window.Close() img := gocv.NewMat() defer img.Close() timeUnix:=time.Now().Unix() + 3 for { if time.Now().Unix() > timeUnix { break } webcam.Read(&img) window.IMShow(img) window.WaitKey(1) } }

    Before executing the programmy camera id image

    After executing the program. The camera id changes. Procedure

    image

    Your Environment

    • Operating System and version: MacOS Ventura 13.0.1 macbookpro M1
    • OpenCV version used: 4.5.3
    • How did you install OpenCV? yes
    • GoCV version used:0.31.0
    • Go version: go1.17.2 darwin/arm64
    • Did you run the env.sh or env.cmd script before trying to go run or go build? no
  • Detect blurry images

    Detect blurry images

    Description

    I am trying to detect blurry images according to this Python example

    func main() {
    	img := gocv.IMRead("dog.webp", gocv.IMReadColor)
    	defer img.Close()
    
    	gray := gocv.NewMat()
    	defer gray.Close()
    
    	gocv.CvtColor(img, &gray, gocv.ColorBGRToGray)
    
    	blur := gocv.NewMat()
    	defer blur.Close()
    
    	gocv.Laplacian(gray, &blur, gocv.MatTypeCV64F, 1, 1, 0, gocv.BorderDefault)
    
    	meanStdDev := gocv.NewMat()
    	defer meanStdDev.Close()
    
    	stdDev := gocv.NewMat()
    	defer stdDev.Close()
    
    	gocv.MeanStdDev(blur, &meanStdDev, &stdDev)
    }
    

    How can I get variance out of meanStdDev or stdDev? I know this code is pretty much ugly. :D

    Your Environment

    • Operating System and version: Linux Mint 20.3 Una
    • OpenCV version used: 4.6.0
    • How did you install OpenCV? By instructions from this repository.
    • GoCV version used: 0.31.0
    • Go version: 1.19.3
    • Did you run the env.sh or env.cmd script before trying to go run or go build? No, my build works.
  • [feature request] PaddleOCR integration

    [feature request] PaddleOCR integration

    Is it possible to integrate PaddleOCR into this project ? It is probably the best general purpose OCR so far. I am sure it will help a lot, though I found this job is beyond my reach .

使用 Golang+Chrome+OpenCV 破解腾讯滑块验证码
使用 Golang+Chrome+OpenCV 破解腾讯滑块验证码

一、背景 滑块验证码是一项人机识别技术,操作简单,真人体验好,机器识别效果也不差,可以有效防止脚本做任务,增加机器脚本薅羊毛的难度。但其破解也相对简单,这里演示一个Demo,以了解。通过 OpenCV 匹配找出滑块位置,计算出滑动距离,然后模拟 js 鼠标事件,在 Chrome 控制台执行脚本,完成

Dec 28, 2022
Convert images to computer generated art using delaunay triangulation.
Convert images to computer generated art using delaunay triangulation.

▲ Triangle is a tool for generating triangulated image using delaunay triangulation. It takes a source image and converts it to an abstract image comp

Dec 29, 2022
📸 Clean your image folder using perceptual hashing and BK-trees using Go!
📸 Clean your image folder using perceptual hashing and BK-trees using Go!

Image Cleaner ?? ?? ➡ ?? This tool can take your image gallery and create a new folder with image-alike-cluster folders. It uses a perceptual image ha

Oct 8, 2022
Go package captcha implements generation and verification of image and audio CAPTCHAs.
Go package captcha implements generation and verification of image and audio CAPTCHAs.

Package captcha ⚠️ Warning: this captcha can be broken by advanced OCR captcha breaking algorithms. import "github.com/dchest/captcha" Package captch

Dec 30, 2022
Go package for decoding and encoding TARGA image format

tga tga is a Go package for decoding and encoding TARGA image format. It supports RLE and raw TARGA images with 8/15/16/24/32 bits per pixel, monochro

Sep 26, 2022
asciigrid is a Go package that implements decoder and encoder for the Esri ASCII grid format, also known as ARC/INFO ASCII GRID.

asciigrid asciigrid is a Go package that implements decoder and encoder for the Esri ASCII grid format, also known as ARC/INFO ASCII GRID. Install go

Jul 3, 2022
Pbm - Package ppm implements a Portable Bit Map (PBM) image decoder and encoder written in Go

Package pbm import "github.com/slashformotion/pbm" Package pbm implements a Portable Bit Map (PBM) image decoder and encoder. The supported image col

Jan 5, 2022
go-pix is a Go library for generating Pix transactions using Copy and Paste or QR codes. 💳 💰
go-pix is a Go library for generating Pix transactions using Copy and Paste or QR codes. 💳 💰

go-pix go-pix is a Go library for generating Pix transactions using Copy and Paste or QR codes.

Sep 12, 2022
A Pong clone made from scratch with Go and C using OpenGL 3.3

Go-Pong A Pong video game clone made with Go lang and OpenGL 3.3 using C. Gameplay Offline Key bindings are 'w' and 's' for the left player and 'up ar

Feb 10, 2022
Human-friendly Go module that builds and prints directory trees using ASCII art

Human-friendly Go module that builds and prints directory trees using ASCII art.

Oct 11, 2022
A pure Go package for coordinate transformations.

WGS84 A pure Go package for coordinate transformations. go get github.com/wroge/wgs84 Usage east, north, h := wgs84.LonLat().To(wgs84.ETRS89UTM(32)).R

Nov 25, 2022
Go package for fast high-level image processing powered by libvips C library

bimg Small Go package for fast high-level image processing using libvips via C bindings, providing a simple programmatic API. bimg was designed to be

Jan 2, 2023
Go bindings for GStreamer (retired: currently I don't use/develop this package)

Retired. I don't use/develop this package anymore. Go bindings for GStreamer at a very early stage of maturity. This package is based on GLib bindings

Nov 10, 2022
Imaging is a simple image processing package for Go
Imaging is a simple image processing package for Go

Imaging Package imaging provides basic image processing functions (resize, rotate, crop, brightness/contrast adjustments, etc.). All the image process

Dec 30, 2022
Go Perceptual image hashing package

goimagehash Inspired by imagehash A image hashing library written in Go. ImageHash supports: Average hashing Difference hashing Perception hashing Wav

Jan 3, 2023
generativeart is a Go package to generate many kinds of generative art.
generativeart is a Go package to generate many kinds of generative art.

generativeart is a Go package to generate many kinds of generative art. The goal is to collect some excellent generative art (implemented in R or Processing), and rewrite them in Go again

Dec 29, 2022
golang package to find the K most dominant/prominent colors in an image
golang package to find the K most dominant/prominent colors in an image

prominentcolor Find the K most dominant colors in an image The Kmeans function returns the K most dominant colors in the image, ordered in the order o

Nov 7, 2022
openGL Have Fun - A Go package that makes life with OpenGL enjoyable.

glhf openGL Have Fun - A Go package that makes life with OpenGL enjoyable. go get github.com/faiface/glhf Main features Garbage collected OpenGL obje

Jan 1, 2023
p5 is a simple package that provides primitives resembling the ones exposed by p5js.org
p5 is a simple package that provides primitives resembling the ones exposed by p5js.org

p5 p5 is a simple package that provides primitives resembling the ones exposed by the p5/processing library. License p5 is released under the BSD-3 li

Dec 11, 2022