Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
717b2f49
Commit
717b2f49
authored
Aug 24, 2017
by
Alexander Alekhin
Committed by
GitHub
Aug 24, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9139 from Cartucho:improve_match_template_py
GSoC - Improving code match_template.py
parents
a2f71326
7c65f7ef
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
74 deletions
+74
-74
match_template.py
...on/tutorial_code/imgProc/match_template/match_template.py
+74
-74
No files found.
samples/python/tutorial_code/imgProc/match_template/match_template.py
View file @
717b2f49
...
@@ -15,82 +15,82 @@ max_Trackbar = 5
...
@@ -15,82 +15,82 @@ max_Trackbar = 5
def
main
(
argv
):
def
main
(
argv
):
if
(
len
(
sys
.
argv
)
<
3
):
if
(
len
(
sys
.
argv
)
<
3
):
print
'Not enough parameters'
print
'Not enough parameters'
print
'Usage:
\n
match_template_demo.py <image_name> <template_name> [<mask_name>]'
print
'Usage:
\n
match_template_demo.py <image_name> <template_name> [<mask_name>]'
return
-
1
return
-
1
## [load_image]
## [load_image]
global
img
global
img
global
templ
global
templ
img
=
cv2
.
imread
(
sys
.
argv
[
1
],
cv2
.
IMREAD_COLOR
)
img
=
cv2
.
imread
(
sys
.
argv
[
1
],
cv2
.
IMREAD_COLOR
)
templ
=
cv2
.
imread
(
sys
.
argv
[
2
],
cv2
.
IMREAD_COLOR
)
templ
=
cv2
.
imread
(
sys
.
argv
[
2
],
cv2
.
IMREAD_COLOR
)
if
(
len
(
sys
.
argv
)
>
3
):
if
(
len
(
sys
.
argv
)
>
3
):
global
use_mask
global
use_mask
use_mask
=
True
use_mask
=
True
global
mask
global
mask
mask
=
cv2
.
imread
(
sys
.
argv
[
3
],
cv2
.
IMREAD_COLOR
)
mask
=
cv2
.
imread
(
sys
.
argv
[
3
],
cv2
.
IMREAD_COLOR
)
if
((
img
is
None
)
or
(
templ
is
None
)
or
(
use_mask
and
(
mask
is
None
))):
if
((
img
is
None
)
or
(
templ
is
None
)
or
(
use_mask
and
(
mask
is
None
))):
print
'Can
\'
t read one of the images'
print
'Can
\'
t read one of the images'
return
-
1
return
-
1
## [load_image]
## [load_image]
## [create_windows]
## [create_windows]
cv2
.
namedWindow
(
image_window
,
cv2
.
WINDOW_AUTOSIZE
)
cv2
.
namedWindow
(
image_window
,
cv2
.
WINDOW_AUTOSIZE
)
cv2
.
namedWindow
(
result_window
,
cv2
.
WINDOW_AUTOSIZE
)
cv2
.
namedWindow
(
result_window
,
cv2
.
WINDOW_AUTOSIZE
)
## [create_windows]
## [create_windows]
## [create_trackbar]
## [create_trackbar]
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'
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'
cv2
.
createTrackbar
(
trackbar_label
,
image_window
,
match_method
,
max_Trackbar
,
MatchingMethod
)
cv2
.
createTrackbar
(
trackbar_label
,
image_window
,
match_method
,
max_Trackbar
,
MatchingMethod
)
## [create_trackbar]
## [create_trackbar]
MatchingMethod
(
match_method
)
MatchingMethod
(
match_method
)
## [wait_key]
## [wait_key]
cv2
.
waitKey
(
0
)
cv2
.
waitKey
(
0
)
return
0
return
0
## [wait_key]
## [wait_key]
def
MatchingMethod
(
param
):
def
MatchingMethod
(
param
):
global
match_method
global
match_method
match_method
=
param
match_method
=
param
## [copy_source]
## [copy_source]
img_display
=
img
.
copy
()
img_display
=
img
.
copy
()
## [copy_source]
## [copy_source]
## [match_template]
## [match_template]
method_accepts_mask
=
(
cv2
.
TM_SQDIFF
==
match_method
or
match_method
==
cv2
.
TM_CCORR_NORMED
)
method_accepts_mask
=
(
cv2
.
TM_SQDIFF
==
match_method
or
match_method
==
cv2
.
TM_CCORR_NORMED
)
if
(
use_mask
and
method_accepts_mask
):
if
(
use_mask
and
method_accepts_mask
):
result
=
cv2
.
matchTemplate
(
img
,
templ
,
match_method
,
None
,
mask
)
result
=
cv2
.
matchTemplate
(
img
,
templ
,
match_method
,
None
,
mask
)
else
:
else
:
result
=
cv2
.
matchTemplate
(
img
,
templ
,
match_method
)
result
=
cv2
.
matchTemplate
(
img
,
templ
,
match_method
)
## [match_template]
## [match_template]
## [normalize]
## [normalize]
cv2
.
normalize
(
result
,
result
,
0
,
1
,
cv2
.
NORM_MINMAX
,
-
1
)
cv2
.
normalize
(
result
,
result
,
0
,
1
,
cv2
.
NORM_MINMAX
,
-
1
)
## [normalize]
## [normalize]
## [best_match]
## [best_match]
minVal
,
maxVal
,
minLoc
,
maxLoc
=
cv2
.
minMaxLoc
(
result
,
None
)
_minVal
,
_
maxVal
,
minLoc
,
maxLoc
=
cv2
.
minMaxLoc
(
result
,
None
)
## [best_match]
## [best_match]
## [match_loc]
## [match_loc]
if
(
match_method
==
cv2
.
TM_SQDIFF
or
match_method
==
cv2
.
TM_SQDIFF_NORMED
):
if
(
match_method
==
cv2
.
TM_SQDIFF
or
match_method
==
cv2
.
TM_SQDIFF_NORMED
):
matchLoc
=
minLoc
matchLoc
=
minLoc
else
:
else
:
matchLoc
=
maxLoc
matchLoc
=
maxLoc
## [match_loc]
## [match_loc]
## [imshow]
## [imshow]
cv2
.
rectangle
(
img_display
,
matchLoc
,
(
matchLoc
[
0
]
+
templ
.
shape
[
0
],
matchLoc
[
1
]
+
templ
.
shape
[
1
]),
(
0
,
0
,
0
),
2
,
8
,
0
)
cv2
.
rectangle
(
img_display
,
matchLoc
,
(
matchLoc
[
0
]
+
templ
.
shape
[
0
],
matchLoc
[
1
]
+
templ
.
shape
[
1
]),
(
0
,
0
,
0
),
2
,
8
,
0
)
cv2
.
rectangle
(
result
,
matchLoc
,
(
matchLoc
[
0
]
+
templ
.
shape
[
0
],
matchLoc
[
1
]
+
templ
.
shape
[
1
]),
(
0
,
0
,
0
),
2
,
8
,
0
)
cv2
.
rectangle
(
result
,
matchLoc
,
(
matchLoc
[
0
]
+
templ
.
shape
[
0
],
matchLoc
[
1
]
+
templ
.
shape
[
1
]),
(
0
,
0
,
0
),
2
,
8
,
0
)
cv2
.
imshow
(
image_window
,
img_display
)
cv2
.
imshow
(
image_window
,
img_display
)
cv2
.
imshow
(
result_window
,
result
)
cv2
.
imshow
(
result_window
,
result
)
## [imshow]
## [imshow]
pass
pass
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
(
sys
.
argv
[
1
:])
main
(
sys
.
argv
[
1
:])
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment