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
Photo image error pyc0de 2 281 Mar-23-2024, 06:20 PM
Last Post: pyc0de
  Using OpenCV and image path is invalid AudunNilsen 5 562 Mar-18-2024, 05:28 PM
Last Post: snippsat
  Count image's colors very fast flash77 18 1,572 Mar-05-2024, 06:12 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,899 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  error "cannot identify image file" part way through running hatflyer 0 679 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Mirror Video Image in realtime makingwithheld 1 436 Oct-30-2023, 02:45 PM
Last Post: Larz60+
  howto get size of a ctk image? janeik 2 875 Oct-03-2023, 03:49 AM
Last Post: janeik
  Use of PIL.Image nafshar 12 2,132 Sep-07-2023, 11:02 PM
Last Post: nafshar
Question image manipulation (cropping and MetaData) SpongeB0B 4 1,164 Jul-03-2023, 06:35 PM
Last Post: SpongeB0B
  Save image from outlook email cubangt 1 702 Jun-07-2023, 06:52 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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