Engee 文档
Notebook

形态学操作,第2部分

在这个演示中,我们将看到形态学操作,如填充、穿刺和清除边界。

让我们从连接必要的库开始。

In [ ]:
Pkg.add(["ImageBinarization", "Noise", "ImageDraw", "TestImages", "ImageMorphology"])
In [ ]:
Pkg.add("ImageBinarization")
   Resolving package versions...
  No Changes to `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`
In [ ]:
using Images, Noise, ImageMorphology
using ImageBinarization
using ImageDraw
using TestImages

上传图像并执行其阈值处理。

In [ ]:
img_src = testimage("morphology_test_512")
Out[0]:
No description has been provided for this image
In [ ]:
img_input = binarize(Gray.(img_src), UnimodalRosin()) .> 0.5
Out[0]:
512×512 BitMatrix:
 0  0  0  0  0  0  0  0  0  0  0  0  0  …  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0  …  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0  …  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 ⋮              ⋮              ⋮        ⋱  ⋮              ⋮              ⋮  
 0  0  0  0  0  0  0  0  0  0  0  0  0  …  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0  …  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0  …  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  0  0  0  0  0     0  0  0  0  0  0  0  0  0  0  0  0

我们将在未来的工作中通过为其分配灰度格式来使用此图像,因为下面列出的操作适用于灰色格式的图像。

In [ ]:
Gray.(img_input)
Out[0]:
No description has been provided for this image

突出边框

Convexhull函数输出看起来最像涂层的图像的外边框,并将border对象的顶点作为CartesianIndex数组返回。

In [ ]:
cordinates = convexhull(img_input)
img_convex = RGB.(copy(img_input))
push!(cordinates, cordinates[1])
draw!(img_convex, Path(cordinates), RGB(1))
img_convex
Out[0]:
No description has been provided for this image

填写图像

图像填充操作查找相关图像分量并使用它们来产生结果图像。 完成填充功能后,此图像落在指定区间的范围内。

In [ ]:
img_noise = salt_pepper(img_input, 0.5)
fill_image_1 = imfill(img_noise, (0.1, 1))
fill_image_2 = imfill(img_noise, (0.1, 10)) # this configuration gets us best results
fill_image_3 = imfill(img_noise, (1, 10))
fill_image_4 = imfill(img_noise, (5, 20)) # objects of smaller sizes gets left out
Gray.([img_noise fill_image_1 fill_image_2 fill_image_3 fill_image_4])
Out[0]:
No description has been provided for this image

变薄图像

细化操作应用Go算法,该算法确定要离开哪些像素。

In [ ]:
img_thinning = thinning(img_input, algo = GuoAlgo());
Gray.([img_input img_thinning])
Out[0]:
No description has been provided for this image

清理边界

Clearborder方法可用于清除与图像边界相关联的对象。

In [ ]:
cleared_img_1 = clearborder(img_input, 20); # 20 is the width of border that's examined
cleared_img_2 = clearborder(img_input, 30); # notice how it remove the inner circle even if it's outside its range
cleared_img_3 = clearborder(img_input, 30, 1);

删除的默认颜色是0,这意味着RGB(0)被删除,但现在由于它是1,它清除整个图像由于填充算法。

In [ ]:
Gray.([img_input cleared_img_1 cleared_img_2 cleared_img_3])
Out[0]:
No description has been provided for this image

结论

在这个例子中,我们分析了一些可应用于图像的有用形态学操作。