Cropping and Resizing (Single-Image, Single-ROI)

Principles

Sample Code

You can view the complete code in Sample Overview.

Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code sample of key steps for your reference only.

//1. Initialize AscendCL.
aclRet = aclInit(nullptr);

//2. Allocate runtime resources including the device, context, and stream in sequence.
aclrtContext context_;
aclrtStream stream_;
aclrtSetDevice(0);
aclrtCreateContext(&context_, 0);
aclrtCreateStream(&stream_);

//3. Create resizing configuration data and specify cropArea_.
//resizeConfig_ is of type acldvppResizeConfig.
resizeConfig_ = acldvppCreateResizeConfig();
aclError aclRet = acldvppSetResizeConfigInterpolation(resizeConfig_, 0);
//cropArea_ is of type acldvppRoiConfig.
cropArea_ = acldvppCreateRoiConfig(550, 749, 480, 679);

//4. Create description of the data processing channel. dvppChannelDesc_ is of type acldvppChannelDesc.
dvppChannelDesc_ = acldvppCreateChannelDesc();

//5. Create a data processing channel.
ret = acldvppCreateChannel(dvppChannelDesc_);

//6. Allocate input buffer (run mode specific).
//Call aclrtGetRunMode to obtain the run mode of the software stack. If ACL_DEVICE is returned, allocate and use the device buffer. If ACL_HOST is returned, transfer the input image data to the device by using the aclrtMemcpy call. After the data transfer is complete, the buffer needs to be freed in a timely manner.
aclrtRunMode runMode;
ret = aclrtGetRunMode(&runMode);
//inputPicWidth and inputPicHeight respectively indicate the width and height of an image after alignment. The following uses a YUV420SP image as an example.
uint32_t cropInBufferSize = inputPicWidth * inputPicHeight * 3 / 2;
if (runMode == ACL_HOST) { 
    //Allocate host buffer cropInHostBuffer.
    void* cropInHostBuffer = nullptr;
    cropInHostBuffer = malloc(cropInBufferSize);
    //Load the input image into the buffer. The ReadPicFile function is defined by the user.
    ReadPicFile(picName, cropInHostBuffer, cropInBufferSize);
    //Allocate the device buffer cropInDevBuffer_.
    aclRet = acldvppMalloc(&cropInDevBuffer_, cropInBufferSize);
    //Transfer the image data to the device by using the aclrtMemcpy call.
    aclRet = aclrtMemcpy(cropInDevBuffer_, cropInBufferSize, cropInHostBuffer, cropInBufferSize, ACL_MEMCPY_HOST_TO_DEVICE);
    //Free the buffer in a timely manner after data transfer is complete.
    free(cropInHostBuffer);
} else { 
    //Allocate input buffer cropInDevBuffer_ on the device.
    ret = acldvppMalloc(&cropInDevBuffer_, cropInBufferSize);
    //Load the input image into the buffer. The ReadPicFile function is defined by the user.
    ReadPicFile(picName, cropInDevBuffer_, cropInBufferSize);
}

//7. Allocate output buffer cropOutBufferDev_ on the device. The buffer size cropOutBufferSize_ is calculated based on the formula.
//outputPicWidth and outputPicHeight respectively indicate the width and height after image alignment. The following uses a YUV420SP image as an example.
uint32_t cropOutBufferSize_ = outputPicWidth * outputPicHeight * 3 / 2;
ret = acldvppMalloc(&cropOutBufferDev_, cropOutBufferSize_)

//8. Create the description of the input image and set the attribute values. cropInputDesc_ is of type acldvppPicDesc.
cropInputDesc_ = acldvppCreatePicDesc();
acldvppSetPicDescData(cropInputDesc_, cropInDevBuffer_);
acldvppSetPicDescFormat(cropInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(cropInputDesc_, inputWidth_);
acldvppSetPicDescHeight(cropInputDesc_, inputHeight_);
acldvppSetPicDescWidthStride(cropInputDesc_, inputWidthStride);
acldvppSetPicDescHeightStride(cropInputDesc_, inputHeightStride);
acldvppSetPicDescSize(cropInputDesc_, cropInBufferSize);

//9. Create the description of the output image and set the attribute values. cropOutputDesc_ is of type acldvppPicDesc.
//If the cropped output image is used as the input for model inference, the output width and height must meet the requirements of the model.
cropOutputDesc_ = acldvppCreatePicDesc();
acldvppSetPicDescData(cropOutputDesc_, cropOutBufferDev_);
acldvppSetPicDescFormat(cropOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); acldvppSetPicDescWidth(cropOutputDesc_, OutputWidth_);
acldvppSetPicDescHeight(cropOutputDesc_, OutputHeight_);
acldvppSetPicDescWidthStride(cropOutputDesc_, OutputWidthStride);
acldvppSetPicDescHeightStride(cropOutputDesc_, OutputHeightStride);
acldvppSetPicDescSize(cropOutputDesc_, cropOutBufferSize_);

//10. Perform asynchronous cropping and resizing, and call aclrtSynchronizeStream to wait for the stream tasks to complete.
ret = acldvppVpcCropResizeAsync(dvppChannelDesc_, cropInputDesc_,
        cropOutputDesc_, cropArea_, resizeConfig_, stream_);
ret = aclrtSynchronizeStream(stream_);

//11. After cropping and pasting are complete, destroy the allocations, including the description of the input and output image, input and output buffer, channel description, and channel.
acldvppDestroyRoiConfig(cropArea_);
acldvppDestroyResizeConfig(resizeConfig_);
acldvppDestroyPicDesc(cropInputDesc_);
acldvppDestroyPicDesc(cropOutputDesc_);
if (runMode == ACL_HOST) { 
    //In this mode, the processing result is on the device. Therefore, you need to call the memory copy API to transfer the result data, and then free the device buffer.
    //Allocate host buffer cropOutHostBuffer.
    void* cropOutHostBuffer = nullptr;
    cropOutHostBuffer = malloc(cropOutBufferSize_);
    //Transfer the processing result of the device to the host by using the aclrtMemcpy call.
    aclRet = aclrtMemcpy(cropOutHostBuffer, cropOutBufferSize_, cropOutBufferDev_, cropOutBufferSize_, ACL_MEMCPY_DEVICE_TO_HOST);
    //Free the input and output buffer on the device.
    (void)acldvppFree(cropInDevBuffer_);
    (void)acldvppFree(cropOutBufferDev_);
    //Free the buffer after the data is used.
    free(cropOutHostBuffer);
} else { 
    //The process is running on the device, and the processing result is also on the device. Free the device buffer when the data is no longer needed.
    (void)acldvppFree(cropInDevBuffer_);
    (void)acldvppFree(cropOutBufferDev_);
}
acldvppDestroyChannel(dvppChannelDesc_);
(void)acldvppDestroyChannelDesc(dvppChannelDesc_);
dvppChannelDesc_ = nullptr;

//12. Release runtime resources including the stream, context, and device in sequence.
aclrtDestroyStream(stream_);
aclrtDestroyContext(context_);
aclrtResetDevice(0);

//13. Deinitialize AscendCL.
aclRet = aclFinalize();

//...