Need to find and replace some portions in an image, with other images.
The original image is shown below. I want to find and replace:
blue squares to blue triangles
orange squares to orange octagons
the image files are in place as:
original.jpg, blue square.jpg, orange square.jpg, blue triangle.jpg, orange octagon.jpg
using cv2, I can find the locations of the target images.
import cv2
import numpy as np
img_original = cv2.imread("C:\\original.jpg")
img_to_replace_0 = cv2.imread("C:\\blue square.jpg")
img_to_replace_1 = cv2.imread("C:\\orange square.jpg")
img_overlay_0 = cv2.imread("C:\\blue triangle.jpg")
img_overlay_1 = cv2.imread("C:\\orange octagon.jpg")
res_0 = cv2.matchTemplate(img_original, img_to_replace_0, cv2.TM_CCOEFF_NORMED)
res_1 = cv2.matchTemplate(img_original, img_to_replace_1, cv2.TM_CCOEFF_NORMED)
threshold = 0.80
loc_0 = np.where (res_0 >= threshold)
loc_1 = np.where (res_1 >= threshold)
bl_0 = list(loc_0)
bl_1 = list(loc_1)
print bl_0
print bl_1
the output is:
[array([106, 294, 477]), array([17, 18, 21])]
[array([ 22, 210, 393]), array([16, 17, 20])]
What’s the best way to proceed further? Is CV2 the best tool for this case? Thank you.






