MATLAB颜色阈值工具箱(Color Thresholder)介绍

一、MATLAB Color Thresholder介绍

Color Thresholder工具箱可以根据不同颜色空间对颜色通道设置阈值,从而分割彩色图像。使用此工具箱,可以为彩色图像创建二值分割掩膜。

Color Thresholder工具箱支持四种颜色空间的分割。在每个颜色空间中,该工具箱将图像、三个颜色通道和所有像素的颜色值显示为三维颜色空间图中的点。可以通过为颜色通道值设置窗口或者在图像或三维颜色空间图中绘制 ROI 来选择掩膜中包含的颜色。

二、Color Thresholder基础教程

2.1 进入工具箱

打开应用程序->图像处理计算机视觉->Color Thresholder
在这里插入图片描述
也可以直接在命令行窗口输出命令:colorThresholder进入工具箱
在这里插入图片描述

2.2 导入图像

原始图像如下:
在这里插入图片描述

导入图像选择Load Image:
在这里插入图片描述
这里将气球图像ball加载到应用程序中,可以看到有四种颜色样式,分别为RGB,HSV,YCbCr以及Lab*。对于基于颜色的分割,请选择提供最佳颜色分离的颜色空间。使用鼠标旋转点云表示,以查看它们如何隔离各个颜色。使用Color Thresholder应用程序进行分割可能是一个迭代过程。在实现满足您需要的分割之前,请尝试几种不同的颜色空间。对于本例,通过选择HSV颜色空间开始该过程。
在这里插入图片描述

2.3 调整阈值

使用图形界面调整HSV通道参数,并在界面窗口中观察结果,当结果令人满意时,选择应用程序窗口右上角的导出图标,将结果导出为图像或函数。
在这里插入图片描述

2.4 导出为函数

导出MATLAB函数后可以将相同的阈值参数应用于MATLAB中的任何图像,而无需重新使用Color Thresholder应用程序。这对于自动化很有用。
在这里插入图片描述

导出函数源代码的示例代码如下。

function [BW,maskedRGBImage] = createMask(RGB)
%createMask  Threshold RGB image using auto-generated code from colorThresholder app.
%  [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
%  auto-generated code from the colorThresholder app. The colorspace and
%  range for each channel of the colorspace were set within the app. The
%  segmentation mask is returned in BW, and a composite of the mask and
%  original RGB images is returned in maskedRGBImage.

% Auto-generated by colorThresholder app on 09-Nov-2021
%------------------------------------------------------


% Convert RGB image to chosen color space
I = rgb2hsv(RGB);

% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.941;
channel1Max = 0.998;

% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.327;
channel2Max = 1.000;

% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.292;
channel3Max = 1.000;

% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
    (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
    (I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;

% Initialize output masked image based on input image.
maskedRGBImage = RGB;

% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;

end

请注意此代码中定义的通道最小值和最大值。要使用该函数,请确保将该函数从createMask重命名为更具体的名称并保存。


参考:

  1. https://ww2.mathworks.cn/help/images/ref/colorthresholder-app.html
  2. https://www.mathworks.com/help/images/image-segmentation-using-the-color-thesholder-app.html
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wendy_ya

您的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值