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 超像素分割代码相关推荐
- VLFeat SLIC超像素分割(Cpp版)
这段时间对VLFeat的C接口非常的感兴趣,以前用的都是其Matlab接口,虽然很方便,而且提供的Matlab接口要比C接口功能更强大,但Matlab终归只能用来做一下快速的方法验证,所以想比较完整的 ...
- julia 调用python库_Julia调用Python实现超像素分割SLIC算法
最近想要在julia中实现 Simple Linear Iterative Clustering (SLIC) 算法对图像进行超像素分割,关于SLIC超像素分割算法,请参考SLIC Superpixe ...
- SLIC超像素分割方法
为了方便查找,记录SLIC超像素分割方法的介绍 简介 关键代码分析 应用
- 超像素分割与超像素合并/区域合并/多尺度分割
最近两年,超像素分割方法非常火爆,计算机视觉.模式识别许多方向都兴起了超像素研究的热潮,提出的方法也比较多.其实这不是个什么新鲜的东西,以前的许多分割算法所获得的结果都可以称为超像素,如Watersh ...
- SLIC超像素分割的算法介绍和源码分析
前述 最近在看显著性检测,发现很多算法的基础是超像素分割,而正在看的Saliency Optimization from Robust Background Detection算法的预处理是SLIC算 ...
- MATLAB显示slic,quickshift超像素分割结果图
首先介绍vlfeat库函数:vl_slic,vl_quickshift,vl_quckseg vl_slic SLIC superpixels segments = vl_slic(im,regio ...
- 超像素分割算法————综述
参考:超像素-学习笔记 什么是超像素?评价标准?SLIC.SEED.ETPS算法 比较的指标:图像边界的粘附性.算法速度.存储效率.分割性能 超像素算法:将像素组合成感知有意义的原子区域( atomi ...
- 超像素分割(Slic算法)——个人梳理
一.使用背景 我在进行乳腺癌图像识别的学校项目中,参考了山东大学的硕士论文,并希望加以简化复现,此论文会在文末附上.项目要求我们需要对乳腺癌图片进行分类(无肿瘤,良性肿瘤,恶性肿瘤),参照论文所说,我 ...
- 图像分割:Python的SLIC超像素分割
图像分割:Python的SLIC超像素分割 1. 什么是超像素? 2. 为什么超像素在计算机视觉方面有重要的作用? 3. 简单线性迭代聚类(SLIC) 4. 效果图 5. 源码 参考 1. 什么是超像 ...
最新文章
- Normalization 的发展历程
- Python staticmethod() 函数
- WordCount运行详解
- XSuperTooltip - Office 2007 Super Tooltip class
- 优化 .net core 应用的 dockerfile
- 2.1.4 Python单例模式
- 优化的意义,不仅在于业绩的提升
- 获得两点之间连续坐标,向量加法、减法、乘法的运用
- HTTP报文结构详解
- React实现微信扫码支付
- Java 阿里云实人认证
- 安装windows与Ubuntu双系统,并使用GRUB启动引导器
- Nexus升级、license安装和恢复密码
- 使用SfntTool制作字体剪辑工具1 - 直接使用sfnttool.jar
- 陈学智升任VMware全球副总裁、大中华区总裁,面临四个挑战
- TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)
- 【学习笔记】刘晓艳英语语法笔记(2/6)——并列句
- JavaScript mongodb(数据库)复杂值
- CMOS 和CCD的区别
- linux下安装nodejs的方式