Skip to content

match_template Python example has coordinates backward from shape #27292

@shirriff

Description

@shirriff

Describe the doc issue

The sample program at https://docs.opencv.org/5.0.0-alpha/de/da9/tutorial_template_matching.html draws the wrong rectangle on the image, reversing the width and height. The problem is that it adds shape[0] to the first (X) coordinate and shape[1] to the second (Y) coordinate, but numpy.shape has the height first and the width second, the opposite order.

Fix suggestion

The problem is in the demo program at https://github.com/opencv/opencv/blob/5.x/samples/python/tutorial_code/imgProc/match_template/match_template.py

cv.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
    cv.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )

should change to

cv.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[1], matchLoc[1] + templ.shape[0]), (0,0,0), 2, 8, 0 )
    cv.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[1], matchLoc[1] + templ.shape[0]), (0,0,0), 2, 8, 0 )

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions