SLIC算法

SLIC算法

simple linear iterative clustering 2010年由Achanta等人提出 解决目标:超像素分割(指具有相似纹理、颜色、亮度等特征的相邻像素构成的有一定视觉意义的不规则像素块) 优点:速度快,效果好,方法简单 缺点:超像素大小不一致

补充说明

超像素

超像素就是把一幅原本是像素级(pixel-level)的图,划分成区域级(district-level)的图。可以将其看做是对基本信息进行的抽象。

超像素概念是2003年由Ren和Malik提出的,是一种图像分割方法,将图像分割成具有相似纹理、颜色、亮度等特征的相邻像素构成的有一定视觉意义的不规则像素块。超像素分割是图像分割的一种,是图像分割的预处理步骤,目的是减少图像分割的复杂度,提高分割的准确性, 所以通常作为分割算法的预处理步骤

图像梯度

图像梯度是指图像某像素在x和y两个方向上的变化率(与相邻像素比较)是一个二维向量,由2个分量组成X轴的变化、Y轴的变化 。其中:

  • X轴的变化是指当前像素右侧(X加1)的像素值减去当前像素左侧(X减1)的像素值。

  • Y轴的变化是当前像素下方(Y加1)的像素值减去当前像素上方(Y减1)的像素值。

图像梯度计算示意

Lab颜色空间

Lab颜色空间的介绍: Lab色彩模型是由亮度(L)和有关色彩的a, b三个要素组成。L表示亮度(Luminosity),L的值域由0(黑色)到100(白色)。a表示从洋红色至绿色的范围(a为负值指示绿色而正值指示品红),b表示从黄色至蓝色的范围(b为负值指示蓝色而正值指示黄色)。

Lab颜色空间的优点: 1)不像RGB和CMYK色彩空间,Lab 颜色被设计来接近人类生理视觉。它致力于感知均匀性,它的 L 分量密切匹配人类亮度感知。因此可以被用来通过修改 a 和 b 分量的输出色阶来做精确的颜色平衡,或使用 L 分量来调整亮度对比。这些变换在 RGB 或 CMYK 中是困难或不可能的。 2)因为 Lab 描述的是颜色的显示方式,而不是设备(如显示器、打印机或数码相机)生成颜色所需的特定色料的数量,所以 Lab 被视为与设备无关的颜色模型。 3)色域宽阔。它不仅包含了RGB,CMYK的所有色域,还能表现它们不能表现的色彩。人的肉眼能感知的色彩,都能通过Lab模型表现出来。另外,Lab色彩模型的绝妙之处还在于它弥补了RGB色彩模型色彩分布不均的不足,因为RGB模型在蓝色到绿色之间的过渡色彩过多,而在绿色到红色之间又缺少黄色和其他色彩。如果我们想在数字图形的处理中保留尽量宽阔的色域和丰富的色彩,最好选择Lab

SLIC算法原理

SLIC算法创新点

算法的具体思路如下所示

  1. 先初始化种子点,根据图片的像素总数以及希望分割的超像素区域个数均匀分布种子点。

  2. 在种子点的邻域内(一般取n=3)搜索最小的梯度值,将种子点移动到梯度值最小的位置。(避免种子点落在梯度较大的轮廓边界上,以免影响后续聚类效果)

  3. 对于每个种子点,将其周围2S*2S的区域内的像素点进行聚类,计算每个像素点与种子点的距离,以及像素点的梯度值,计算距离和梯度值的加权和,以此作为像素点与种子点的相似度。

  4. 对于每个像素点,将其分配到与其最近的种子点所在的聚类中。

  5. 迭代优化。理论上上述步骤不断迭代直到误差收敛(可以理解为每个像素点聚类中心不再发生变化为止),实践发现10次迭代对绝大部分图片都可以得到较理想效果,所以一般迭代次数取10。

  6. 增强连通性。经过上述迭代优化可能出现以下瑕疵:出现多连通情况、超像素尺寸过小,单个超像素被切割成多个不连续超像素等,这些情况可以通过增强连通性解决。主要思路是:新建一张标记表,表内元素均为-1,按照“Z”型走向(从左到右,从上到下顺序)将不连续的超像素、尺寸过小超像素重新分配给邻近的超像素,遍历过的像素点分配给相应的标签,直到所有点遍历完毕为止。

下面是测试代码,可以直接运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import math
from skimage import io, color
import numpy as np
from tqdm import trange


class Cluster(object):
cluster_index = 1

def __init__(self, h, w, l=0, a=0, b=0):
self.update(h, w, l, a, b)
self.pixels = []
self.no = self.cluster_index
Cluster.cluster_index += 1

def update(self, h, w, l, a, b):
self.h = h
self.w = w
self.l = l
self.a = a
self.b = b

def __str__(self):
return "{},{}:{} {} {} ".format(self.h, self.w, self.l, self.a, self.b)

def __repr__(self):
return self.__str__()


class SLICProcessor(object):
@staticmethod
def open_image(path):
"""
Return:
3D array, row col [LAB]
"""
rgb = io.imread(path)
lab_arr = color.rgb2lab(rgb)
return lab_arr

