Reputation: 13
I'm working on a school-project.
I've been given the task to identify objects when seeing them with a camera.
I've chosen to try template matching
:
from matplotlib import pyplot as plt
import cv2
template = cv2.imread('./frog.jpg') # read image as template
cap = cv2.VideoCapture('./frog.mov') # simulate camera input
height = template.shape[0]
width = template.shape[1]
while True:
ret, frame = cap.read() # read the current frame
if ret is False:
break
res = cv2.matchTemplate(frame, template, cv2.TM_CCORR_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + width, top_left[1] + height)
cv2.rectangle(frame,top_left, bottom_right, 255, 2)
match_found = False
for i in res:
if i.any() > 0.9999:
print ('match found')
match_found = True
break
if match_found:
plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(frame,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.suptitle('TM_CCORR_NORMED')
plt.show()
The problem is, that almost everything is matching. How can I get this to work ?
Upvotes: 1
Views: 66
Reputation: 8270
If you would use frog and bear images you will get 100% objects matching. Following script detects these images on video and shows names of detected objects:
from matplotlib import pyplot as plt
import cv2
images = {'Frog': cv2.imread('frog.jpg'), 'Bear': cv2.imread('bear.jpg')}
cap = cv2.VideoCapture('frog.mov') # simulate camera input
while cap.isOpened():
ret, frame = cap.read() # read the current frame
if ret:
for name, image in images.items():
match = cv2.matchTemplate(frame, image, cv2.TM_CCOEFF_NORMED)
_, quality, _, max_loc = cv2.minMaxLoc(match)
if quality > 0.99:
top_left = max_loc
bottom_right = (top_left[0] + image.shape[1], top_left[1] + image.shape[0])
cv2.rectangle(frame, top_left, bottom_right, 255, 2)
plt.subplot(121), plt.imshow(match, cmap='gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(frame, cmap='gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.suptitle(name)
plt.show()
else:
break
So what I have on the end:
Upvotes: 1