1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief a header file with common samples functionality using OpenCV
12 #include <samples/common.hpp>
13 #include <opencv2/opencv.hpp>
16 * @brief Sets image data stored in cv::Mat object to a given Blob object.
17 * @param orig_image - given cv::Mat object with an image data.
18 * @param blob - Blob object which to be filled by an image data.
19 * @param batchIndex - batch index of an image inside of the blob.
22 void matU8ToBlob(const cv::Mat& orig_image, InferenceEngine::Blob::Ptr& blob, int batchIndex = 0) {
23 InferenceEngine::SizeVector blobSize = blob->getTensorDesc().getDims();
24 const size_t width = blobSize[3];
25 const size_t height = blobSize[2];
26 const size_t channels = blobSize[1];
27 T* blob_data = blob->buffer().as<T*>();
29 cv::Mat resized_image(orig_image);
30 if (static_cast<int>(width) != orig_image.size().width ||
31 static_cast<int>(height) != orig_image.size().height) {
32 cv::resize(orig_image, resized_image, cv::Size(width, height));
35 int batchOffset = batchIndex * width * height * channels;
37 for (size_t c = 0; c < channels; c++) {
38 for (size_t h = 0; h < height; h++) {
39 for (size_t w = 0; w < width; w++) {
40 blob_data[batchOffset + c * width * height + h * width + w] =
41 resized_image.at<cv::Vec3b>(h, w)[c];
48 * @brief Wraps data stored inside of a passed cv::Mat object by new Blob pointer.
49 * @note: No memory allocation is happened. The blob just points to already existing
51 * @param mat - given cv::Mat object with an image data.
52 * @return resulting Blob pointer.
54 static UNUSED InferenceEngine::Blob::Ptr wrapMat2Blob(const cv::Mat &mat) {
55 size_t channels = mat.channels();
56 size_t height = mat.size().height;
57 size_t width = mat.size().width;
59 size_t strideH = mat.step.buf[0];
60 size_t strideW = mat.step.buf[1];
63 strideW == channels &&
64 strideH == channels * width;
66 if (!is_dense) THROW_IE_EXCEPTION
67 << "Doesn't support conversion from not dense cv::Mat";
69 InferenceEngine::TensorDesc tDesc(InferenceEngine::Precision::U8,
70 {1, channels, height, width},
71 InferenceEngine::Layout::NHWC);
73 return InferenceEngine::make_shared_blob<uint8_t>(tDesc, mat.data);