当前位置:网站首页>使用 Kmeans聚类实现颜色的分割
使用 Kmeans聚类实现颜色的分割
2022-07-27 09:56:00 【用户9925864】
之前分享过kmeans算法(传送门:数据挖掘算法—K-Means算法),这期分享一下使用 Kmeans聚类实现颜色的分割,使用 L*a*b* 颜色空间和 K 均值聚类自动分割颜色。
步骤 1:读取图像
读取hestain.png,
he = imread('hestain.png');
imshow(he), title('H&E image');步骤 2:将图像从 RGB 颜色空间转换为 L*a*b* 颜色空间
L*a*b* 颜色空间(也称为 CIELAB 或 CIE L*a*b*)能够量化视觉差异。
L*a*b* 颜色空间是从 CIE XYZ 三色值派生的。L*a*b* 空间包含光度层 'L*'、色度层 'a*'(表示颜色落在沿红-绿轴的位置)和色度层 'b*'(表示颜色落在沿蓝-黄轴的位置)。所有颜色信息都在 'a*' 和 'b*' 层。可以使用欧几里德距离度量来测量两种颜色之间的差异。
使用 rgb2lab 将图像转换为 L*a*b* 颜色空间。
lab_he = rgb2lab(he);步骤 3:用 K 均值聚类对基于 'a*b*' 空间的颜色进行分类
聚类是一种分离对象组的方法。K 均值聚类将每个对象视为在空间中有一个位置。它将对象划分为若干分区,使每个簇中的对象尽可能彼此靠近,并尽可能远离其他簇中的对象。K 均值聚类要求您指定要划分的簇数和用于量化两个对象之间距离的距离度量。
由于颜色信息基于 'a*b*' 颜色空间,因此您的对象是具有 'a*' 和 'b*' 值的像素。将数据转换为数据类型 single,以便与 imsegkmeans 结合使用。使用 imsegkmeans 对对象进行聚类以分为三个簇。
ab = lab_he(:,:,2:3);
ab = im2single(ab);
nColors = 3;
% 重复三次聚类,避免局部最优
pixel_labels = imsegkmeans(ab,nColors,'NumAttempts',3);对于输入中的每个对象,imsegkmeans 会返回一个对应于簇的索引或标签。用像素的标签标注图像中的每个像素。
imshow(pixel_labels,[])
title('Image Labeled by Cluster Index');步骤 4:创建按颜色分割 H&E 图像的图像
使用 pixel_labels,可以按颜色分离 hestain.png 中的对象,这将产生三个图像。
mask1 = pixel_labels==1;
cluster1 = he .* uint8(mask1);
imshow(cluster1)
title('Objects in Cluster 1');mask2 = pixel_labels==2;
cluster2 = he .* uint8(mask2);
imshow(cluster2)
title('Objects in Cluster 2');mask3 = pixel_labels==3;
cluster3 = he .* uint8(mask3);
imshow(cluster3)
title('Objects in Cluster 3');步骤 5:分割核
簇 3 包含蓝色对象。请注意,有深蓝色和浅蓝色对象。您可以使用 L*a*b* 颜色空间中的 'L*' 层来分离深蓝色和浅蓝色。细胞核为深蓝色。
前面提到过,'L*' 层包含每种颜色的亮度值。提取此簇中像素的亮度值,并使用 imbinarize 用全局阈值对其设置阈值。掩膜 is_light_blue 给出了浅蓝色像素的索引。
L = lab_he(:,:,1);
L_blue = L .* double(mask3);
L_blue = rescale(L_blue);
idx_light_blue = imbinarize(nonzeros(L_blue));复制蓝色对象的掩膜 mask3,然后从掩膜中删除浅蓝色像素。将新掩膜应用于原始图像并显示结果。只有深蓝色细胞核可见。
blue_idx = find(mask3);
mask_dark_blue = mask3;
mask_dark_blue(blue_idx(idx_light_blue)) = 0;
blue_nuclei = he .* uint8(mask_dark_blue);
imshow(blue_nuclei)
title('Blue Nuclei');边栏推荐
- What age are you still using date
- open3d库的安装,conda常用指令,导入open3d时报这个错误Solving environment: failed with initial frozen solve. Retrying w
- Anchor free detector: centernet
- Leetcode.565. array nesting____ Violent dfs- > pruning dfs- > in situ modification
- 活体检测综述
- 直播倒计时 3 天|SOFAChannel#29 基于 P2P 的文件和镜像加速系统 Dragonfly
- pytorch中对BatchNorm2d()函数的理解
- 数学推理题:张王李赵陈五对夫妇聚会,见面握手
- SE(Squeeze and Excitation)模块的理解以及代码实现
- Food safety | the more you eat junk food, the more you want to eat it? Please keep this common food calorimeter
猜你喜欢

交换机端口镜像配置指南

Expose a technology boss from a poor family

pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0

中高级试题」:MVCC 实现原理是什么?
![WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]](/img/94/92ad89751e746a18edf80296db9188.png)
WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]

Leetcode.814. binary tree pruning____ DFS

Concurrent Park and unpark description

数据库性能系列之子查询

Understand chisel language. 24. Chisel sequential circuit (IV) -- detailed explanation of chisel memory
![[scm] source code management - lock of perforce branch](/img/c6/daead474a64a9a3c86dd140c097be0.jpg)
[scm] source code management - lock of perforce branch
随机推荐
数学推理题:张王李赵陈五对夫妇聚会,见面握手
open3d库的安装,conda常用指令,导入open3d时报这个错误Solving environment: failed with initial frozen solve. Retrying w
活体检测综述
直播倒计时 3 天|SOFAChannel#29 基于 P2P 的文件和镜像加速系统 Dragonfly
S switch stacking scheme configuration guide
Acl2021 best paper released, from ByteDance
Review of in vivo detection
Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
There is no CUDA option in vs2019+cuda11.1 new project
I haven't delivered books for a long time, and I feel uncomfortable all over
pytorch中对BatchNorm2d()函数的理解
PCL各模块概述(1.6)
Is Damon partgroupdef a custom object?
Shell integrated application cases, archiving files, sending messages
食品安全 | 垃圾食品越吃越想吃?这份常见食品热量表请收好
QT learning (II) -- a brief introduction to QT Creator
吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
数据库性能系列之子查询
Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
面试京东 T5,被按在地上摩擦,鬼知道我经历了什么?