1.skimage.segmentation.clear_border函数
clear_border(labels, buffer_size=0, bgval=0, in_place=False)主要作用是清除二值图像中边界的1值。例如
>>> import numpy as np
>>> from skimage.segmentation import clear_border
>>> labels = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0],
... [0, 0, 0, 0, 1, 0, 0, 0, 0],
... [1, 0, 0, 1, 0, 1, 0, 0, 0],
... [0, 0, 1, 1, 1, 1, 1, 0, 0],
... [0, 1, 1, 1, 1, 1, 1, 1, 0],
... [0, 0, 0, 0, 0, 0, 0, 0, 0]])
>>> clear_border(labels)
array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]])
2.skimage.measure.label函数
在skimage包中,使用measure子模块下的label函数即可实现连通区域标记。参数input表示需要处理的二值图像,connectivity表示判定连通的模式(1代表4连通,2代表8连通),输出labels为一个从0开始的标记数组。
neighbors:{4,8},int,可选是否使用4或8“连接”。在3D中,4“连接”意味着连接的像素在这个像素点的三个坐标截面(xyz)中任意一个存在4连通,共2+2+2=6种像素点。而对于8-“连通性”意味着连接的像素在这个像素点的任意截面中存在一截面存在8连通即可,共3×3×3-1=26种像素点。 **不推荐使用,
x = [[[1,0,1],
[0,1,0],
[0,0,1]],
[[1,0,0],
[0,0,0],
[0,0,1]],
[[1,0,0],
[0,1,0],
[0,0,1]]]
x=np.array(x)
print(label(x, neighbors=4))
[[[1 0 2]
[0 3 0]
[0 0 4]]
[[1 0 0]
[0 0 0]
[0 0 4]]
[[1 0 0]
[0 5 0]
[0 0 4]]]
x = [[[1,0,1],
[0,1,0],
[0,0,1]],
[[1,0,0],
[0,0,0],
[0,0,1]],
[[1,0,0],
[0,1,0],
[0,0,1]]]
x=np.array(x)
print(label(x, neighbors=8))
[[[1 0 1]
[0 1 0]
[0 0 1]]
[[1 0 0]
[0 0 0]
[0 0 1]]
[[1 0 0]
[0 1 0]
[0 0 1]]]
>>> import numpy as np
>>> x = np.eye(3).astype(int)
>>> print(x)
[[1 0 0]
[0 1 0]
[0 0 1]]
>>> print(label(x, connectivity = 1))
[[1 0 0]
[0 2 0]
[0 0 3]]
>>> print(label(x, connectivity = 2))
[[1 0 0]
[0 1 0]
[0 0 1]]
>>> print(label(x, background = -1))
[[1 2 2]
[2 1 2]
[2 2 1]]
>>> x = np.array([[1, 0, 0],
... [1, 1, 5],
... [0, 0, 0]])
>>> print(label(x))
[[1 0 0]
[1 1 2]
[0 0 0]]
3.skimage.measure.regionprops函数
此外,使用measure子模块中的regionprops()函数可以很方便地对每一个连通区域进行属性获取和操作,比如计算面积、外接矩形、凸包面积等等。
计算结果返回为所有连通区域的属性列表,列表长度为连通区域个数(第i个连通区域的attribute属性可以通过properties[i].attribute获取)。
Notes ----- The following properties can be accessed as attributes or keys:
**area** : int Number of pixels of region.
**bbox** : tuple Bounding box ``(min_row, min_col, max_row, max_col)`
**label** : int The label in the labeled input image.
在三维中bbox表示连通区域所有点中最小的xyz,以及最大的xyz加上1.
x = [[[1,0,0],
[0,0,0],
[0,0,1]],
[[1,0,0],
[0,0,0],
[0,0,1]],
[[0,0,0],
[0,0,0],
[0,0,1]]]
x=np.array(x)
x1=label(x, neighbors=8)
print(x1)
regions = regionprops(x1)
print(regions[0].bbox)
print(regions[0].area)
[[[1 0 0]
[0 0 0]
[0 0 2]]
[[1 0 0]
[0 0 0]
[0 0 2]]
[[0 0 0]
[0 0 0]
[0 0 2]]]
(0L, 0L, 0L, 2L, 1L, 1L)
2
作用:dict() 函数用于创建一个字典。返回一个字典。
5.Python enumerate() 函数
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
语法
以下是 enumerate() 方法的语法:enumerate(sequence, [start=0])
参数
- sequence -- 一个序列、迭代器或其他支持迭代对象。
- start -- 下标起始位置。
返回值
返回 enumerate(枚举) 对象。
>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1)) # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
普通的 for 循环
>>>i = 0
>>> seq = ['one', 'two', 'three']
>>> for element in seq:
... print i, seq[i]
... i +=1
...
0 one
1 two
2 three
for 循环使用 enumerate
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
... print i, element
...
0 one
1 two
2 three
6.skimage.morphology.binary_dilation(image, selem=None)函数二值图像的膨胀。
原理:一般对二值图像进行操作。找到像素值为1的点,将它的邻近像素点都设置成这个值。1值表示白,0值表示黑,因此膨胀操作可以扩大白色值范围,压缩黑色值范围。一般用来扩充边缘或填充小的孔洞。
selem表示结构元素,用于设定局部区域的形状和大小。
分别用边长为5或15的正方形滤波器对棋盘图片进行膨胀操作,结果如下:
可见滤波器的大小,对操作结果的影响非常大。一般设置为奇数。
除了正方形的滤波器外,滤波器的形状还有一些,现列举如下:
morphology.square: 正方形
morphology.disk: 平面圆形#disk_12 = disk(6, dtype=np.bool)半径为6的圆形滤波器
morphology.ball: 球形
morphology.cube: 立方体形
morphology.diamond: 钻石形
morphology.rectangle: 矩形
morphology.star: 星形
morphology.octagon: 八角形
morphology.octahedron: 八面体
注意,如果处理图像为非二值图像(只有0和1两个值),则可以调用:
skimage.morphology.dilation(image, selem=None)
7.skimage.morphology.binary_erosion(image, selem=None)二值图像腐蚀操作。
原理:和膨胀相反的操作,将0值扩充到邻近像素。扩大黑色部分,减小白色部分。可用来提取骨干信息,去掉毛刺,去掉孤立的像素。
selem表示结构元素,用于设定局部区域的形状和大小。
用边长为5和25的正方形滤波器进行腐蚀滤波结果如下:
注意,如果处理图像为非二值图像(只有0和1两个值),则可以调用:
skimage.morphology.erosion(image, selem=None)
前面两点参考:https://blog.csdn.net/haoji007/article/details/52063306。这里边是基本滤波学蛮全的很好!!!
8.skimage.morphology.remove_small_holes(ar, min_size=64, connectivity=1, in_place=False):
移除小于指定尺寸的连续孔,去掉中间的小块(例子为值为0的区域)。
Examples
--------
>>> from skimage import morphology
>>> a = np.array([[1, 1, 1, 1, 1, 0],
... [1, 1, 1, 0, 1, 0],
... [1, 0, 0, 1, 1, 0],
... [1, 1, 1, 1, 1, 0]], bool)
>>> b = morphology.remove_small_holes(a, 2)#最小值设为2了,默认4连通
>>> b
array([[ True, True, True, True, True, False],
[ True, True, True, True, True, False],
[ True, False, False, True, True, False],
[ True, True, True, True, True, False]], dtype=bool)
>>> c = morphology.remove_small_holes(a, 2, connectivity=2)
>>> c
array([[ True, True, True, True, True, False],
[ True, True, True, False, True, False],
[ True, False, False, True, True, False],
[ True, True, True, True, True, False]], dtype=bool)
>>> d = morphology.remove_small_holes(a, 2, in_place=True)
>>> d is a
True
9.skimage.morphology.binary_closing(image, selem=None)闭运算函数(非二值化图像skimage.morphology.closing)
selem表示结构元素,用于设定局部区域的形状和大小。
先膨胀再腐蚀,可用来填充孔洞。
用边长为9的圆形滤波器进行闭运算结果如下:
反之还有skimage.morphology.binary_opening(image, selem=None)开运算函数(非二值化图像skimage.morphology.opening)
selem表示结构元素,用于设定局部区域的形状和大小。
先腐蚀再膨胀,可以消除小物体或小斑块。
用边长为9的圆形滤波器进行开运算结果如下:
这里还是参考:https://blog.csdn.net/haoji007/article/details/52063306。代码里面是ball(3)的立体闭运算。说明可以在三维上用。
ps:2019年6月26号,关于skimage库好的攻略:https://www.jianshu.com/p/f2e88197e81d