主要内容

Augment Bounding Boxes for Object Detection

This example shows how to perform common kinds of image and bounding box augmentation as part of object detection workflows.

Object detector training data consists of images and associated bounding box labels. When you augment training data, you must apply identical transformations to the image and associated bounding boxes. This example demonstrates three common types of transformations:

然后示例显示了如何将增强应用于数据存储的培训数据结合多种类型的转换。

You can use augmented training data to train a network. For an example showing how to train an object detection network, seeObject Detection Using Faster R-CNN Deep Learning(Computer Vision Toolbox)

Read and display a sample image and bounding box. To compare the effects of the different types of augmentation, each transformation in this example uses the same input image and bounding box.

filenameImage ='kobi.png'; I = imread(filenameImage); bbox = [4 156 1212 830]; label =“狗”;

Display the image and bounding box.

annotatedImage = insertShape(I,"rectangle",bbox,“行宽”,8); imshow(annotatedImage) title('Original Image and Bounding Box')

图包含一个轴对象。带有标题原始图像和边界框的轴对象包含类型图像的对象。

Resize Image and Bounding Box

Useimresize缩放图像的比例为2。

scale = 1/2; J = imresize(I,scale);

Usebboxresizeto apply the same scaling to the associated bounding box.

bboxResized = bboxresize(bbox,scale);

显示调整大小的图像和边界框。

注释图=插入形状(j,"rectangle",bboxResized,“行宽”,8); imshow(annotatedImage) title('Resized Image and Bounding Box')

图包含一个轴对象。The axes object with title Resized Image and Bounding Box contains an object of type image.

Crop Image and Bounding Box

Cropping is a common preprocessing step to make the data match the input size of the network. To create output images of a desired size, first specify the size and position of the crop window by using therandomWindow2d(Image Processing Toolbox)或者centerCropWindow2d(Image Processing Toolbox)功能。Make sure you select a cropping window that includes the desired content in the image. Then, crop the image and pixel label image to the same window by usingimcrop

将裁切区域的所需大小指定为形式的两元素矢量[height,宽度]。

targetsize = [1024 1024];

Crop the image to the target size from the center of the image by usingimcrop

win = centerCropWindow2d(尺寸(i),targetsize);j = imcrop(i,win);

使用相同的作物窗口裁剪边界盒bboxcrop。SpecifyOverlapThresholdas a value less than 1 so that the function clips the bounding boxes to the crop window instead of discarding them when the crop window does not completely enclose the bounding box. The overlap threshold enables you to control the amount of clipping that is tolerable for objects in your images. For example, clipping more than half a person is not useful for training a person detector, whereas clipping half a vehicle might be tolerable.

[bboxCropped,valid] = bboxcrop(bbox,win,"OverlapThreshold",0.7);

Keep labels that are inside the cropping window.

label = label(valid);

Display the cropped image and bounding box.

注释图=插入形状(j,"rectangle",bboxCropped,“行宽”,8); imshow(annotatedImage) title('Cropped Image and Bounding Box')

图包含一个轴对象。带有标题裁剪图像和边界框的轴对象包含类型图像的对象。

Crop and Resize Image and Bounding Box

Cropping and resizing are often performed together. You can usebboxcropbboxresize在串联以实施常用的“作物和调整大小”转换ation.

Create a crop window from a random position in the image. Crop the image and bounding box to the same crop window.

作作= [1024 1024];win = RandomWindow2d(size(i),compopsize);j = imcrop(i,win);chroppedbox = bboxcrop(bbox,win,"OverlapThreshold",0.5);

将图像和框调整到目标大小。

targetsize = [512 512];j = imresize(j,targetsize);裁剪和质量盒= bboxresize(裁剪盒,靶size。/copopsize);

Display the cropped and resized image and bounding box.

注释图=插入形状(j,"rectangle",裁剪和构造盒,“行宽”,8); imshow(annotatedImage) title(“作物和大小的图像和边界框”)

图包含一个轴对象。The axes object with title Crop and Resized Image and Bounding Box contains an object of type image.

Warp Image and Bounding Box

TherandomAffine2d(Image Processing Toolbox)功能creates a randomized 2-D affine transformation from a combination of rotation, translation, scaling (resizing), reflection, and shearing. Warp an image by usingimwarp(Image Processing Toolbox)。Warp bounding boxes by usingbboxwarp。Control the spatial bounds and resolution of the warped output by using theaffineOutputView(Image Processing Toolbox)功能。

This example demonstrates two of the randomized affine transformations: scaling and rotation.

Random Scale

Create a scale transformation that resizes the input image and bounding box using a scale factor selected randomly from the range [1.5,1.8]. This transformation applies the same scale factor in the horizontal and vertical directions.

tform = randomAffine2d("Scale",[1.5 1.8]);

Create an output view for the affine transform.

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

Rescale the image usingimwarp和rescale the bounding box usingbboxwarp。Specify anOverlapThreshold值为0.5。

j = imwarp(i,tform,"OutputView",Rout);bboxscaled = bboxwarp(bbox,tform,dout,"OverlapThreshold",0.5);

Display the scaled image and bounding box.

注释图=插入形状(j,"rectangle",bboxScaled,“行宽”,8); imshow(annotatedImage) title(“缩放图像和边界框”)

图包含一个轴对象。The axes object with title Scaled Image and Bounding Box contains an object of type image.

Random Rotation

