Padding

Function Description

Pads an input image and outputs the image to the Image object.

For details about the API, see padding.

API Call Process

Before calling the padding APIs, prepare the image object to be padded.

Figure 1 Process of calling image processing (padding) APIs

Key steps are demonstrated as follows:

  1. Perform global initialization by calling mx_init().
  2. Initialize ImageProcessor.

    Construct the ImageProcessor object. You need to specify the device ID during the construction.

  3. Call the image decoding API to decode the input image.

    Decode the image based on services to generate an Image object that can be padded. Subsequently, the image can be processed by using the image processing APIs to generate a final Image object to be padded.

  4. Construct the padding parameters.

    Construct the input padDim (indicating the pixel values of the upper, lower, left, and right sides of the padded edge) and color (color of the padded edge) based on the service requirements.

  5. Pad the input image by calling the padding API.
  6. Perform deinitialization by calling mx_deinit().

Sample Code

The following is a code example of key steps of functions and features, which is for reference only and cannot be directly copied for execution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from mindx.sdk import base  
from mindx.sdk.base import ImageProcessor, Dim, Color, Image

def process():
    # Image decoding
    # Initialize the ImageProcessor object.
    imageProcessor = ImageProcessor(device_id)  
    image_path = "image_data/test_image.jpg"  
    # Read the image path for decoding. The decoding format is nv12 (YUV_SP_420).
    decoded_image = imageProcessor.decode(image_path, base.nv12)  
    
    # Image padding
    # Border size
    dim_para = Dim(100, 100, 100, 100)  
    # Pad the decoded Image class based on Dim by repeating the last element.
    padded_image = imageProcessor.padding(decoded_image, dim_para, Color(0, 0, 0), base.border_replicate)  

if __name__ == "__main__":
    base.mx_init()   # Initialize resources.
    process()
    base.mx_deinit() # Deinitialize resources.