Image Cropping and Resizing
When programming with Ascend 310, you are advised to use digital vision pre-processing (DVPP) for image cropping and resizing.
Figure 1 shows the cropping/resizing process. This process crops the region of interest (ROI) and uses the cropped area for re-sampling. The process of cutting out the ROI is called cropping, and the process of re-sampling is called resizing. When the resizing coefficient is 1, only cropping is performed. When the original image is the cropping output, only resizing is performed.
The restrictions on cropping/resizing input are as follows:
- Input data address (OS virtual address): 16-byte aligned
- Buffer restriction for the input image width: 16-byte aligned
- Buffer restriction for the input image height: 2-byte aligned
To achieve zero-copy during high performance programming, the memory addresses assigned to users on the device (receive end) must meet the preceding restrictions when the memory is allocated. Generally, you can use either of the following methods depending on different input types:
- Method 1: If the app uses the host or other hardware for decoding, perform data cropping or padding on the host (transmit end) to meet the 16 x 2 alignment requirement. In this way, the Matrix module automatically allocates data memory that meets the preceding restrictions at the data receive end.
The input data in the sample is a YUV image. Therefore, the size of the image is calculated by using the following formula: (Image width x Image height) x 3/2. You need to change the formula according to the image format.
static const uint32_t ALIGN_W = 16; static const uint32_t ALIGN_H = 2; uint32_t imageWidth = 500; uint32_t imageHeight = 333; uint32_t imageSize = imageWidth * imageHeight *3/2; uint32_t align_width = image_width, align_height = image_height; // Performs width and height alignment. alignWidth = (alignWidth % ALIGN_W) ? alignWidth : (imageWidth + ALIGN_W)/ALIGN_W* ALIGN_W; alignHeight = (alignHeight % ALIGN_H) ? alignHeight : (imageHeight + ALIGN_H)/ALIGN_H * ALIGN_H; uint32_t alignSize = alignWidth * alignHeight *3/2; // Reads the file data and copies the aligned memory. FILE *fpIn = fopen(filePath.data(), "rb"); Uint8_t* imageBuffer = (uint8_t*) malloc(imageSize); HIAI_StatusT getRet = hiai::HIAIMemory::HIAI_DMalloc(fileLen, (void*&)alignBuffer, 10000); size_t size = fread(imageBuffer, 1, imageSize, fpIn); // Copies data. Uint32_t tempLen = imageWidth * imageHeight; if (alignWidth == imageWidth) { if(alignHeight > imageHeight) { memcpy_s(alignBuffer, tempLen, imageBuffer, tempLen); memcpy_s(alignBuffer + alignHeight * alignWidth, tempLen/2, imageBuffer + tempLen, tempLen/2); } } else { for(int32_t n=0;n<imageHeight;n++) { memcpy_s(alignBuffer+n*alingWidth,imageWidth, imageBuffer+n*imageWidth,imageWidth); } for(int32_t n=0;n<imageHeight/2;n++) { memcpy_s(alignBuffer+n*alignWidth+alignHeight* alignWidth, imageWidth, imageBuffer+n* imageWidth + imageHeight*imageWidth,imageWidth);//UV } }
- Method 2: Decode the image or video on the device to generate 16 x 2 aligned output that can be used as the input for DVPP VPC cropping and resizing.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot