I'm a new in OpenCV and I whould like to find a solution to find multiple images with text on a some image. In the future I need that items for recognition.
First of all I've got a frame of images for template of searching. It looks like frame with transparent center. I've tried a lot of samples to match the template but they give only one result of searching: just first or second item is found, but I need all of them.
Please help me to find ways to solve the issue.
frame template:
scene:
code:
import cv2
import numpy as np
method = cv2.TM_CCOEFF_NORMED
threshold = 0.90
img_main = cv2.imread('images/garden.jpg')
template = cv2.imread('images/frame_trans.png', cv2.IMREAD_GRAYSCALE)
template_gray = template
img_main_gray = cv2.cvtColor(img_main, cv2.COLOR_BGR2GRAY)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_main_gray, template_gray, method)
cv2.normalize(res, res, 0., 1., cv2.NORM_MINMAX)
cv2.threshold(res, threshold, 1, cv2.THRESH_TOZERO)
i = 0
while i < 100:
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
if max_val > threshold:
print(top_left)
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img_main, top_left, bottom_right, (0, 0, 255), 2)
cv2.floodFill(img_main_gray, None, top_left, 0, 0.1, 1.0)
else:
break
i += 1
cv2.imwrite('sample6_output.png', img_main)
cv2.imshow('sample6', img_main)
cv2.waitKey()
Result of the script is here...
PyDev console: starting.
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
runfile('D:/MyProjects/PyHeroRecognition/sample6.py', wdir='D:/MyProjects/PyHeroRecognition')
(71, 45)
(71, 45)
(71, 45)
(71, 45)
(71, 45)
(71, 45)
(71, 45)
(71, 45)
(71, 45)
(71, 45)
Result image: