Main Content

增强语义细分的像素标签

此示例显示了如何作为语义分割工作流程的一部分执行常见的图像和像素标签增强。

语义分割训练数据由数字矩阵和像素标签图像表示的图像组成,该图像由分类矩阵表示。当您增加培训数据时,必须将相同的转换应用于图像和相关的像素标签。此示例展示了三种常见类型的转换类型:

这example then shows how toapply augmentation to semantic segmentation training data in datastores结合多种类型的转换。

您可以使用增强培训数据来培训网络。有关如何训练语义分割网络的示例,请参见Semantic Segmentation Using Deep Learning(计算机视觉工具箱)

To demonstrate the effects of the different types of augmentation, each transformation in this example uses the same input image and pixel label image.

阅读示例图像。

filenameimage ='kobi.png';I = imread(filenameImage);

阅读像素标签图像。图像有两个类。

filenameLabels ='kobipixellabeled.png';l = imread(filenamelabels);class = ["floor",,,,"dog"]; ids = [1 2];

将像素标签图像转换为分类数据类型。

c =分类(l,ids,类);

Display the labels over the image by using theLabeloverlay函数。Pixels with the label "floor" have a blue tint and pixels with the label "dog" have a cyan tint.

b = labeloverlay(i,c);imshow(b)标题(“原始图像和像素标签”

图包含一个轴对象。这axes object with title Original Image and Pixel Labels contains an object of type image.

Resize Image and Pixel Labels

您可以使用imresize函数。Resize the image and the pixel label image to the same size, and display the labels over the image.

targetsize = [300 300];resizedI = imresize(I,targetSize); resizedC = imresize(C,targetSize);

Display the resized labels over the resized image.

B = labeloverlay(resizedI,resizedC); imshow(B) title(“调整图像和像素标签”

图包含一个轴对象。带标题大小的图像和像素标签的轴对象包含一个类型图像的对象。

作物图像和像素标签

裁剪是使数据与网络的输入大小相匹配的常见预处理步骤。要创建所需尺寸的输出图像,请首先使用RandomWindow2D(Image Processing Toolbox)Centercropwindow2d(Image Processing Toolbox)功能。确保选择一个裁剪窗口,其中包含图像中所需的内容。然后,通过使用图像和像素标签图像将图像裁剪到同一窗口imcrop

Specify the desired size of the cropped region as a two-element vector of the form [高度,,,,width].

targetsize = [300 300];

将图像从图像中心裁剪到目标大小。

win = centerCropWindow2d(尺寸(i),targetsize);croppedi = imcrop(i,win);croppedc = imcrop(c,win);

在裁剪图像上显示裁剪标签。

b = labeloverlay(croppedi,croppedc);imshow(b)标题(“中心裁剪图像和像素标签”

图包含一个轴对象。带有标题中心裁剪图像和像素标签的轴对象包含类型图像的对象。

Crop the image to the target size from a random position in the image.

win = RandomWindow2d(size(i),targetsize);croppedi = imcrop(i,win);croppedc = imcrop(c,win);

在裁剪图像上显示裁剪标签。

b = labeloverlay(croppedi,croppedc);imshow(b)标题('Random Cropped Image and Pixel Labels'

图包含一个轴对象。带有标题随机裁剪图像和像素标签的轴对象包含类型图像的对象。

经线图像和像素标签

randomAffine2d(Image Processing Toolbox)function creates a randomized 2-D affine transformation from a combination of rotation, translation, scaling (resizing), reflection, and shearing. Apply the transformation to images and pixel label images by usingIMWARP(Image Processing Toolbox)。控制扭曲输出的空间界限和分辨率affineOutputView(Image Processing Toolbox)函数。

通过从[-50,50]度范围内随机选择的角度旋转输入图像和像素标签图像。

tform = RandomAffine2d(“回转”,[-50 50]);

为扭曲的图像和像素标签图像创建输出视图。

dout = affineOutputView(size(i),tform);

UseIMWARP旋转图像和像素标签图像。

rotatedI = imwarp(I,tform,'OutputView',Rout);rotatedc = imwarp(c,tform,'OutputView',Rout);

Display the rotated labels over the rotated image.

B = labeloverlay(rotatedI,rotatedC); imshow(B) title(“旋转图像和像素标签”

图包含一个轴对象。带有标题旋转图像和像素标签的轴对象包含类型图像的对象。

Apply Augmentation to Semantic Segmentation Training Data in Datastores

Datastores are a convenient way to read and augment collections of images. Create a datastore that stores image and pixel label image data, and augment the data with a series of multiple operations.

创建包含图像和像素标签图像数据的数据存储

要增加样品数据存储的大小,请复制图像和像素标签图像的文件名。

numObservations = 4;trainimages = repelem({filenameimage},numobservations,1);trainlabels = repelem({filenamelabels},numobservations,1);

创建一个imageDatastorefrom the training image files. Create aPixellabeldatastore从培训像素标签文件中。数据存储包含多个相同数据的副本。

imds = imageDatastore(trainImages); pxds = pixelLabelDatastore(trainLabels,classes,ids);

通过组合图像数据存储和像素标签数据存储,将图像和像素标签对关联。

triendingdata = combine(imds,pxds);

从组合数据存储中读取第一个图像及其关联的像素标签图像。

data = read(triendingdata);i = data {1};C =数据{2};

显示图像和像素标签数据。

b = labeloverlay(i,c);imshow(b)

图包含一个轴对象。轴对象包含类型图像的对象。

应用数据增强

将数据扩展应用于培训数据转换函数。本例中执行两种布局方式te augmentations to the training data.

这first augmentation jitters the color of the image and then performs identical random scaling, horizontal reflection, and rotation on the image and pixel label image pairs. These operations are defined in thejitterImageColorAndWarp在此示例末尾的辅助功能。

augmentedTrainingData = transform(triendingdata,@jitterimagecolorandwarp);

阅读所有增强数据。

data = readall(augmentedTrainingData);

Display the augmented image and pixel label data.

RGB =单元格(NumObservations,1);为了k = 1:numObservations i = data {k,1};c = data {k,2};rgb {k} = labeloverlay(i,c);结尾蒙太奇(RGB)

图包含一个轴对象。轴对象包含类型图像的对象。

这second augmentation center crops the image and pixel label image to a target size. These operations are defined in thecenterCropImageAndLabel在此示例末尾的辅助功能。

targetsize = [800 800];preprocessedTrainingData =变换(增强培养Data,...@(data)centerCropImageAndLabel(data,targetSize));

阅读所有预处理数据。

data = readall(预处理trainingdata);

显示预处理图像和像素标签数据。

RGB =单元格(NumObservations,1);为了k = 1:numObservations i = data {k,1};c = data {k,2};rgb {k} = labeloverlay(i,c);结尾蒙太奇(RGB)

图包含一个轴对象。轴对象包含类型图像的对象。

助手功能增强功能

jitterImageColorAndWarphelper function applies random color jitter to the image data, then applies an identical affine transformation to the image and pixel label image data. The transformation consists of a random combination of scaling by a scale factor in the range [0.8 1.5], horizontal reflection, and rotation in the range [-30, 30] degrees. The inputdata和outputoutare two-element cell arrays, where the first element is the image data and the second element is the pixel label image data.

functionout = jitterImageColorAndWarp(data)% Unpack original data.i = data {1};C =数据{2};% Apply random color jitter.I = jitterColorHSV(I,“亮度”,,,,0.3,"Contrast",,,,0.4,“饱和”,0.2);%定义随机仿射变换。tform = RandomAffine2d("Scale",[0.8 1.5],,“ X反射”,真的,'回转',[-30 30]);dout = affineOutputView(size(i),tform);%变换图像和边界框标签。augmentedImage = imwarp(i,tform,"OutputView",Rout);augmentedlabel = imwarp(c,tform,"OutputView",Rout);%返回增强数据。out = {augmentedImage,augmentedlabel};结尾

centerCropImageAndLabelhelper function creates a crop window centered on the image, then crops both the image and the pixel label image using the crop window. The inputdata和outputoutare two-element cell arrays, where the first element is the image data and the second element is the pixel label image data.

functionout = centerCropImageAndLabel(data,targetSize) win = centerCropWindow2d(size(data{1}),targetSize); out{1} = imcrop(data{1},win); out{2} = imcrop(data{2},win);结尾

也可以看看

(Image Processing Toolbox)|(Image Processing Toolbox)|(Image Processing Toolbox)

相关示例

更多关于