Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / common / format_reader / opencv_wraper.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #ifdef USE_OPENCV
6 #include "opencv_wraper.h"
7 #include <fstream>
8 #include <iostream>
9
10 #include <opencv2/opencv.hpp>
11
12 #include <samples/slog.hpp>
13
14 using namespace std;
15 using namespace FormatReader;
16
17 OCVReader::OCVReader(const string &filename) {
18     img = cv::imread(filename);
19     _size = 0;
20
21     if (img.empty()) {
22         return;
23     }
24
25     _size   = img.size().width * img.size().height * img.channels();
26     _width  = img.size().width;
27     _height = img.size().height;
28 }
29
30 std::shared_ptr<unsigned char> OCVReader::getData(size_t width = 0, size_t height = 0) {
31     cv::Mat resized(img);
32     if (width != 0 && height != 0) {
33         size_t iw = img.size().width;
34         size_t ih = img.size().height;
35         if (width != iw || height != ih) {
36             slog::warn << "Image is resized from (" << iw << ", " << ih << ") to (" << width << ", " << height << ")" << slog::endl;
37         }
38         cv::resize(img, resized, cv::Size(width, height));
39     }
40
41     size_t size = resized.size().width * resized.size().height * resized.channels();
42     _data.reset(new unsigned char[size], std::default_delete<unsigned char[]>());
43     for (size_t id = 0; id < size; ++id) {
44         _data.get()[id] = resized.data[id];
45     }
46     return _data;
47 }
48 #endif