Files
openvino/samples/cpp/common/format_reader/opencv_wrapper.cpp

54 lines
1.4 KiB
C++
Raw Normal View History

// Copyright (C) 2018-2022 Intel Corporation
2018-10-16 13:45:03 +03:00
// SPDX-License-Identifier: Apache-2.0
//
#ifdef USE_OPENCV
# include <fstream>
# include <iostream>
// clang-format off
# include <opencv2/opencv.hpp>
# include "samples/slog.hpp"
# include "opencv_wrapper.h"
// clang-format on
2018-10-16 13:45:03 +03:00
using namespace std;
using namespace FormatReader;
OCVReader::OCVReader(const string& filename) {
2018-10-16 13:45:03 +03:00
img = cv::imread(filename);
_size = 0;
if (img.empty()) {
return;
}
_size = img.size().width * img.size().height * img.channels();
_width = img.size().width;
2018-10-16 13:45:03 +03:00
_height = img.size().height;
}
2019-04-12 18:25:53 +03:00
std::shared_ptr<unsigned char> OCVReader::getData(size_t width = 0, size_t height = 0) {
2021-10-28 12:58:08 +03:00
if (width == 0)
width = img.cols;
if (height == 0)
height = img.rows;
2018-10-16 13:45:03 +03:00
2021-10-28 12:58:08 +03:00
size_t size = width * height * img.channels();
2018-10-16 13:45:03 +03:00
_data.reset(new unsigned char[size], std::default_delete<unsigned char[]>());
2021-10-28 12:58:08 +03:00
cv::Mat resized(cv::Size(width, height), img.type(), _data.get());
2021-10-28 12:58:08 +03:00
if (width != img.cols || height != img.rows) {
slog::warn << "Image is resized from (" << img.cols << ", " << img.rows << ") to (" << width << ", " << height
<< ")" << slog::endl;
2018-10-16 13:45:03 +03:00
}
2021-10-28 12:58:08 +03:00
// cv::resize() just copy data to output image if sizes are the same
cv::resize(img, resized, cv::Size(width, height));
2018-10-16 13:45:03 +03:00
return _data;
}
#endif