Jul-16-2020, 11:39 AM
Hello,
Replacing part of a source image with the matching template image
I have been using this code which shows when a small template image (e.g. 'images/bubbles_template.png') matches to part of a bigger source image (e.g. 'images/bubbles.png').
When there is a close enough match, a rectangle is drawn showing the outline of the template image on the source image.
Instead of drawing a rectangle, I need to paste the template image into that position, replacing the matching part of the source image.
Can anyone suggest code for pasting the template image into the source image when there is a match please?
Thanks
Regards
David
Replacing part of a source image with the matching template image
I have been using this code which shows when a small template image (e.g. 'images/bubbles_template.png') matches to part of a bigger source image (e.g. 'images/bubbles.png').
When there is a close enough match, a rectangle is drawn showing the outline of the template image on the source image.
### Template matching - multiple objects #For multiple occurances, cv2.minMaxLoc() won’t give all the locations #So we need to set a threshold import cv2 import numpy as np from matplotlib import pyplot as plt img_rgb = cv2.imread('images/bubbles.png') img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template = cv2.imread('images/bubbles_template.png',0) h, w = template.shape[::] res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) plt.imshow(res, cmap='gray') threshold = 0.8 #Pick only values above 0.8. For TM_CCOEFF_NORMED, larger values = good fit. loc = np.where( res >= threshold) #Outputs 2 arrays. Combine these arrays to get x,y coordinates - take x from one array and y from the other. #Reminder: ZIP function is an iterator of tuples where first item in each iterator is paired together, #then the second item and then third, etc. for pt in zip(*loc[::-1]): #-1 to swap the values as we assign x and y coordinate to draw the rectangle. #Draw rectangle around each object. We know the top left (pt), draw rectangle to match the size of the template image. cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2) #Red rectangles with thickness 2. #cv2.imwrite('images/template_matched.jpg', img_rgb) cv2.imshow("Matched image", img_rgb) cv2.waitKey() cv2.destroyAllWindows()Source: https://github.com/bnsreenu/python_for_m...atching.py
Instead of drawing a rectangle, I need to paste the template image into that position, replacing the matching part of the source image.
Can anyone suggest code for pasting the template image into the source image when there is a match please?
Thanks
Regards
David