Crops the input image and outputs it to the tensor object.
For details about the API, see Crop.
Before calling the image cropping API, prepare the image to be cropped and convert it into a tensor object.
Key steps are demonstrated as follows:
Select the one-to-one or one-to-many image cropping mode based on service requirements to construct the corresponding input Rect and output tensor.
No stream is created. The input image and other parameters are passed to the Crop method to obtain the image cropping 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 |
// Initialization MxBase::MxInit(); { // Image reading std::string imgPath = "./test.jpg"; cv::Mat imgData = cv::imread(imgPath, cv::IMREAD_UNCHANGED); std::vector<uint32_t> shape{600, 600, 3}; // Construct an input tensor. MxBase::Tensor input(imgData.data, shape, MxBase::TensorDType::UINT8, -1); input.ToDvpp(0); // Set the cropped area. MxBase::Rect rect(0, 0, 320, 320); // Construct an output tensor. std::vector<uint32_t> dstShape = {320, 320, 3}; MxBase::Tensor dst(dstShape, MxBase::TensorDType::UINT8, -1); dst.Malloc(); dst.ToDevice(0); // Whether to reserve invalid boundaries for the output tensor bool keepMargin = true; // Perform cropping. APP_ERROR ret = MxBase::Crop(input, rect, dst, keepMargin); if (ret != APP_ERR_OK) { std::cout << "Crop failed." << std::endl; } } // Deinitialization MxBase::MxDeInit(); |