- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I've been working on a model that requires normalized image values i.e image/255. the images read by opencv are U8, so i first convert them to CV_32FC3 and than normalize but cant understand how to convert this mat to blob. There are 2 functions that most samples are using but these convert MatU8 to blob, so how to accurately convert a CV_32FC3 mat to blob? The functions in the samples are as follows:
template <typename T>
void matU8ToBlob(const cv::Mat& orig_image, InferenceEngine::Blob::Ptr& blob, int batchIndex = 0) {
InferenceEngine::SizeVector blobSize = blob->getTensorDesc().getDims();
const size_t width = blobSize[3];
const size_t height = blobSize[2];
const size_t channels = blobSize[1];
T* blob_data = blob->buffer().as<T*>();
cv::Mat resized_image(orig_image);
if (static_cast<int>(width) != orig_image.size().width ||
static_cast<int>(height) != orig_image.size().height) {
cv::resize(orig_image, resized_image, cv::Size(width, height));
}
int batchOffset = batchIndex * width * height * channels;
for (size_t c = 0; c < channels; c++) {
for (size_t h = 0; h < height; h++) {
for (size_t w = 0; w < width; w++) {
blob_data[batchOffset + c * width * height + h * width + w] =
resized_image.at<cv::Vec3b>(h, w)
}
}
}
}
InferenceEngine::Blob::Ptr wrapMat2Blob(const cv::Mat &mat) {
size_t channels = mat.channels();
size_t height = mat.size().height;
size_t width = mat.size().width;
size_t strideH = mat.step.buf[0];
size_t strideW = mat.step.buf[1];
bool is_dense =
strideW == channels &&
strideH == channels * width;
if (!is_dense) THROW_IE_EXCEPTION
<< "Doesn't support conversion from not dense cv::Mat";
InferenceEngine::TensorDesc tDesc(InferenceEngine::Precision::U8,
{1, channels, height, width},
InferenceEngine::Layout::NHWC);
return InferenceEngine::make_shared_blob<uint8_t>(tDesc, mat.data);
}
Link Copied

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page