Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV remove watermark

I am trying to remove this watermark, I have tried a thousand ways but none of them work, any ideas, here is the code

if __name__ == '__main__':

    img = cv2.imread("doc.jpg")
    cv2.imshow("Image With Water Mark", img)

    img1 = cv2.imread("doc.jpg")
    _, thresh = cv2.threshold(img1, 150, 255, cv2.THRESH_BINARY)
    #cv2.imshow('Image Without Water Mark', thresh)
    cv2.imshow('Image Without Water Mark', thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

enter image description here

This is the result:

enter image description here

Some ideas of how solve this

like image 954
bomboerick Avatar asked Dec 06 '25 07:12

bomboerick


2 Answers

This does a pretty good job. It may need additional smoothing. The text is all pretty much black and white, but the watermark is gray. So, convert everything less than 60% black to white.

from PIL import Image

x = Image.open('vodRX.jpg')
x1 = x.convert('L')
x2 = x1.point( lambda p: (p > 100) * 255 )
x2.save('after.jpg')

However, be aware that what you're doing may be illegal. They add these watermarks for a reason.

result

like image 121
Tim Roberts Avatar answered Dec 08 '25 20:12

Tim Roberts


I am trying to remove this watermark, I have tried a thousand ways but none of them work

The problem can be fixed.

You need to add more algorithms.

  • cv2.getStructuringElement()
  • cv2.morphologyEx()
  • cv2.subtract()
  • cv2.threshold()

Snippet:

import cv2
import numpy as np


def wartermark_remove(filename):
    img = cv2.imread(filename)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    for i in range(5):
        kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                            (2 * i + 1, 2 * i + 1))
        bg = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel2)
        bg = cv2.morphologyEx(bg, cv2.MORPH_OPEN, kernel2)

        dif = cv2.subtract(bg, gray)

        bw = cv2.threshold(dif, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
        dark = cv2.threshold(bg, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

        darkpix = gray[np.where(dark > 0)]

        darkpix = cv2.threshold(darkpix, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

        bw[np.where(dark > 0)] = darkpix.T

    cv2.imshow('Watermark Image', bw)
    cv2.waitKey(0)


wartermark_remove('w.jpg')

Screenshot:

enter image description here