Template Matching
비록 Self Driving Car에서의 Object Detection에서는 차 형태나 색깔의 다양성으로 인해 쓰이지는 않으나 다른 부분에서 참고할만하다.
OpenCV
Udacity
Find Matches Implementation
# Define a function to search for template matches
# and return a list of bounding boxes
def find_matches(img, template_list):
\# Define an empty list to take bbox coords
bbox\_list = \[\]
\# Define matching method
\# Other options include: cv2.TM\_CCORR\_NORMED', 'cv2.TM\_CCOEFF', 'cv2.TM\_CCORR',
\# 'cv2.TM\_SQDIFF', 'cv2.TM\_SQDIFF\_NORMED'
method = cv2.TM\_CCOEFF\_NORMED
\# Iterate through template list
for temp in template\_list:
\# Read in templates one by one
tmp = mpimg.imread\(temp\)
\# Use cv2.matchTemplate\(\) to search the image
result = cv2.matchTemplate\(img, tmp, method\)
\# Use cv2.minMaxLoc\(\) to extract the location of the best match
min\_val, max\_val, min\_loc, max\_loc = cv2.minMaxLoc\(result\)
\# Determine a bounding box for the match
w, h = \(tmp.shape\[1\], tmp.shape\[0\]\)
if method in \[cv2.TM\_SQDIFF, cv2.TM\_SQDIFF\_NORMED\]:
top\_left = min\_loc
else:
top\_left = max\_loc
bottom\_right = \(top\_left\[0\] + w, top\_left\[1\] + h\)
\# Append bbox position to list
bbox\_list.append\(\(top\_left, bottom\_right\)\)
\# Return the list of bounding boxes
return bbox\_list