尝试删除这个微弱的水印 - 但最终在 matplotlib 显示中出现蓝色色调

问题描述 投票:0回答:1

我正在开发一个项目,需要我从一些图片的背景中删除特定类型的水印(通常与干相关,因为这是一个教育项目。)现在,我尝试从下面的图片中删除水印以开始与:

这是我试图将其删除的图片

1

您可能需要放大才能看到它;但它说的是考试目标

但是,由于某种原因,我得到的输出虽然远非理想,但呈蓝色。我不知道为什么会发生这种情况,并且已经为此工作了几个小时;但找不到解决办法

2

我在谷歌colab上运行了以下代码;这确实有点(虽然非常糟糕)消除了水印。供您参考,我使用了 matplotlib、numpy 以及 cv2。

我真的很感谢有关如何解决此问题的一些帮助,因为我也在尝试将这个过程推广到多张图片。

import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files

uploaded = files.upload()
for filename in uploaded.keys():
    print(f'Uploaded file name: {filename}')
image_path = filename
image = cv2.imread(image_path)


if image is None:
    print(f"Error: Unable to load image at {image_path}")
else:
    print("Image loaded successfully")


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
enhanced = cv2.equalizeHist(gray)
adaptive_thresh = cv2.adaptiveThreshold(enhanced, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                        cv2.THRESH_BINARY_INV, 11, 2)
kernel = np.ones((3, 3), np.uint8)
morph = cv2.morphologyEx(adaptive_thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
mask = cv2.bitwise_not(morph)
result = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA)
plt.figure(figsize=(15, 15))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.subplot(1, 2, 2)
plt.title('Image without Watermark')
plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.show()
python opencv matplotlib image-processing watermark
1个回答
0
投票

正如 @TinoD 所正确建议的,问题是由于您将图像视为 RGB/BGR,但它还有一个 alpha 通道,在您的实现中会丢失。

import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files

uploaded = files.upload()
for filename in uploaded.keys():
    print(f'Uploaded file name: {filename}')
image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
image_original = image.copy()

if image is None:
    print(f"Error: Unable to load image at {image_path}")
else:
    print("Image loaded successfully")

# Split RGB and alpha channel
rgb   = image[:, :, :-1]
alpha = image[:, :, -1]

# Process the RGB channels
gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
enhanced = cv2.equalizeHist(gray)
adaptive_thresh = cv2.adaptiveThreshold(enhanced, 255, 
                                        cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                        cv2.THRESH_BINARY_INV, 11, 2)
kernel = np.ones((3, 3), np.uint8)
morph = cv2.morphologyEx(adaptive_thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
mask = cv2.bitwise_not(morph)
result = cv2.inpaint(rgb, mask, 3, cv2.INPAINT_TELEA)

# Re-apply the original alpha channel
result = cv2.cvtColor(result, cv2.COLOR_RGB2RGBA)
result[:, :, -1] = alpha

plt.figure(figsize=(15, 15))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(cv2.cvtColor(image_original, cv2.COLOR_BGRA2RGBA))
plt.subplot(1, 2, 2)
plt.title('Image without Watermark')
plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGRA2RGBA))
plt.show()

您将获得具有预期背景颜色的图像。

Original and processed image with expected bg color

如您所见,这解决了

matplotlib
数据着色的问题。

对于水印删除部分,正如 @Christoph Rackwitz 提到的,您将很难获得帮助来侵犯他人的 SO 版权。

© www.soinside.com 2019 - 2024. All rights reserved.