抱歉,我不太了解 OpenCV。所以这里是如何做到这一点的大纲。我还展示了使用 Imagemagick 和 Python Wand 代码的代码。请注意,我保存中间图像以显示步骤。
Read the image
Blur it some
Threshold is to binary
Do connected components processing to remove all small regions (see contours or blobs in OpenCV)
Use morphology open and close to smooth the edges
Extract an edge outline of the transition between white and black (there are many edge operators: laplacian, gradients, canny, etc)
输入:
convert img.jpg -blur 0x1 -threshold 9% -type bilevel +write threshold.png \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=10000 \
-connected-components 4 +write ccl.png \
-morphology open disk:5 \
-morphology close disk:5 +write smooth.png \
-morphology edge diamond:1 \
result.png
门槛:
连接组件:
流畅:
结果:
这是使用 Python Wand 0.5.6(目前正在开发中)和 Imagemagick 7.0.8.56 的等效代码
#!/bin/python3.7
from wand.image import Image
from wand.display import display
with Image(filename='curve.jpg') as img:
img.blur(radius=0, sigma=1)
img.threshold(threshold=0.09)
img.connected_components(connectivity=4, area_threshold=1000, mean_color=True)
img.morphology(method='open', kernel='disk:5')
img.morphology(method='close', kernel='disk:5')
img.morphology(method='edge', kernel='diamond:1')
img.save(filename='0_wand_trim.png')
display(img)