Create a randomized rotation transformation that rotates the image and box labels by an angle selected randomly from the range [-15,15] degrees.

tform = randomAffine2d("Rotation",[-15 15]);

Create an output view forimwarpbboxwarp

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

Rotate the image usingimwarp和rotate the bounding box usingbboxwarp。Specify anOverlapThreshold值为0.5。

j = imwarp(i,tform,"OutputView",Rout);bboxRotated = bboxwarp(bbox,tform,dout,"OverlapThreshold",0.5);

Display the cropped image and bounding box. Note that the bounding box returned bybboxwarpis always aligned to the image axes. The size and aspect ratio of the bounding box changes to accommodate the rotated object.

注释图=插入形状(j,"rectangle",bboxRotated,“行宽”,8); imshow(annotatedImage) title(“旋转图像和边界框”)

图包含一个轴对象。The axes object with title Rotated Image and Bounding Box contains an object of type image.

Apply Augmentation to Training Data in Datastores

数据存储是一个便捷的途径,阅读和augment collections of data. Create a datastore that stores image and bounding box data, and augment the data using a series of multiple operations.

创建包含图像和边界框数据的数据存储

增加样本数据存储的大小,代表licate the file names of the image and the bounding box and labels.

numObservations = 4;images = repelem({filenameimage},numobservations,1);bboxes = repelem({bbox},numobservations,1);labels = repelem({label},numobservations,1);

Create an成像from the training image files. Combine the bounding box and label data in a table, then create aboxLabelDatastore从桌子上。

imds = imageDatastore(images); tbl = table(bboxes,labels); blds = boxLabelDatastore(tbl);

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

trainingData = combine(imds,blds);

Read the first image and its associated box label from the combined datastore.

数据= read(trainingData); I = data{1}; bboxes = data{2}; labels = data{3};

Display the image and box label data.

annotatedImage = insertObjectAnnotation(I,'rectangle',bbox,labels,。。。'LineWidth',8,'FontSize',40);imshow(注释图)

图包含一个轴对象。The axes object contains an object of type image.

Apply Data Augmentation

Apply data augmentation to the training data by using the转换功能。This example performs two separate augmentations to the training data.

The first augmentation jitters the color of the image and then performs identical random horizontal reflection and rotation on the image and box label pairs. These operations are defined in thejitterimagecolorandwarphelper function at the end of this example.

augmentedTrainingData = transform(trainingData,@jitterImageColorAndWarp);

阅读所有增强数据。

数据= readall(augmentedTrainingData);

Display the augmented image and box label data.

rgb = cell(numObservations,1);fork = 1:numObservations I = data{k,1}; bbox = data{k,2}; labels = data{k,3}; rgb{k} = insertObjectAnnotation(I,'rectangle',bbox,labels,'LineWidth',8,'FontSize',40);endmontage(rgb)

图包含一个轴对象。The axes object contains an object of type image.

第二次增强将图像和框标签重新缩放为目标尺寸。这些操作在resizeImageAgeAndlabelhelper function at the end of this example.

targetSize = [300 300]; preprocessedTrainingData = transform(augmentedTrainingData,。。。@(data)resizeImageAndLabel(data,targetSize));

Read all of the preprocessed data.

data = readall(预处理trainingdata);

Display the preprocessed image and box label data.

rgb = cell(numObservations,1);fork = 1:numObservations I = data{k,1}; bbox = data{k,2}; labels = data{k,3}; rgb{k} = insertObjectAnnotation(I,'rectangle',bbox,labels,。。。'LineWidth',8,'FontSize',15);endmontage(rgb)

图包含一个轴对象。The axes object contains an object of type image.

Helper Functions for Augmentation

Thejitterimagecolorandwarphelper function applies random color jitter to the image data, then applies an identical affine transformation to the image and box label data. The transformation consists of random horizontal reflection and rotation. The input数据和outputoutare two-element cell arrays, where the first element is the image data and the second element is the box label data.

功能out = jitterImageColorAndWarp(data)% Unpack original data.I = data{1}; boxes = data{2}; labels = data{3};%应用随机颜色抖动。I = jitterColorHSV(I,"Brightness",0.3,"Contrast",0.4,"Saturation",0.2);%定义随机仿射变换。tform = randomAffine2d("XReflection",true,'Rotation',[-30 30]);dout = affineOutputView(size(i),tform);% Transform image and bounding box labels.augmentedImage = imwarp(i,tform,"OutputView",Rout);[augmentedboxes,有效] = bboxwarp(盒子,tform,dout,'OverlapThreshold',0.4);AugmentedLabels =标签(有效);% Return augmented data.out = {augmentedImage,augmentedBoxes,augmentedLabels};end

TheresizeImageAgeAndlabel辅助功能计算图像匹配目标大小的比例因子,然后使用imresize和盒子标签使用bboxresize。输入数据和输出数据是两元素单元格数组,其中第一个元素是图像数据,第二个元素是框标签数据。

功能data = resizeImageAndlabel(data,targetsize)scale = targetsize./size(Data {1} ,、 [1 2]);data {1} = imResize(data {1},targetSize);data {2} = bboxResize(data {2},比例);end

See Also

(Computer Vision Toolbox)|(Computer Vision Toolbox)|(Computer Vision Toolbox)||(Image Processing Toolbox)|(Image Processing Toolbox)|(Image Processing Toolbox)

Related Examples

More About