Golang and GoCV on windows – MatchTemplate causes exceptions

  go, gocv, opencv, windows

I am running into the following issue when attempting to use the MatchTemplate function in the GoCV library.

Exception 0x20474343 0x17dfa24eb60 0x7ffca1fd59c6 0x7ffca6fd4f99

runtime: unknown pc 0x7ffca6fd4f99
stack: frame={sp:0x194f1ff0b0, fp:0x0} stack=[0x0,0x194f1ff9e0)
goroutine 1 [syscall]:
runtime.cgocall(0xcdf650, 0xc000123dc8)
        C:/Program Files/Go/src/runtime/cgocall.go:156 +0x4a fp=0xc000123da0 sp=0xc000123d68 pc=0xb640aa
gocv.io/x/gocv._Cfunc_MatchTemplate(0x17dfa218a30, 0x17dfa24e9b0, 0x17dfa24ea20, 0x5, 0x17dfa24ea90)
        _cgo_gotypes.go:5195 +0x52 fp=0xc000123dc8 sp=0xc000123da0 pc=0xcd1292
gocv.io/x/gocv.MatchTemplate.func1({0x17dfa218a30, {0x0, 0xdf3498, 0xef84a0}}, {0x17dfa24e9b0, {0x0, 0xcdbaf0, 0xc000123e98}}, 0xc000123e98, 0x5, ...)
        C:/Users/zachw/go/pkg/mod/gocv.io/x/[email protected]/imgproc.go:673 +0xcf fp=0xc000123e18 sp=0xc000123dc8 pc=0xcd202f
gocv.io/x/gocv.MatchTemplate({0x17dfa218a30, {0x0, 0x7cee6480140, 0xb9c932}}, {0x17dfa24e9b0, {0x0, 0xc000056000, 0x300000002}}, 0xc000056000, 0x5, ...)
        C:/Users/zachw/go/pkg/mod/gocv.io/x/[email protected]/imgproc.go:673 +0x79 fp=0xc000123e98 sp=0xc000123e18 pc=0xcd1ed9

The code is pretty simple I’m reading 2 PNG files then passing them into the MatchTemplate function.. I’m not sure what could possibly be wrong.

func main() {
    haystack_img := gocv.IMRead("./test.png", gocv.IMReadUnchanged)
    needle_img := gocv.IMRead("./test2.png", gocv.IMReadUnchanged)
    img := gocv.NewMat()
    gocv.MatchTemplate(haystack_img, needle_img, &img, gocv.TmCcoeffNormed, gocv.NewMat())

    window := gocv.NewWindow("Hello")

    for {

GoCV and OpenCV versions:

$ go run cmd/version/main.go
gocv version: 0.28.0
opencv lib version: 4.5.3

Go Version:
go version go1.17.2 windows/amd64

Windows 10

Purpose: Just trying to figure out how to use OpenCV in Golang.. To play around. I am trying to take in a smaller image and see if it’s located within another image.

Source: Windows Questions