使用抠图接口前,用户需准备好被抠图的图片并转换到Tensor对象中。
关键步骤说明如下:
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 |
//初始化 MxBase::MxInit(); { // 读取图片 std::string imgPath = "./test.jpg"; cv::Mat imgData = cv::imread(imgPath, cv::IMREAD_UNCHANGED); std::vector<uint32_t> shape{600, 600, 3}; // 构造输入tensor MxBase::Tensor input(imgData.data, shape, MxBase::TensorDType::UINT8, -1); input.ToDvpp(0); // 设置抠图区域 MxBase::Rect rect(0, 0, 320, 320); // 构造输出tensor std::vector<uint32_t> dstShape = {320, 320, 3}; MxBase::Tensor dst(dstShape, MxBase::TensorDType::UINT8, -1); dst.Malloc(); dst.ToDevice(0); // 设置输出tensor是否保留无效边界 bool keepMargin = true; // 执行抠图操作 APP_ERROR ret = MxBase::Crop(input, rect, dst, keepMargin); if (ret != APP_ERR_OK) { std::cout << "Crop failed." << std::endl; } } //去初始化 MxBase::MxDeInit(); |