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 and replace the text with a black rectangle.

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:

Notebook

The initial image 10.jpg looks like the following:

enter image description here

My template matchedTxt_106.jpg:

enter image description here

I would like to get the following result:

enter image description here

On the result image the location of the watermark is a black textbox and the background of the result image is white. The result image should have the same size as the previous image.

Any suggestions what I am doing wrong? Furthermore how can I get the coordinates of where the image text located on initial image?

I appreciate your replies!

Answer

Your main issue is replacing width and height.

Replace w, h = template.shape[:-1] with:

h, w = template.shape[:-1]

In NumPy array shape, the height comes first.


Here is a code sample that results a black rectangle on white background:

import cv2
import numpy as np


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

img_bw = np.full(img_rgb.shape[:-1], 255, np.uint8)  # Initialize white image

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.rectangle(img_bw, pt, (pt[0] + w, pt[1] + h), 0, -1)  # Draw black (filled) rectangle on img_bw

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

Result:

result_bw.png:
enter image description here

result1.png:
enter image description here