当前位置:网站首页 > 技术博客 > 正文

超像素slic区域合并



 1 import math  2 from skimage import io, color  3 import numpy as np  4  5 class Cluster(object):  6  7 cluster_index = 1  8  9 def __init__(self, row, col, l=0, a=0, b=0):  10  self.update(row, col, l, a, b)  11 self.pixels = []  12 self.no = self.cluster_index  13 Cluster.cluster_index += 1  14  15 def update(self, row, col, l, a, b):  16 self.row = row  17 self.col = col  18 self.l = l  19 self.a = a  20 self.b = b  21  22  23 class SLICProcessor(object):  24  @staticmethod  25 def open_image(path):  26 rgb = io.imread(path)  27 lab_arr = color.rgb2lab(rgb)  28 return lab_arr  29  30  @staticmethod  31 def save_lab_image(path, lab_arr):  32 rgb_arr = color.lab2rgb(lab_arr)  33  io.imsave(path, rgb_arr)  34  35 def make_cluster(self, row, col):  36 row=int(row)  37 col=int(col)  38 return Cluster(row, col,  39  self.data[row][col][0],  40 self.data[row][col][1],  41 self.data[row][col][2])  42  43 def __init__(self, filename, K, M):  44 self.K = K  45 self.M = M  46  47 self.data = self.open_image(filename)  48 self.rows = self.data.shape[0]  49 self.cols = self.data.shape[1]  50 self.N = self.rows * self.cols  51 self.S = int(math.sqrt(self.N / self.K))  52  53 self.clusters = []  54 self.label = {}  55 self.dis = np.full((self.rows, self.cols), np.inf)  56  57 def init_clusters(self):  58 row = self.S / 2  59 col = self.S / 2  60 while row < self.rows:  61 while col < self.cols:  62  self.clusters.append(self.make_cluster(row, col))  63 col+= self.S  64 col = self.S / 2  65 row += self.S  66  67 def get_gradient(self, row, col):  68 if col + 1 >= self.cols:  69 col = self.cols - 2  70 if row + 1 >= self.rows:  71 row = self.rows - 2  72  73 gradient = (self.data[row + 1][col][0] +self.data[row][col+1][0]-2*self.data[row][col][0])+  74 (self.data[row + 1][col][1] +self.data[row][col+1][1]-2*self.data[row][col][1]) +  75 (self.data[row + 1][col][2] +self.data[row][col+1][2]-2*self.data[row][col][2])  76  77 return gradient  78  79 def move_clusters(self):  80 for cluster in self.clusters:  81 cluster_gradient = self.get_gradient(cluster.row, cluster.col)  82 for dh in range(-1, 2):  83 for dw in range(-1, 2):  84 _row = cluster.row + dh  85 _col = cluster.col + dw  86 new_gradient = self.get_gradient(_row, _col)  87 if new_gradient < cluster_gradient:  88 cluster.update(_row, _col, self.data[_row][_col][0], self.data[_row][_col][1], self.data[_row][_col][2])  89 cluster_gradient = new_gradient  90  91 def assignment(self):  92 for cluster in self.clusters:  93 for h in range(cluster.row - 2 * self.S, cluster.row + 2 * self.S):  94 if h < 0 or h >= self.rows: continue  95 for w in range(cluster.col - 2 * self.S, cluster.col + 2 * self.S):  96 if w < 0 or w >= self.cols: continue  97 L, A, B = self.data[h][w]  98 Dc = math.sqrt(  99 math.pow(L - cluster.l, 2) + 100 math.pow(A - cluster.a, 2) + 101 math.pow(B - cluster.b, 2)) 102 Ds = math.sqrt( 103 math.pow(h - cluster.row, 2) + 104 math.pow(w - cluster.col, 2)) 105 D = math.sqrt(math.pow(Dc / self.M, 2) + math.pow(Ds / self.S, 2)) 106 if D < self.dis[h][w]: 107 if (h, w) not in self.label: 108 self.label[(h, w)] = cluster 109  cluster.pixels.append((h, w)) 110 else: 111  self.label[(h, w)].pixels.remove((h, w)) 112 self.label[(h, w)] = cluster 113  cluster.pixels.append((h, w)) 114 self.dis[h][w] = D 115 116 def update_cluster(self): 117 for cluster in self.clusters: 118 sum_h = sum_w = number = 0 119 for p in cluster.pixels: 120 sum_h += p[0] 121 sum_w += p[1] 122 number += 1 123 _h =int( sum_h / number) 124 _w =int( sum_w / number) 125 cluster.update(_h, _w, self.data[_h][_w][0], self.data[_h][_w][1], self.data[_h][_w][2]) 126 127 def save_current_image(self, name): 128 image_arr = np.copy(self.data) 129 for cluster in self.clusters: 130 for p in cluster.pixels: 131 image_arr[p[0]][p[1]][0] = cluster.l 132 image_arr[p[0]][p[1]][1] = cluster.a 133 image_arr[p[0]][p[1]][2] = cluster.b 134 image_arr[cluster.row][cluster.col][0] = 0 135 image_arr[cluster.row][cluster.col][1] = 0 136 image_arr[cluster.row][cluster.col][2] = 0 137  self.save_lab_image(name, image_arr) 138 139 def iterates(self): 140  self.init_clusters() 141  self.move_clusters() 142 #考虑到效率和效果,折中选择迭代10次 143 for i in range(10): 144  self.assignment() 145  self.update_cluster() 146 self.save_current_image("output.jpg") 147 148 149 if __name__ == '__main__': 150 p = SLICProcessor('beauty.jpg', 200, 40) 151 p.iterates()

版权声明


相关文章:

  • sprintf赋值string2025-08-30 10:30:02
  • malloc函数菜鸟教程2025-08-30 10:30:02
  • 动态规划解01背包的算法2025-08-30 10:30:02
  • idea2020安装破解教程2025-08-30 10:30:02
  • 气体扩散模型2025-08-30 10:30:02
  • 静态嵌套类和内部类的区别2025-08-30 10:30:02
  • spring security oauth2 jwt 登出2025-08-30 10:30:02
  • mockito captor2025-08-30 10:30:02
  • 图的遍历方法有哪些2025-08-30 10:30:02
  • 怎么打开git命令窗口2025-08-30 10:30:02