我正在尝试使用Python和cv2、numpy、skimage等从黑白图像中删除“阴影线”(如果图像中存在“阴影线”)。本质上,我的图像可以有 1 或 2 条曲线,如下例所示。但每条线都有一条 1-5 像素外的阴影线,需要删除。我怎样才能在Python中做到这一点?
您可以通过使用修复或使用形态操作来删除它们。 我在下面为您提供了代码,希望这有帮助:),如果没有,请进一步询问我。
import cv2
import numpy as np
import matplotlib.pyplot as plt
image_path = '/mnt/data/image.png' #load the image from your designated location
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
_, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV) #thresholding the image to get a binary image
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #finding contours in the binary image
mask = np.zeros_like(binary_image) #creating a mask to sketch the contours
for contour in contours:
cv2.drawContours(mask, [contour], -1, 255, thickness=cv2.FILLED) #drawing the controus on the mask
kernel = np.ones((3, 3), np.uint8)
cleaned_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=2)#performing morphological operations to delete your shadow line between the two white lines
inpainted_image = cv2.inpaint(image, cleaned_mask, 3, cv2.INPAINT_TELEA) #inpainting the original image using the cleaned mask
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.subplot(1, 2, 2)
plt.title('Processed Image')
plt.imshow(inpainted_image, cmap='gray')
plt.show()
好吧,伙计,至少显示一些代码,伙计,你真的让我输入了整个内容:/