Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to remove text from image (not inpaiting but just solid color over the text)

Hi have the following python code that find the text in a image, and uses cv2.inpaint to remove that text by covering with generated background, but I just want to put solid color over the text instead generating the background

this is what I want to achieve
enter image description here

import keras_ocr
import cv2
import math
import numpy as np


def inpaint_text(img_path, pipeline):
    img = keras_ocr.tools.read(img_path) 
    
    prediction_groups = pipeline.recognize([img])
    inpainted_img = ""
    
    mask = np.zeros(img.shape[:2], dtype="uint8")
    for box in prediction_groups[0]:
        x0, y0 = box[1][0]
        x1, y1 = box[1][1] 
        x2, y2 = box[1][2]
        x3, y3 = box[1][3] 
        
        points = np.array([[x0, y0], [x1, y1], [x2, y2],[x3, y3]])
        
        cv2.fillPoly(mask,np.int32([points]), 255)
        inpainted_img = cv2.inpaint(img, mask, 0, cv2.INPAINT_NS)
            
    return(inpainted_img)

pipeline = keras_ocr.pipeline.Pipeline()

img_text_removed = inpaint_text('image.png', pipeline)


plt.imshow(img_text_removed)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite('result.png', cv2.cvtColor(img_text_removed, cv2.COLOR_BGR2RGB))```

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import keras_ocr
import cv2
import numpy as np
import matplotlib.pyplot as plt

def inpaint_text(img_path, pipeline):
    img = keras_ocr.tools.read(img_path) 
    
    prediction_groups = pipeline.recognize([img])
    inpainted_img = img.copy()  # Create a copy of the original image
    
    mask = np.zeros(img.shape[:2], dtype="uint8")
    color = (0, 0, 255)  # Define the color you want to use for covering the text
    
    for box in prediction_groups[0]:
        x0, y0 = box[1][0]
        x1, y1 = box[1][1] 
        x2, y2 = box[1][2]
        x3, y3 = box[1][3] 
        
        points = np.array([[x0, y0], [x1, y1], [x2, y2], [x3, y3]])
        
        cv2.fillPoly(inpainted_img, np.int32([points]), color)  # Fill the area with the specified color
            
    return inpainted_img

pipeline = keras_ocr.pipeline.Pipeline()

img_text_removed = inpaint_text('image.png', pipeline)

plt.imshow(cv2.cvtColor(img_text_removed, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

cv2.imwrite('result.png', img_text_removed)

In this code, the primary change is that we directly modify the inpainted_img using the cv2.fillPoly() function to fill the text area with the specified color (color = (0, 0, 255) in this case). The cv2.cvtColor() function is not needed when saving the image, as we’re already working with the BGR format. Additionally, I’ve added plt.axis('off') to turn off the axis display when showing the image with matplotlib.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading