答案 0 :(得分:3)
这是一种结合使用过滤技术和遮罩的方法。
这是结果
import cv2
import numpy as np
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 15)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,3)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
erode = cv2.erode(thresh, kernel, iterations=1)
dilate = cv2.dilate(erode, kernel, iterations=3)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
mask = np.zeros(image.shape, dtype=np.uint8)
for c in cnts:
area = cv2.contourArea(c)
if area > 850:
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
mask = cv2.dilate(mask, kernel, iterations=1)
image = 255 - image
result = 255 - cv2.bitwise_and(mask, image)
cv2.imshow('result', result)
cv2.waitKey(0)
第二种方法
这是另一种方法,与第一种方法相同,但是使用专用的水平和垂直内核进行过滤。这种方法可能更健壮。我们没有使用猜测和检查形态变换的方法,而是使用专用的内核过滤掉了水平/垂直网格线。
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,5))
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,1))
这是经过每个内核之后的掩码结果
结果大致相同,但稍微干净一点:)
import cv2
import numpy as np
image = cv2.imread('1.png',0)
blur = cv2.GaussianBlur(image, (5,5), 0)
thresh = cv2.threshold(blur, 130, 255, cv2.THRESH_BINARY_INV)[1]
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,5))
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel)
remove_vertical = cv2.morphologyEx(remove_horizontal, cv2.MORPH_OPEN, horizontal_kernel)
cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
mask = np.ones(image.shape, dtype=np.uint8)
for c in cnts:
area = cv2.contourArea(c)
if area > 50:
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
mask = cv2.dilate(mask, kernel, iterations=1)
image = 255 - image
result = 255 - cv2.bitwise_and(mask, image)
cv2.imshow('result', result)
cv2.imwrite('result.png', result)
cv2.imshow('mask', mask)
cv2.waitKey(0)
答案 1 :(得分:2)
您可以使用图像的打开或关闭(取决于您使用的是普通图像还是反转图像)。打开将首先腐蚀您的图像,然后将其放大。假设黑色背景上有明亮的物体,这将删除较小的物体。
例如,对于倒置图像,请使用
out = cv2.morphologyEx(src, MORPH_OPEN)
有关更多信息,请查看this教程
答案 2 :(得分:2)
这是使用Imagemagick的非常简单的解决方案。但是,可以从OpenCV使用相同的概念。抱歉,我不太会使用OpenCV。
Threshold the image
Perform morphologic close
Use connected components processing to remove extraneous features
输入:
kernel="5x5: 0,0,0,0,0 1,1,1,1,1 1,1,1,1,1 1,1,1,1,1 0,0,0,0,0"
convert img.png -threshold 75% \
-morphology close "$kernel" \
-define connected-components:area-threshold=100 \
-define connected-components:mean-color=true \
-connected-components 4 result.png
添加:
这里是Python Wand代码可以执行相同的操作。 Python Wand基于Imagemagick。它将需要Wand 0.5.6(如果有)和Imagemagick 7。
#!/bin/python3.7
from wand.image import Image
with Image(filename='curve.png') as img:
krnl="5x5: 0,0,0,0,0 1,1,1,1,1 1,1,1,1,1 1,1,1,1,1 0,0,0,0,0"
img.threshold(threshold=0.75)
img.morphology(method='close',kernel=krnl)
img.connected_components(connectivity=4, area_threshold=100, mean_color=True)
img.save(filename='curve_proc.png')