Main Content

S.egment Image and Create Mask Using Color Thresholder App

此示例显示如何使用该图像进行段并使用该图像进行映射并创建二进制屏蔽映像颜色阈值app. The example segments the foreground (the peppers) from the background (the purple cloth) based on color values.

在里面颜色阈值app, image segmentation can be an iterative process. For example, try segmenting the image in several of the color spaces supported by the app because one color space might isolate a particular color better than another. In any of the supported color spaces, you can initially perform an automatic segmentation by selecting a region in the foreground or background. Then, you can refine the segmentation by using color component controls provided by the app.

该last part of this example shows how to save the results of your work, create a mask image, and get the MATLAB® code the app used to perform the segmentation.

在颜色阈值器中打开图像

Read a color image into the workspace.

rgb = imread('peppers.png');

打开颜色阈值app from the MATLAB toolstrip. On the Apps tab, in the图像处理和计算机视觉section点击颜色阈值

将图像加载到颜色阈值app. ClickLoad Image那和then selectLoad Image from Workspace.在里面Import From Workspace dialog box, select the image from the workspace, and then click

您还可以使用命令行打开应用程序colorThresholderfunction, specifying the name of the image:ColorthResholder(RGB);。您还可以获得图像Acquire Live Images in the Color Thresholder App

颜色阈值应用程序显示图像中的图像选择一个颜色空间标签,带点云表示这些颜色空间中的图像:RGB,HSV,YCBCR和l * a * b *。对于基于颜色的分割,请选择提供最佳颜色分离的颜色空间。使用鼠标,旋转点云表示,以查看它们如何隔离单个颜色。分割使用颜色阈值应用程序可以是迭代过程 - 在实现满足您需求的分段之前尝试多个不同的颜色空间。对于此示例,通过选择YCBCR颜色空间来启动该过程。

段ycbcr图像颜色阈值应用程序

When you choose a color space, the app opens a new tab, displaying the image along with a set of controls for each color component and the point cloud representation. The color controls vary depending on the color space. For the YCbCr color space, the颜色阈值应用程序显示表示三种颜色组件的三个直方图:ycomponent represents brightness, theCB.组件代表蓝黄色谱,以及CR.component represents the red-green spectrum.

To explore the image, move the cursor over the image to access the pan and zoom controls.

Automatic Thresholding

首先,使用自动阈值划分图像。因为背景颜色(紫色布)接近均匀的颜色,所以它而不是前景物体(辣椒)。您可以稍后始终倒置掩码逆变掩码选项。

使用Freehan ROI工具定义一个区域。单击按钮in the upper-left corner of the image and draw an ROI on the background. You can draw multiple regions.

绘制该地区后,颜色阈值应用程序根据您选择的区域中选择的颜色自动阈值阈值。该yCB.那和CR.color controls change to reflect the segmentation. This automatic thresholding does not create a clean segmentation of the background and foreground, especially at the lower border between the foreground and background. For this example, the background color is lighter near the bottom of the image. If you want to delete a region you drew and start over, right-click anywhere in the region and selectDelete Freehand

完善的自动阈值颜色Controls

要微调自动阈值,请使用颜色控件。对于每一个人yCB.那和CR.颜色控制,可以通过拖动该直方图中的下限和上限来设置值范围。使用这些颜色控件,您可以显着提高前景的分割。

使用点云的阈值图像颜色值

在YCBCR颜色空间中分割图像的另一种方法是在点云上绘制ROI以选择一系列颜色。

在App ToolStrip上,单击重置阈值恢复到原始图像。在应用程序的右下角,单击并拖动点云以旋转,直到隔离您对阈值处理感兴趣的颜色的视图。点击点云的左上角的按钮。该颜色阈值App将3-D点云转换为2-D表示,并激活多边形ROI工具。围绕您想要段(紫色)的颜色绘制ROI。该方法可以创建比初始自动阈值接近更好的分割。

另一个颜色空间中的段图像

要在另一个颜色空间中分段图像,请单击新的色彩空间在App ToolStrip中。在里面选择一个颜色空间tab, choose the HSV color space.

颜色阈值应用程序创建一个新的选项卡,显示HSV颜色空间的图像和颜色组件控件。在这个颜色空间,H代表色调,S.represents saturation, andV.代表价值。HSV颜色空间使用双向旋钮H组件和两个直方图滑块S.V.组件。该选项卡还包含图像中颜色的点云表示。

与在前一个迭代中一样,您可以使用所有相同的技术:自动阈值和交互式使用颜色组件控件,包括点云。使用颜色控件时,您可以看到正在进行的分段。在窗格中Hcontrol, change the range of the hue by clicking and dragging one arrow at a time. Experiment with the controls until you have a clean separation of the background from the foreground. You can clean up small imperfections after you create the mask image using toolbox functions, such as morphological operators.

CR.eate Mask Image Using Color Thresholder App

This part of the example shows how to create a mask image after segmentation. You can also get the segmented image and the MATLAB code used to create the mask image.

Because the example segmented the background (the purple cloth) rather than the foreground objects (the peppers), swap the foreground and background by clicking逆变掩码

V.iew the binary mask image that you created by clicking显示二进制在App ToolStrip上。

将掩码图像保存在工作区中。在面具工具架上,单击Export并选择出口图像

在里面Export To Workspace dialog box, specify variable names for the binary mask image. You can also save the original input RGB image and the segmented version of the original image.

要保存要重新创建分段所需的MATLAB代码,请单击Export并选择Export Function。颜色阈值应用程序将使用创建分段的代码打开MATLAB编辑器。要保存代码,请单击保存on the MATLAB Editor toolstrip. You can run this code, passing it an RGB image, to create the same mask image programmatically.

function[BW,maskedRGBImage] = createMask(RGB)%createmask阈值RGB映像使用ColorthResholder应用程序的自动生成的代码。%[BW,MaskedRGBimage] = CreateMask(RGB)阈值图像RGB使用% auto-generated code from the colorThresholder App. The colorspace andColorPace的每个通道的%最小/最大值已设置%app并导致二进制掩码bw和复合图​​像maskedrgbimage,%显示掩码BW下的原始RGB图像值。2016年6月22日的ColorthResholder应用程序自动生成%------------------------------------------------------%将RGB图像转换为选择的颜色空间I = rgb2hsv(RGB);% Define thresholds for channel 1 based on histogram settingschannel1Min = 0.713; channel1Max = 0.911;基于直方图设置,%定义通道2的阈值channel2Min = 0.049; channel2Max = 0.971;基于直方图设置,%定义通道3的阈值Channel3min = 0.005;channel3max = 1.000;% Create mask based on chosen histogram thresholdssliderbw =(i(:,:,1)> = channel1min)&(i(:,:,1)<= channel1max)&......(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) &......(i(:,:,3)> = channel3min)&(i(:,:,3)<= channel3max);bw = sliderbw;%逆变掩码BW = ~BW;% 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;结束

也可以看看