Removing watermark using opencv in python

Question:

I have used OpenCV and Python to remove a watermark from an image using code below.

import cv2
import numpy

src = cv2.imread('src.jpg')
mask = cv2.imread('mask.jpg')
save = numpy.zeros(src.shape, numpy.uint8) 

for row in range(src.shape[0]):
    for col in range(src.shape[1]):
        for channel in range(src.shape[2]):
            if mask[row, col, channel] == 0:
                val = 0
            else:
                reverse_val = 255 - src[row, col, channel]
                val = 255 - reverse_val * 256 / mask[row, col, channel]
                if val < 0: val = 0

            save[row, col, channel] = val

cv2.imwrite('result.jpg', save)

Here are the src, mask files and what I get from the code

I tried to neutralize the original image watermark with a white background inverse watermark image.

But now it makes no progress and I don’t what happened to it.

I googled a bit and found some info about it, but in my case I have a mask.
How can I achieve it with my current code? Any help is appreciated.

source
source

mask
mask

result
enter image description here

Asked By: wwwww135781012

||

Answers:

ok, I didn’t understand your algorithm of "neutralizing":

reverse_val = 255 - src[row, col, channel]
val = 255 - reverse_val * 256 / mask[row, col, channel]

But I can tell you, why it doesn’t work. When you read the mask image mask = cv2.imread('mask.jpg') you read it as usual image with white background and the watermark. You should make an binary image from this, so you will undestand which pixels you need to neutralize:

threshed = cv2.inRange(mask, 0, 254)

And you will get something like this:
binary mask
So now you need to neutralize the "white" pixels of mask on the original image.
(BUT! You should remember that binary image and grayscale image have only one channel).
As I said before: I didn’t understand your algorithm of "neutralizing" because when I use it – I get an image like this:
enter image description here
So I recommend you just to make all the pixels white:
enter image description here
And the full code:

import cv2
import numpy


src = cv2.imread("src.jpg")
mask = cv2.imread("mask.jpg", cv2.IMREAD_GRAYSCALE)
save = numpy.zeros(src.shape, numpy.uint8)

threshed = cv2.inRange(mask, 0, 254)


def get_reversed(values: tuple) -> tuple:
    return 255 - values[0], 255 - values[1], 255 - values[2]


def get_processed(values: tuple, mask: int) -> tuple:
    return 255 - values[0] * 256 / mask, 255 - values[1] * 256 / mask, 255 - values[2] * 256 / mask


for row in range(src.shape[0]):
    for col in range(src.shape[1]):
        if threshed[row, col] != 0:
            # save[row, col] = get_reversed(src[row, col])
            save[row, col] = (255, 255, 255)
            # reverse_val = get_reversed(src[row, col])
            # val = get_processed(reverse_val, mask[row, col])
        else:
            save[row, col] = src[row, col]


cv2.imwrite('result.jpg', save)

Answered By: crackanddie
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.