Crops the input image, pastes it to the background image, and outputs it to the Image object.
For details about the API, see CropAndPaste.
Before calling the image cropping and pasting API, prepare the image object to be cropped and pasted.
Key steps are demonstrated as follows:
Construct an ImageProcessor object and initialize the channel by calling InitVpcChannel(). If you skip this API, ImageProcessor automatically initializes the channel before cropping and pasting.
Decode the image based on services to generate an Image object that can be cropped and pasted. Subsequently, the image can be processed by using the image processing APIs to generate a final Image object to be cropped and pasted.
No stream is created. The input image and other parameters are passed to the API to obtain the image cropping and pasting result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// Initialization MxInit(); { // Construct the image processing class. ImageProcessor imageProcessor(deviceId); // Decode the image for Image generation. // Decoded image class Image decodedImage; // Perform decoding based on the image path. APP_ERROR ret = imageProcessor.Decode(imagePath, decodedImage); if (ret != APP_ERR_OK) { std::cout << "Decode failed." << std::endl; } // (Optional) Initialize the image processing channel. imageProcessor.InitVpcChannel(); // Construct the background Image. (You can also select another Image object with existing data.) Size imageSize(640, 640); size_t dataSize = 640 * 640 * 3 / 2; MemoryData imgData(dataSize, MemoryData::MemoryType::MEMORY_DVPP, deviceId); if (MemoryHelper::MxbsMalloc(imgData) != APP_ERR_OK) { std::cout << "Malloc failed." << std::endl; } std::shared_ptr<uint8_t> pastedData((uint8_t*)imgData.ptrData, imgData.free); // Image class after cropping and pasting. Image pastedImage(pastedData, dataSize, deviceId, imageSize); // Perform image cropping and pasting. // Cropping and pasting sizes Rect rectFrom(0, 0, 240, 240); Rect rectTo(0, 0, 480, 480); std::pair<Rect, Rect> cropPasteRect = {rectFrom, rectTo}; // Image cropping and pasting ret = imageProcessor.CropAndPaste(resizeImage, cropPasteRect, pastedImage) ; if (ret != APP_ERR_OK) { std::cout << "CropAndPaste failed." << std::endl; } } // Deinitialization MxDeInit(); |