Skip to content

Commit 717b2f4

Browse files
authored
Merge pull request opencv#9139 from Cartucho:improve_match_template_py
GSoC - Improving code match_template.py
2 parents a2f7132 + 7c65f7e commit 717b2f4

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

samples/python/tutorial_code/imgProc/match_template/match_template.py

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,82 +15,82 @@
1515

1616
def main(argv):
1717

18-
if (len(sys.argv) < 3):
19-
print 'Not enough parameters'
20-
print 'Usage:\nmatch_template_demo.py <image_name> <template_name> [<mask_name>]'
21-
return -1
22-
23-
## [load_image]
24-
global img
25-
global templ
26-
img = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)
27-
templ = cv2.imread(sys.argv[2], cv2.IMREAD_COLOR)
28-
29-
if (len(sys.argv) > 3):
30-
global use_mask
31-
use_mask = True
32-
global mask
33-
mask = cv2.imread( sys.argv[3], cv2.IMREAD_COLOR )
34-
35-
if ((img is None) or (templ is None) or (use_mask and (mask is None))):
36-
print 'Can\'t read one of the images'
37-
return -1
38-
## [load_image]
39-
40-
## [create_windows]
41-
cv2.namedWindow( image_window, cv2.WINDOW_AUTOSIZE )
42-
cv2.namedWindow( result_window, cv2.WINDOW_AUTOSIZE )
43-
## [create_windows]
44-
45-
## [create_trackbar]
46-
trackbar_label = 'Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED'
47-
cv2.createTrackbar( trackbar_label, image_window, match_method, max_Trackbar, MatchingMethod )
48-
## [create_trackbar]
49-
50-
MatchingMethod(match_method)
51-
52-
## [wait_key]
53-
cv2.waitKey(0)
54-
return 0
55-
## [wait_key]
18+
if (len(sys.argv) < 3):
19+
print 'Not enough parameters'
20+
print 'Usage:\nmatch_template_demo.py <image_name> <template_name> [<mask_name>]'
21+
return -1
22+
23+
## [load_image]
24+
global img
25+
global templ
26+
img = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)
27+
templ = cv2.imread(sys.argv[2], cv2.IMREAD_COLOR)
28+
29+
if (len(sys.argv) > 3):
30+
global use_mask
31+
use_mask = True
32+
global mask
33+
mask = cv2.imread( sys.argv[3], cv2.IMREAD_COLOR )
34+
35+
if ((img is None) or (templ is None) or (use_mask and (mask is None))):
36+
print 'Can\'t read one of the images'
37+
return -1
38+
## [load_image]
39+
40+
## [create_windows]
41+
cv2.namedWindow( image_window, cv2.WINDOW_AUTOSIZE )
42+
cv2.namedWindow( result_window, cv2.WINDOW_AUTOSIZE )
43+
## [create_windows]
44+
45+
## [create_trackbar]
46+
trackbar_label = 'Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED'
47+
cv2.createTrackbar( trackbar_label, image_window, match_method, max_Trackbar, MatchingMethod )
48+
## [create_trackbar]
49+
50+
MatchingMethod(match_method)
51+
52+
## [wait_key]
53+
cv2.waitKey(0)
54+
return 0
55+
## [wait_key]
5656

5757
def MatchingMethod(param):
5858

59-
global match_method
60-
match_method = param
61-
62-
## [copy_source]
63-
img_display = img.copy()
64-
## [copy_source]
65-
## [match_template]
66-
method_accepts_mask = (cv2.TM_SQDIFF == match_method or match_method == cv2.TM_CCORR_NORMED)
67-
if (use_mask and method_accepts_mask):
68-
result = cv2.matchTemplate(img, templ, match_method, None, mask)
69-
else:
70-
result = cv2.matchTemplate(img, templ, match_method)
71-
## [match_template]
72-
73-
## [normalize]
74-
cv2.normalize( result, result, 0, 1, cv2.NORM_MINMAX, -1 )
75-
## [normalize]
76-
## [best_match]
77-
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result, None)
78-
## [best_match]
79-
80-
## [match_loc]
81-
if (match_method == cv2.TM_SQDIFF or match_method == cv2.TM_SQDIFF_NORMED):
82-
matchLoc = minLoc
83-
else:
84-
matchLoc = maxLoc
85-
## [match_loc]
86-
87-
## [imshow]
88-
cv2.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
89-
cv2.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
90-
cv2.imshow(image_window, img_display)
91-
cv2.imshow(result_window, result)
92-
## [imshow]
93-
pass
59+
global match_method
60+
match_method = param
61+
62+
## [copy_source]
63+
img_display = img.copy()
64+
## [copy_source]
65+
## [match_template]
66+
method_accepts_mask = (cv2.TM_SQDIFF == match_method or match_method == cv2.TM_CCORR_NORMED)
67+
if (use_mask and method_accepts_mask):
68+
result = cv2.matchTemplate(img, templ, match_method, None, mask)
69+
else:
70+
result = cv2.matchTemplate(img, templ, match_method)
71+
## [match_template]
72+
73+
## [normalize]
74+
cv2.normalize( result, result, 0, 1, cv2.NORM_MINMAX, -1 )
75+
## [normalize]
76+
## [best_match]
77+
_minVal, _maxVal, minLoc, maxLoc = cv2.minMaxLoc(result, None)
78+
## [best_match]
79+
80+
## [match_loc]
81+
if (match_method == cv2.TM_SQDIFF or match_method == cv2.TM_SQDIFF_NORMED):
82+
matchLoc = minLoc
83+
else:
84+
matchLoc = maxLoc
85+
## [match_loc]
86+
87+
## [imshow]
88+
cv2.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
89+
cv2.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
90+
cv2.imshow(image_window, img_display)
91+
cv2.imshow(result_window, result)
92+
## [imshow]
93+
pass
9494

9595
if __name__ == "__main__":
96-
main(sys.argv[1:])
96+
main(sys.argv[1:])

0 commit comments

Comments
 (0)