@staticmethod
def save_lab_image(path, lab_arr):
"""
Convert the array to RBG, then save the image
:param path:
:param lab_arr:
:return:
"""
rgb_arr = color.lab2rgb(lab_arr)
io.imsave(path, rgb_arr)

def make_cluster(self, h, w):
h = int(h)
w = int(w)
return Cluster(h, w,
self.data[h][w][0],
self.data[h][w][1],
self.data[h][w][2])

def __init__(self, filename, K, M):
self.K = K
self.M = M

self.data = self.open_image(filename)
self.image_height = self.data.shape[0]
self.image_width = self.data.shape[1]
self.N = self.image_height * self.image_width
self.S = int(math.sqrt(self.N / self.K))

self.clusters = []
self.label = {}
self.dis = np.full((self.image_height, self.image_width), np.inf)

def init_clusters(self):
h = self.S / 2
w = self.S / 2
while h < self.image_height:
while w < self.image_width:
self.clusters.append(self.make_cluster(h, w))
w += self.S
w = self.S / 2
h += self.S

def get_gradient(self, h, w):
if w + 1 >= self.image_width:
w = self.image_width - 2
if h + 1 >= self.image_height:
h = self.image_height - 2

gradient = self.data[h + 1][w + 1][0] - self.data[h][w][0] + \
self.data[h + 1][w + 1][1] - self.data[h][w][1] + \
self.data[h + 1][w + 1][2] - self.data[h][w][2]
return gradient

def move_clusters(self):
for cluster in self.clusters:
cluster_gradient = self.get_gradient(cluster.h, cluster.w)
for dh in range(-1, 2):
for dw in range(-1, 2):
_h = cluster.h + dh
_w = cluster.w + dw
new_gradient = self.get_gradient(_h, _w)
if new_gradient < cluster_gradient:
cluster.update(_h, _w, self.data[_h][_w][0], self.data[_h][_w][1], self.data[_h][_w][2])
cluster_gradient = new_gradient

def assignment(self):
for cluster in self.clusters:
for h in range(cluster.h - 2 * self.S, cluster.h + 2 * self.S):
if h < 0 or h >= self.image_height: continue
for w in range(cluster.w - 2 * self.S, cluster.w + 2 * self.S):
if w < 0 or w >= self.image_width: continue
L, A, B = self.data[h][w]
Dc = math.sqrt(
math.pow(L - cluster.l, 2) +
math.pow(A - cluster.a, 2) +
math.pow(B - cluster.b, 2))
Ds = math.sqrt(
math.pow(h - cluster.h, 2) +
math.pow(w - cluster.w, 2))
D = math.sqrt(math.pow(Dc / self.M, 2) + math.pow(Ds / self.S, 2))
if D < self.dis[h][w]:
if (h, w) not in self.label:
self.label[(h, w)] = cluster
cluster.pixels.append((h, w))
else:
self.label[(h, w)].pixels.remove((h, w))
self.label[(h, w)] = cluster
cluster.pixels.append((h, w))
self.dis[h][w] = D

def update_cluster(self):
for cluster in self.clusters:
sum_h = sum_w = number = 0
for p in cluster.pixels:
sum_h += p[0]
sum_w += p[1]
number += 1
_h = int(sum_h / number)
_w = int(sum_w / number)
cluster.update(_h, _w, self.data[_h][_w][0], self.data[_h][_w][1], self.data[_h][_w][2])

def save_current_image(self, name):
image_arr = np.copy(self.data)
for cluster in self.clusters:
for p in cluster.pixels:
image_arr[p[0]][p[1]][0] = cluster.l
image_arr[p[0]][p[1]][1] = cluster.a
image_arr[p[0]][p[1]][2] = cluster.b
image_arr[cluster.h][cluster.w][0] = 0
image_arr[cluster.h][cluster.w][1] = 0
image_arr[cluster.h][cluster.w][2] = 0
self.save_lab_image(name, image_arr)

def iterate_10times(self):
self.init_clusters()
self.move_clusters()
for i in trange(10):
self.assignment()
self.update_cluster()
name = 'lenna_M{m}_K{k}_loop{loop}.png'.format(loop=i, m=self.M, k=self.K)
self.save_current_image(name)


if __name__ == '__main__':
p = SLICProcessor('Lenna.png', 200, 40)
p.iterate_10times()
p = SLICProcessor('Lenna.png', 300, 40)
p.iterate_10times()
p = SLICProcessor('Lenna.png', 500, 40)
p.iterate_10times()
p = SLICProcessor('Lenna.png', 1000, 40)
p.iterate_10times()
p = SLICProcessor('Lenna.png', 200, 5)
p.iterate_10times()
p = SLICProcessor('Lenna.png', 300, 5)
p.iterate_10times()
p = SLICProcessor('Lenna.png', 500, 5)
p.iterate_10times()
p = SLICProcessor('Lenna.png', 1000, 5)
p.iterate_10times()

SLIC算法
http://example.com/2025/02/01/SLIC/
作者
John Doe
发布于
2025年2月1日
许可协议