MatchTemplates returns unintellegable results

I’m trying to use MatchTemplates but the results but all 6 modes output unintellegable results. I setup a demonstration like the one in the documentation but I can’t get similar results. I wrote this in Go using the Cgo bindings “gocv.io/x/gocv”. I get the same behavior when I implemented this in python.

Code:

package main

import (
	"fmt"
	"time"

	_ "image/jpeg"
	_ "image/png"

	"gocv.io/x/gocv"
)

func main() {
	start := time.Now()
	defer func() {
		diff := time.Since(start)
		fmt.Println("Elapsed time", diff)
	}()

	subjectMat := gocv.IMRead("./20180521_082936.jpg", gocv.IMReadGrayScale)
	defer subjectMat.Close()

	templateMat := gocv.IMRead("./template.jpg", gocv.IMReadGrayScale)
	defer templateMat.Close()

	match := func(mode gocv.TemplateMatchMode) {
		matchMat := gocv.NewMat()
		defer matchMat.Close()

		maskMat := gocv.NewMat()
		defer maskMat.Close()
		gocv.MatchTemplate(subjectMat, templateMat, &matchMat, mode, maskMat)
		gocv.IMWrite(fmt.Sprintf("match%s.jpg", mode), matchMat)
	}

	match(gocv.TmSqdiff)
	match(gocv.TmSqdiffNormed)
	match(gocv.TmCcorr)
	match(gocv.TmCcorrNormed)
	match(gocv.TmCcoeffNormed)
	match(gocv.TmCcoeff)
	match(gocv.TmCcoeffNormed)
}

Images:

please explain: unintellegable . what does it mean ?
your code also seems incomplete, missing the actual min / max localization from the match result.

tutorial code

I was expecting to see a similar greyscale map to what I saw in the tutorial but each one resembling my images. Instead some of the results came back as all black or all white and the ccoeff was the only one not to be all black were all white, but there was still no way to make sense of where the template matched to the image. I’m aware of the minmaxloc step, but it doesn’t make sense to do it now because the template match data doesn’t make any sense.

1 Like

did you expect the result to be scaled into the range of 0 to 255? it’s not. it’s scaled (or not scaled) according to the equations given in the documentation.

if you need scaling to a specific range, you do that explicitly.

I did expect the outputs to be between 0-255 because I guess I didn’t see where in the docs was a range specified. I guess I should have gotten that from the math equations. When I multiplied each pixel by 255 I got an output I can recognize. Thank you for pointing that out, I’ve resolved the problem <3

1 Like