SILC 超像素分割代码
生活随笔
收集整理的這篇文章主要介紹了
SILC 超像素分割代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
詳見:https://www.cnblogs.com/wangyong/p/8991465.html
import math from skimage import io, color import numpy as npclass Cluster(object):cluster_index = 1def __init__(self, row, col, l=0, a=0, b=0):self.update(row, col, l, a, b)self.pixels = []self.no = self.cluster_indexCluster.cluster_index += 1def update(self, row, col, l, a, b):self.row = rowself.col = colself.l = lself.a = aself.b = bclass SLICProcessor(object):@staticmethoddef open_image(path):rgb = io.imread(path)lab_arr = color.rgb2lab(rgb)return lab_arr@staticmethoddef save_lab_image(path, lab_arr):rgb_arr = color.lab2rgb(lab_arr)io.imsave(path, rgb_arr)def make_cluster(self, row, col):row=int(row)col=int(col)return Cluster(row, col,self.data[row][col][0],self.data[row][col][1],self.data[row][col][2])def __init__(self, filename, K, M):self.K = Kself.M = Mself.data = self.open_image(filename)self.rows = self.data.shape[0]self.cols = self.data.shape[1]self.N = self.rows * self.colsself.S = int(math.sqrt(self.N / self.K))self.clusters = []self.label = {}self.dis = np.full((self.rows, self.cols), np.inf)def init_clusters(self):row = self.S / 2col = self.S / 2while row < self.rows:while col < self.cols:self.clusters.append(self.make_cluster(row, col))col+= self.Scol = self.S / 2row += self.Sdef get_gradient(self, row, col):if col + 1 >= self.cols:col = self.cols - 2if row + 1 >= self.rows:row = self.rows - 2gradient = (self.data[row + 1][col][0] +self.data[row][col+1][0]-2*self.data[row][col][0])+ \(self.data[row + 1][col][1] +self.data[row][col+1][1]-2*self.data[row][col][1]) + \(self.data[row + 1][col][2] +self.data[row][col+1][2]-2*self.data[row][col][2])return gradientdef move_clusters(self):for cluster in self.clusters:cluster_gradient = self.get_gradient(cluster.row, cluster.col)for dh in range(-1, 2):for dw in range(-1, 2):_row = cluster.row + dh_col = cluster.col + dwnew_gradient = self.get_gradient(_row, _col)if new_gradient < cluster_gradient:cluster.update(_row, _col, self.data[_row][_col][0], self.data[_row][_col][1], self.data[_row][_col][2])cluster_gradient = new_gradientdef assignment(self):for cluster in self.clusters:for h in range(cluster.row - 2 * self.S, cluster.row + 2 * self.S):if h < 0 or h >= self.rows: continuefor w in range(cluster.col - 2 * self.S, cluster.col + 2 * self.S):if w < 0 or w >= self.cols: continueL, 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.row, 2) +math.pow(w - cluster.col, 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)] = clustercluster.pixels.append((h, w))else:self.label[(h, w)].pixels.remove((h, w))self.label[(h, w)] = clustercluster.pixels.append((h, w))self.dis[h][w] = Ddef update_cluster(self):for cluster in self.clusters:sum_h = sum_w = number = 0for 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.limage_arr[p[0]][p[1]][1] = cluster.aimage_arr[p[0]][p[1]][2] = cluster.bimage_arr[cluster.row][cluster.col][0] = 0image_arr[cluster.row][cluster.col][1] = 0image_arr[cluster.row][cluster.col][2] = 0self.save_lab_image(name, image_arr)def iterates(self):self.init_clusters()#均勻分配塊的位置self.move_clusters()#一開始略調塊的中心,選擇區域中梯度最小的位置作為中心#考慮到效率和效果,折中選擇迭代10次for i in range(10):self.assignment()#每次計算聚類中心周圍2*S范圍內的像素點,根據相似性劃分self.update_cluster()#添加一些莫名其妙的像素點,我的理解是如果周圍的像素點都屬于某一個聚類,那么該像素點應該也屬于這個聚類self.save_current_image("output.jpg")if __name__ == '__main__':p = SLICProcessor('x1.png', 160, 40)p.iterates()?
總結
以上是生活随笔為你收集整理的SILC 超像素分割代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据分析--布林带策略(择时)
- 下一篇: 【IT-Windows】某些设置由你的组