From 6ac571e12a73f26655c7742ce3ac00e30b94c4b9 Mon Sep 17 00:00:00 2001 From: Vladimir Dudnik Date: Thu, 28 Oct 2021 12:58:08 +0300 Subject: [PATCH] eliminate odd memory copy (#8093) --- .../common/format_reader/opencv_wrapper.cpp | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/inference-engine/samples/common/format_reader/opencv_wrapper.cpp b/inference-engine/samples/common/format_reader/opencv_wrapper.cpp index 326ea165990..26161e082fe 100644 --- a/inference-engine/samples/common/format_reader/opencv_wrapper.cpp +++ b/inference-engine/samples/common/format_reader/opencv_wrapper.cpp @@ -27,22 +27,24 @@ OCVReader::OCVReader(const string& filename) { } std::shared_ptr OCVReader::getData(size_t width = 0, size_t height = 0) { - cv::Mat resized(img); - if (width != 0 && height != 0) { - size_t iw = img.size().width; - size_t ih = img.size().height; - if (width != iw || height != ih) { - slog::warn << "Image is resized from (" << iw << ", " << ih << ") to (" << width << ", " << height << ")" - << slog::endl; - } - cv::resize(img, resized, cv::Size(width, height)); - } + if (width == 0) + width = img.cols; - size_t size = resized.size().width * resized.size().height * resized.channels(); + if (height == 0) + height = img.rows; + + size_t size = width * height * img.channels(); _data.reset(new unsigned char[size], std::default_delete()); - for (size_t id = 0; id < size; ++id) { - _data.get()[id] = resized.data[id]; + + cv::Mat resized(width, height, img.type(), _data.get()); + + if (width != img.cols || height != img.rows) { + slog::warn << "Image is resized from (" << img.cols << ", " << img.rows << ") to (" << width << ", " << height + << ")" << slog::endl; } + // cv::resize() just copy data to output image if sizes are the same + cv::resize(img, resized, cv::Size(width, height)); + return _data; } #endif