Python Forum
Replacing part of a source image with the matching template image
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing part of a source image with the matching template image
#1
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.

### 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
Reply
#2
Your code should be something like the following (instead of line No. 28):

img_rgb[pt[0]:pt[0]+temaplate.shape[0], pt[1]:pt[1]+temaplate.shape[1]] = temaplate
However, I didn't try it...
Reply
#3
(Jul-17-2020, 01:04 AM)scidam Wrote: Your code should be something like the following (instead of line No. 28):

img_rgb[pt[0]:pt[0]+temaplate.shape[0], pt[1]:pt[1]+temaplate.shape[1]] = temaplate
However, I didn't try it...

Hi scidam - thank you very much for your help!

I've been having fun trying out the code and it works

I'm just learning python so it was great to have your code to practice with
Best wishes
David
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  image does not show with pillow Mohamad_afeff_nasser 5 1,294 Apr-13-2025, 07:53 PM
Last Post: snippsat
  Placing image button on top of background image Sintek 1 500 Mar-02-2025, 05:36 PM
Last Post: Sintek
  Problem When using canny edge detection,black image returned rickyw2777 1 456 Feb-17-2025, 03:22 AM
Last Post: rickyw2777
  Why it gives me a black image and libpng warning: iCCP rickyw2777 1 462 Feb-16-2025, 08:26 PM
Last Post: rickyw2777
  QR code creation with image Raja31 1 819 Jan-15-2025, 11:17 AM
Last Post: Larz60+
Photo image generation with text style Belialhun 0 653 Oct-08-2024, 01:53 PM
Last Post: Belialhun
  Product Image Download Help Required pythonustasi 5 1,352 Jul-21-2024, 08:12 PM
Last Post: snippsat
  tkinter photo image problem jacksfrustration 5 3,518 Jun-27-2024, 12:06 AM
Last Post: AdamHensley
Photo image error pyc0de 2 1,805 Mar-23-2024, 06:20 PM
Last Post: pyc0de
  Using OpenCV and image path is invalid AudunNilsen 5 1,984 Mar-18-2024, 05:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020