Home > Mobile >  Removing watermark using opencv in python
Removing watermark using opencv in python

Time:01-08

I have used opencv and python to remove watermark from 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 try 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 to achieve it with my current code any help is appreciated.

actually i can't post any pics so here's the link

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)

  • Related