# -*- coding: utf-8 -*-
import numpy as np
from numpy import NaN
import cv2
def __make_mask__(image):
hue_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
low_range = np.array([140, 100, 90])
high_range = np.array([185, 255, 255])
th = cv2.inRange(hue_image, low_range, high_range)
index1 = th == 255
mask_img = np.zeros(image.shape, np.uint8)
mask_img[:, :] = (255, 255, 255)
mask_img[index1] = image[index1]
cv2.imshow('mask_img',mask_img)
cv2.waitKey(0)
return image,mask_img
def Remove_watermark(image):
image, mask = __make_mask__(image)
h, w = image.shape[:2]
image = [image[:, :, 0], image[:, :, 1], image[:, :, 2]]
mask = [mask[:, :, 0], mask[:, :, 1], mask[:, :, 2]]
index = [0,1,2]
array_255 = np.full((h, w), 255.0, dtype=np.float32)
result = []
for i,array,mask in zip(index,image,mask):
reverse_val = array_255-array
value = array_255-reverse_val * 256 / mask
value = np.nan_to_num(value)
value = np.where(0 < value, 0,value) # 防止像素溢出
value = np.where(value > 255, 255,value) # 防止像素溢出
value.astype(np.int16)
cv2.imshow('img'+str(i),value)
cv2.waitKey(0)
result.append(value)
return result
if __name__ == '__main__':
img_path = 'C:\\Users\\xxxxx\\Desktop\\piaoju/201920100013253001_30302_01_.jpg'
image = cv2.imread(img_path)
image = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
result = Remove_watermark(image)
result_img = cv2.merge([result[0],result[1],result[2]])
cv2.imshow('img',image)
cv2.imshow('result_img',result_img)
cv2.waitKey(0)
cv2.destroyAllWindows()