Alexander Popov
Alexander Popov

Reputation: 404

How to remove text stamps from images?

I have picture like this:

enter image description here

It has text stamps randomly distributed throughout the image file. Some aspect to keep in mind about the image are;

So my question is;

  1. How do I find this text stamps? I'm guessing, maybe template matching with tolerance could help?
  2. Although even if I found the exact location of the text, how do I get rid it? I could try to figure out the random background and do something like I've mentioned as follows;

    • Get the bounding box of the text stamp contour.
    • Then take all pixels outside of the contour.
    • Removing the contour and filling with random pixels from previous step and adding some blur should do the trick as I'm expecting.

Upvotes: 0

Views: 1597

Answers (1)

Shubham Jaiswal
Shubham Jaiswal

Reputation: 359

The following code removes the stamp from your image:

inp_img = cv2.imread('stamp.jpg',cv2.IMREAD_GRAYSCALE)
th,inp_img_thresh = cv2.threshold(255-inp_img,220,255,cv2.THRESH_BINARY)
dilate = cv2.dilate(inp_img_thresh,np.ones((5,5),np.uint8))
canny = cv2.Canny(dilate,0,255)
_,contours,_ = 
cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
test_img = inp_img.copy()
for c in contours:
    (x, y, w, h) = cv2.boundingRect(c)
    #print(x,y,w,h,test_img[y+h//2,x-w])
    test_img[y+3:y-2+h,x+3:x+w] = 240 #test_img[y+h//2,x-w]

cv2.imwrite("stamp_removed.jpg",test_img)
cv2.imshow("input image",inp_img)
cv2.imshow("threshold",inp_img_thresh)
cv2.imshow("output image",test_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output Image With The Stamp Removed

Upvotes: 2

Related Questions