SLIC学习笔记

SLIC学习笔记超像素分割 SLIC 学习最新看论文的时候发现 超像素分割 概念被多次提及 作为图像预处理的一部分 超像素分割 可以在保持图像特征不变的情况下 减少后续图像处理的计算量 这里 将简单介绍一下 SLIC Simplelinear 算法 先贴出相关论文和源代码供大家参考 SLIC 算法描述算法流程 对照上述算法流程图 SLIC 算法可

超像素分割——SLIC学习

SLIC算法描述

python代码实现

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): 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 = int(self.S / 2) w = int(self.S / 2) while h < self.image_height: while w < self.image_width: self.clusters.append(self.make_cluster(h, w)) w += int(self.S) w = int(self.S / 2) h += int(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[w + 1][h + 1][0] - self.data[w][h][0] + \ self.data[w + 1][h + 1][1] - self.data[w][h][1] + \ self.data[w + 1][h + 1][2] - self.data[w][h][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()

代码运行比较慢,花了我大概一分钟左右,如果是用于工程项目中的话,还需要好好优化一下。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210875.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午11:42
下一篇 2026年3月18日 下午11:42


相关推荐

  • Linode 遭受大规模DDoS攻击

    Linode 遭受大规模DDoS攻击美国当地时间12月29日,专用虚拟服务器提供商Linode遭到DDoS攻击,截至到本文发布时其web服务的访问仍受影响,其中API调用和管理功能依然部分不可用。Linode称正在努力尽快恢复正常访问服务。\\早在2013年就有过Linode就遭到大规模DDoS攻击。在HackNews上的讨论中,有人认为Linode在安全建设方面多有疏忽,并且和ISP缺乏配合,从而导致此次DDoS攻击影响过甚。\…

    2022年7月12日
    20
  • varchar2 类型「建议收藏」

    varchar2 类型「建议收藏」1、varchar2在数据库表中的最大长度是4000bytesorcharacter,在oracleplsqlvarchar2最大支持长度为32767个字节SQL>declare 2       v_varvarchar2(32767); 3     begin 4       null; 5     end; 6     /

    2022年6月24日
    53
  • 用冒泡法和选择法对10个整数排序(C语言 数组)

    用冒泡法和选择法对10个整数排序(C语言 数组)1 区别 nbsp nbsp nbsp nbsp nbsp nbsp 两者最大的区别在于算法本身 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 冒泡法是相邻元素两两比较 每趟将最值沉底即可确定一个数在结果的位置 确定元素位置的顺序是从后往前 其余元素可以作相对位置的调整 可以进行升序或降序排序 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 选择法是每趟选出一个最值确定其在结果序列中的位置 确定元素的位置是从前往后 而每趟最多进行一次交换 其余元素的相对位置不变 可进行降序排序或升序排序 2 冒泡法

    2026年2月1日
    1
  • python、cpython、IPython、Jython区别

    python、cpython、IPython、Jython区别Python 是解释型语言 代码在执行时会一行一行地翻译成 CPU 能理解的机器码 这个翻译过程非常耗时 所以很慢 而 C 程序是运行前直接编译成 CPU 能执行的机器码 所以非常快 要运行代码 就需要 Python 解释器去执行 py 文件 当我们从 Python 官方网站下载并安装好 Python3 x 后 我们就直接获得了一个官方版本的解释器 CPython 这个解释器是用 C 语言开发的 所以叫 CPython 在命令行下运行 pyt

    2026年3月19日
    3
  • PyCharm2021安装教程

    PyCharm2021安装教程Windows安装pycharm教程新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入下载安装PyCharm1、进入官网PyCharm的下载地址:http://www.jetbrains.com/pycharm/downl

    2022年5月16日
    57
  • mysql多字段主键_sql改变列数据类型

    mysql多字段主键_sql改变列数据类型MySQL数据类型(1)数值类型1、整数型2、浮点型3、定点型(2)日期时间类型(3)字符串类型MySQL字段属性1、空\不为空值:NULL、NOTNULL2、主键:primarykey3、唯一键:uniquekey4、自增长:auto_increment5、默认值:default6、字段描述:comment数值类型整数类型:tinyint、smallint、mediumint、intege…

    2025年12月2日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号