Crops and resizes the input image, and outputs the image to the tensor object.
For details about the API, see CropResize.
Before calling the cropping and resizing API, prepare an image to be cropped and resized and convert it to a tensor object.
Key steps are demonstrated as follows:
Construct the input Rect and Size as well as the output Tensor based on service requirements.
No stream is created. The input image and other parameters are passed to the CropResize method to obtain the image cropping and resizing 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 |
// 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 and resizing width and height. MxBase::Rect rect(0, 0, 320, 320); std::vector<MxBase::Rect> cropRectVec = {rect}; MxBase::Size size(480, 480); std::vector<MxBase::Size>sizeVec = {size}; // Construct an output tensor. std::vector<uint32_t> dstShape = {480, 480, 3}; MxBase::Tensor dst(dstShape, MxBase::TensorDType::UINT8, -1); dst.Malloc(); dst.ToDvpp(0); std::vector<MxBase::Tensor> outputTensorVec = {dst}; // Perform cropping and resizing. APP_ERROR ret = MxBase::CropResize(input, cropRectVec, sizeVec, outputTensorVec, MxBase::Interpolation::BILINEAR_SIMILAR_OPENCV, true); if (ret != APP_ERR_OK) { std::cout << "CropResize failed." << std::endl; } } // Deinitialization MxBase::MxDeInit(); |