Cropping

Function Description

Crops the input image and outputs it to the Image object.

For details about the API, see cropping (single image) or cropping (batch).

API Call Process

Before calling the image cropping APIs, prepare the image object to be cropped.

Figure 1 Process of calling image processing (cropping) 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 cropped. Subsequently, the image can be processed by using the image processing APIs to generate a final Image object to be cropped.

  4. Construct the cropping Rect and the output Image.

    Select the one-to-many, one-to-one, or many-to-many cropping mode based on service requirements to construct the corresponding input Rect and output Image.

  5. Pass the input image and other parameters to the crop API to obtain the cropping result.
  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
from mindx.sdk import base  
from mindx.sdk.base import ImageProcessor, Rect, Image

def prcoess():
    # 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 processing (image cropping)
    crop_para = [Rect(300, 100, 550, 350)]  
    croped_images = imageProcessor.crop(decoded_image, crop_para)

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