Main Content

preprocess

Preprocess training and test images

Since R2021a

    Description

    outputData = preprocess(detector,trainingData) preprocesses the training data trainingData before using it to train the YOLO v3 object detector. The training images and the corresponding bounding boxes are stored in the trainingData. The preprocess function performs these operations:

    • Rescales the intensity values of the training images to the range [0, 1].

    • Resizes the training images to one of the nearest network input sizes and updates the bounding box coordinate values for accurate training. The function preserves the original aspect ratio of the training data.

    example

    outputImg = preprocess(detector,img) preprocesses the test images img for object detection using a YOLO v3 object detector. The preprocess function performs these operations:

    • Rescales the intensity values of the test images to the range [0, 1].

    • Resizes the test images to one of the nearest network input sizes and preserves the original aspect ratio of each test image.

    example

    [___,scaleInfo] = preprocess(___) returns information on the scale factor applied for image resizing, in addition to any combination of arguments from previous syntaxes.

    Examples

    collapse all

    Load a pretrained YOLO v3 object detector.

    detector = yolov3ObjectDetector('tiny-yolov3-coco');

    Load the training dataset into the workspace. The training data is a cell array that contains three training images and the corresponding bounding box values and class labels.

    load('trainingData.mat','trainingData');

    Resize the training images to the nearest network input size and rescale the intensity values by using the preprocess function.

    [outputData,info] = preprocess(detector,trainingData);

    Display the output images and the scale information used for resizing the images.

    figure
    montage({outputData{1,1},outputData{2,1},outputData{3,1}},Size=[1 3])
    title("Preprocessed Output Image")

    Figure contains an axes object. The hidden axes object with title Preprocessed Output Image contains an object of type image.

    ScaleX = [info{1,1}.ScaleX;info{2,1}.ScaleX;info{3,1}.ScaleX];
    ScaleY = [info{1,1}.ScaleY;info{2,1}.ScaleY;info{3,1}.ScaleY];
    table(ScaleX,ScaleY)
    ans=3×2 table
         ScaleX       ScaleY  
        _________    _________
    
        0.0072115    0.0072115
        0.0072115    0.0072115
        0.0072115    0.0072115
    
    

    Display the input and the preprocessed image size and bounding box values.

    bboxIn = cell2table(trainingData,'VariableNames',{'Images','Bounding Boxes','Labels'})
    bboxIn=3×3 table
             Images               Bounding Boxes           Labels   
        _________________    ________________________    ___________
    
        {224x399x3 uint8}    220    136     35     28    {'vehicle'}
        {224x399x3 uint8}    175    126     61     45    {'vehicle'}
        {224x399x3 uint8}    108    120     45     33    {'vehicle'}
    
    
    bboxOut = cell2table(outputData,'VariableNames',{'Images','Bounding Boxes','Labels'})
    bboxOut=3×3 table
              Images               Bounding Boxes           Labels   
        __________________    ________________________    ___________
    
        {416x416x3 single}    229    232     36     29    {'vehicle'}
        {416x416x3 single}    182    222     64     46    {'vehicle'}
        {416x416x3 single}    112    215     47     35    {'vehicle'}
    
    

    Load a pretrained YOLO v3 object detector.

    detector = yolov3ObjectDetector('tiny-yolov3-coco');

    Read a test image.

    I = imread('highway.png');

    Resize the test image to the network input size and rescale the intensity values by using the preprocess function.

    [outputImg,scaleInfo] = preprocess(detector,I);

    Display the output image and the scale information used for resizing the image.

    figure
    imshow(outputImg)

    Figure contains an axes object. The hidden axes object contains an object of type image.

    disp(scaleInfo)
                       ScaleX: 0.7692
                       ScaleY: 0.5769
        PreprocessedImageSize: [416 416 3]
    

    Input Arguments

    collapse all

    YOLO v3 object detector, specified as a yolov3ObjectDetector object.

    Training data for YOLO v3 object detector, specified as a N-by-3 cell array that contains the images, bounding boxes, and the class labels. Each row is of the form [images bounding boxes labels]. N is the number of output layers in the network. The bounding boxes must be stored as a K-by-4 matrix of the form [x y width height]. K is the number of object classes.

    Test images, specified as a numeric array of size M-by-N-by-C or M-by-N-by-C-by-T. M is the number of rows, N is the number of columns, and C is the number of color channels. The value of C is 1 for grayscale images and 3 for RGB color images. T is the number of test images in the array.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Output Arguments

    collapse all

    Preprocessed training data, returned as a N-by-3 cell array.

    Data Types: cell

    Preprocessed test images, returned as a numeric array of size P-by-Q-by-C or P-by-Q-by-C-by-T. P and Q are the number of rows and columns, respectively, in the preprocessed image.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Information about the scale factor for resizing the input images, returned as a structure with fields PreprocessedImageSize, ScaleX, and ScaleY.

    • PreprocessedImageSize — Size of the output resized image.

    • ScaleX — Scale factor for resizing an image in the X-direction (along the rows).

    • ScaleY — Scale factor for resizing an image in the Y-direction (along the columns).

    Data Types: struct

    Version History

    Introduced in R2021a