123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import cv2
- class MaskWatermark:
- @classmethod
- def find_watermark(cls, image_path):
- """
- 基于OpenCV自动识别水印并获取其位置
- :param image_path:水印
- :return:watermark_area
- """
-
- image = cv2.imread(image_path)
-
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-
- edges = cv2.Canny(gray, 100, 200)
-
- lines = cv2.HoughLinesP(edges, 1, cv2.pi / 180, threshold=100, minLineLength=100, maxLineGap=10)
-
- watermark_lines = []
- if lines is not None:
- for line in lines:
- x1, y1, x2, y2 = line[0]
-
-
- if abs(y2 - y1) < 5 and abs(x2 - x1) > 50:
- watermark_lines.append(line)
-
- if len(watermark_lines) > 1:
- x_coords = [line[0][0] for line in watermark_lines] + [line[0][2] for line in watermark_lines]
- y_coords = [line[0][1] for line in watermark_lines] + [line[0][3] for line in watermark_lines]
- min_x = min(x_coords)
- max_x = max(x_coords)
- min_y = min(y_coords)
- max_y = max(y_coords)
- watermark_area = (min_x, min_y, max_x - min_x, max_y - min_y)
- else:
- watermark_area = None
- return watermark_area
- @classmethod
- def mask_watermark(cls, input_path, output_path, watermark_area):
-
- video = cv2.VideoCapture(input_path)
-
- width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
- height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
-
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
- output = cv2.VideoWriter(output_path, fourcc, 30.0, (width, height))
- while True:
- ret, frame = video.read()
- if not ret:
- break
-
- x, y, w, h = watermark_area
- frame[y:y + h, x:x + w] = 0
-
- output.write(frame)
-
- video.release()
- output.release()
- print("成功去除水印,并保存为", output_path)
- @classmethod
- def remove_watermark(cls, video_path, output_path):
-
- video = cv2.VideoCapture(video_path)
-
- width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
- height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
-
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
- output = cv2.VideoWriter(output_path, fourcc, 30.0, (width, height))
-
- ret, background = video.read()
- if not ret:
- print("无法读取背景帧")
- return
- while True:
- ret, frame = video.read()
- if not ret:
- break
-
- diff = cv2.absdiff(frame, background)
-
- gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
-
- _, threshold = cv2.threshold(gray, 25, 255, cv2.THRESH_BINARY)
-
- kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
- dilated = cv2.dilate(threshold, kernel, iterations=3)
-
- contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
-
- for contour in contours:
-
- if cv2.contourArea(contour) > threshold_area:
-
- cv2.drawContours(frame, [contour], -1, (0, 0, 0), cv2.FILLED)
-
- output.write(frame)
-
- video.release()
- output.release()
- if __name__ == "__main__":
-
- image_path = 'image.jpg'
- watermark_area = MaskWatermark.find_watermark(image_path)
- print("水印区域的位置和大小:", watermark_area)
-
- input_path = 'input.mp4'
- output_path = 'output.mp4'
- watermark_area = (100, 100, 200, 200)
- MaskWatermark.mask_watermark(input_path, output_path, watermark_area)
-
- video_path = 'video.mp4'
- output_path = 'output.mp4'
- threshold_area = 1000
- MaskWatermark.remove_watermark(video_path, output_path)
- print("成功去除水印,并保存为", output_path)
- pass
|