utility.cpp
Go to the documentation of this file.
1 #include "utility.hpp"
2 
3 // libraries
4 #include "fp16/fp16.h"
5 
6 #include "errno.h"
7 
8 #if (defined(_WIN32) || defined(_WIN64))
9 #include <Windows.h>
10 #include <direct.h>
11 #else
12 #include <sys/stat.h>
13 #endif
14 
15 int createDirectory(std::string directory)
16 {
17  int ret = 0;
18 #if (defined(_WIN32) || defined(_WIN64))
19  ret = _mkdir(directory.c_str());
20 #else
21  ret = mkdir(directory.c_str(), 0777);
22 #endif
23  if (ret == EEXIST) ret = 0;
24  return ret;
25 }
26 
27 cv::Mat fromPlanarFp16(const std::vector<float>& data, int w, int h, float mean, float scale){
28  cv::Mat frame = cv::Mat(h, w, CV_8UC3);
29 
30  for(int i = 0; i < w*h; i++) {
31  auto b = data.data()[i + w*h * 0] * scale + mean;
32  frame.data[i*3+0] = (uint8_t)b;
33  }
34  for(int i = 0; i < w*h; i++) {
35  auto g = data.data()[i + w*h * 1] * scale + mean;
36  frame.data[i*3+1] = (uint8_t)g;
37  }
38  for(int i = 0; i < w*h; i++) {
39  auto r = data.data()[i + w*h * 2] * scale + mean;
40  frame.data[i*3+2] = (uint8_t)r;
41  }
42  return frame;
43 }
44 
45 cv::Mat toMat(const std::vector<uint8_t>& data, int w, int h , int numPlanes, int bpp){
46 
47  cv::Mat frame;
48 
49  if(numPlanes == 3){
50  frame = cv::Mat(h, w, CV_8UC3);
51 
52  // optimization (cache)
53  for(int i = 0; i < w*h; i++) {
54  uint8_t b = data.data()[i + w*h * 0];
55  frame.data[i*3+0] = b;
56  }
57  for(int i = 0; i < w*h; i++) {
58  uint8_t g = data.data()[i + w*h * 1];
59  frame.data[i*3+1] = g;
60  }
61  for(int i = 0; i < w*h; i++) {
62  uint8_t r = data.data()[i + w*h * 2];
63  frame.data[i*3+2] = r;
64  }
65 
66  } else {
67  if(bpp == 3){
68  frame = cv::Mat(h, w, CV_8UC3);
69  for(int i = 0; i < w*h*bpp; i+=3) {
70  uint8_t b,g,r;
71  b = data.data()[i + 2];
72  g = data.data()[i + 1];
73  r = data.data()[i + 0];
74  frame.at<cv::Vec3b>( (i/bpp) / w, (i/bpp) % w) = cv::Vec3b(b,g,r);
75  }
76 
77  } else if(bpp == 6) {
78  //first denormalize
79  //dump
80 
81  frame = cv::Mat(h, w, CV_8UC3);
82  for(int y = 0; y < h; y++){
83  for(int x = 0; x < w; x++){
84 
85  const uint16_t* fp16 = (const uint16_t*) (data.data() + (y*w+x)*bpp);
86  uint8_t r = (uint8_t) (fp16_ieee_to_fp32_value(fp16[0]) * 255.0f);
87  uint8_t g = (uint8_t) (fp16_ieee_to_fp32_value(fp16[1]) * 255.0f);
88  uint8_t b = (uint8_t) (fp16_ieee_to_fp32_value(fp16[2]) * 255.0f);
89  frame.at<cv::Vec3b>(y, x) = cv::Vec3b(b,g,r);
90  }
91  }
92 
93  }
94  }
95 
96  return frame;
97 }
98 
99 
100 void toPlanar(cv::Mat& bgr, std::vector<std::uint8_t>& data){
101 
102  data.resize(bgr.cols * bgr.rows * 3);
103  for(int y = 0; y < bgr.rows; y++){
104  for(int x = 0; x < bgr.cols; x++){
105  auto p = bgr.at<cv::Vec3b>(y,x);
106  data[x + y*bgr.cols + 0 * bgr.rows*bgr.cols] = p[0];
107  data[x + y*bgr.cols + 1 * bgr.rows*bgr.cols] = p[1];
108  data[x + y*bgr.cols + 2 * bgr.rows*bgr.cols] = p[2];
109  }
110  }
111 }
112 
113 cv::Mat resizeKeepAspectRatio(const cv::Mat &input, const cv::Size &dstSize, const cv::Scalar &bgcolor)
114 {
115  cv::Mat output;
116 
117  double h1 = dstSize.width * (input.rows/(double)input.cols);
118  double w2 = dstSize.height * (input.cols/(double)input.rows);
119  if( h1 <= dstSize.height) {
120  cv::resize( input, output, cv::Size(dstSize.width, h1));
121  } else {
122  cv::resize( input, output, cv::Size(w2, dstSize.height));
123  }
124 
125  int top = (dstSize.height-output.rows) / 2;
126  int down = (dstSize.height-output.rows+1) / 2;
127  int left = (dstSize.width - output.cols) / 2;
128  int right = (dstSize.width - output.cols+1) / 2;
129 
130  cv::copyMakeBorder(output, output, top, down, left, right, cv::BORDER_CONSTANT, bgcolor );
131 
132  return output;
133 }
134 
DAI_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.hpp:177
toMat
cv::Mat toMat(const std::vector< uint8_t > &data, int w, int h, int numPlanes, int bpp)
Definition: utility.cpp:45
toPlanar
void toPlanar(cv::Mat &bgr, std::vector< std::uint8_t > &data)
Definition: utility.cpp:100
resizeKeepAspectRatio
cv::Mat resizeKeepAspectRatio(const cv::Mat &input, const cv::Size &dstSize, const cv::Scalar &bgcolor)
Definition: utility.cpp:113
fromPlanarFp16
cv::Mat fromPlanarFp16(const std::vector< float > &data, int w, int h, float mean, float scale)
Definition: utility.cpp:27
createDirectory
int createDirectory(std::string directory)
Definition: utility.cpp:15
utility.hpp


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19