Matching a shape and make background white

I am trying to match a text with opencv and make the background of the provided image white.

import cv2
import numpy as np

img_rgb = cv2.imread('./image/10.jpg')
template = cv2.imread('./image/matchedTxt_106.jpg')
w, h = template.shape[:-1]

res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
threshold = .5
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):  
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 0), -1)

cv2.imwrite('result1.png', img_rgb)

Currently, I get the following result:

enter image description here

Find here my google colab example:


I would like to replace the watermark with the following text:

enter image description here

Any suggestions what I am doing wrong? Furthermore how can I get the coordinates of where sub-image is located on this picture?

I appreciate your replies!

sorry to say so, but you wont solve it by asking around:

IF you want to use template-matching (bad idea to locate text already), you will need to understand your the copypasted code here:

1 Like

Thx for your reply!

I have created the code in google colab as quick prototype.

I am already trying to debug it to get a clearer picture.