simpleocv.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef NCNN_SIMPLEOCV_H
  15. #define NCNN_SIMPLEOCV_H
  16. #include "platform.h"
  17. #if NCNN_SIMPLEOCV
  18. #include <limits.h>
  19. #include <string.h>
  20. #include "allocator.h"
  21. #include "mat.h"
  22. #if defined(_MSC_VER) || defined(__GNUC__)
  23. #pragma push_macro("min")
  24. #pragma push_macro("max")
  25. #undef min
  26. #undef max
  27. #endif
  28. #ifndef NCNN_XADD
  29. using ncnn::NCNN_XADD;
  30. #endif
  31. typedef unsigned char uchar;
  32. typedef unsigned short ushort;
  33. typedef unsigned int uint;
  34. enum
  35. {
  36. CV_LOAD_IMAGE_UNCHANGED = -1,
  37. CV_LOAD_IMAGE_GRAYSCALE = 0,
  38. CV_LOAD_IMAGE_COLOR = 1,
  39. };
  40. enum
  41. {
  42. CV_IMWRITE_JPEG_QUALITY = 1
  43. };
  44. // minimal opencv style data structure implementation
  45. namespace cv {
  46. template<typename _Tp>
  47. static inline _Tp saturate_cast(int v)
  48. {
  49. return _Tp(v);
  50. }
  51. template<>
  52. inline uchar saturate_cast<uchar>(int v)
  53. {
  54. return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
  55. }
  56. template<typename _Tp>
  57. struct Scalar_
  58. {
  59. Scalar_()
  60. {
  61. v[0] = 0;
  62. v[1] = 0;
  63. v[2] = 0;
  64. v[3] = 0;
  65. }
  66. Scalar_(_Tp _v0)
  67. {
  68. v[0] = _v0;
  69. v[1] = 0;
  70. v[2] = 0;
  71. v[3] = 0;
  72. }
  73. Scalar_(_Tp _v0, _Tp _v1, _Tp _v2)
  74. {
  75. v[0] = _v0;
  76. v[1] = _v1;
  77. v[2] = _v2;
  78. v[3] = 0;
  79. }
  80. Scalar_(_Tp _v0, _Tp _v1, _Tp _v2, _Tp _v3)
  81. {
  82. v[0] = _v0;
  83. v[1] = _v1;
  84. v[2] = _v2;
  85. v[3] = _v3;
  86. }
  87. const _Tp operator[](const int i) const
  88. {
  89. return v[i];
  90. }
  91. _Tp operator[](const int i)
  92. {
  93. return v[i];
  94. }
  95. _Tp v[4];
  96. };
  97. typedef Scalar_<uchar> Scalar;
  98. template<typename _Tp>
  99. struct Point_
  100. {
  101. Point_()
  102. : x(0), y(0)
  103. {
  104. }
  105. Point_(_Tp _x, _Tp _y)
  106. : x(_x), y(_y)
  107. {
  108. }
  109. template<typename _Tp2>
  110. operator Point_<_Tp2>() const
  111. {
  112. return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y));
  113. }
  114. _Tp x;
  115. _Tp y;
  116. };
  117. typedef Point_<int> Point;
  118. typedef Point_<float> Point2f;
  119. template<typename _Tp>
  120. struct Size_
  121. {
  122. Size_()
  123. : width(0), height(0)
  124. {
  125. }
  126. Size_(_Tp _w, _Tp _h)
  127. : width(_w), height(_h)
  128. {
  129. }
  130. template<typename _Tp2>
  131. operator Size_<_Tp2>() const
  132. {
  133. return Size_<_Tp2>(saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height));
  134. }
  135. _Tp width;
  136. _Tp height;
  137. };
  138. typedef Size_<int> Size;
  139. typedef Size_<float> Size2f;
  140. template<typename _Tp>
  141. struct Rect_
  142. {
  143. Rect_()
  144. : x(0), y(0), width(0), height(0)
  145. {
  146. }
  147. Rect_(_Tp _x, _Tp _y, _Tp _w, _Tp _h)
  148. : x(_x), y(_y), width(_w), height(_h)
  149. {
  150. }
  151. Rect_(Point_<_Tp> _p, Size_<_Tp> _size)
  152. : x(_p.x), y(_p.y), width(_size.width), height(_size.height)
  153. {
  154. }
  155. template<typename _Tp2>
  156. operator Rect_<_Tp2>() const
  157. {
  158. return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height));
  159. }
  160. _Tp x;
  161. _Tp y;
  162. _Tp width;
  163. _Tp height;
  164. // area
  165. _Tp area() const
  166. {
  167. return width * height;
  168. }
  169. };
  170. template<typename _Tp>
  171. static inline Rect_<_Tp>& operator&=(Rect_<_Tp>& a, const Rect_<_Tp>& b)
  172. {
  173. _Tp x1 = std::max(a.x, b.x), y1 = std::max(a.y, b.y);
  174. a.width = std::min(a.x + a.width, b.x + b.width) - x1;
  175. a.height = std::min(a.y + a.height, b.y + b.height) - y1;
  176. a.x = x1;
  177. a.y = y1;
  178. if (a.width <= 0 || a.height <= 0)
  179. a = Rect_<_Tp>();
  180. return a;
  181. }
  182. template<typename _Tp>
  183. static inline Rect_<_Tp>& operator|=(Rect_<_Tp>& a, const Rect_<_Tp>& b)
  184. {
  185. _Tp x1 = std::min(a.x, b.x), y1 = std::min(a.y, b.y);
  186. a.width = std::max(a.x + a.width, b.x + b.width) - x1;
  187. a.height = std::max(a.y + a.height, b.y + b.height) - y1;
  188. a.x = x1;
  189. a.y = y1;
  190. return a;
  191. }
  192. template<typename _Tp>
  193. static inline Rect_<_Tp> operator&(const Rect_<_Tp>& a, const Rect_<_Tp>& b)
  194. {
  195. Rect_<_Tp> c = a;
  196. return c &= b;
  197. }
  198. template<typename _Tp>
  199. static inline Rect_<_Tp> operator|(const Rect_<_Tp>& a, const Rect_<_Tp>& b)
  200. {
  201. Rect_<_Tp> c = a;
  202. return c |= b;
  203. }
  204. typedef Rect_<int> Rect;
  205. typedef Rect_<float> Rect2f;
  206. #define CV_8UC1 1
  207. #define CV_8UC3 3
  208. #define CV_8UC4 4
  209. #define CV_32FC1 4
  210. struct NCNN_EXPORT Mat
  211. {
  212. Mat()
  213. : data(0), refcount(0), rows(0), cols(0), c(0)
  214. {
  215. }
  216. Mat(int _rows, int _cols, int flags)
  217. : data(0), refcount(0)
  218. {
  219. create(_rows, _cols, flags);
  220. }
  221. // copy
  222. Mat(const Mat& m)
  223. : data(m.data), refcount(m.refcount)
  224. {
  225. if (refcount)
  226. NCNN_XADD(refcount, 1);
  227. rows = m.rows;
  228. cols = m.cols;
  229. c = m.c;
  230. }
  231. Mat(int _rows, int _cols, int flags, void* _data)
  232. : data((unsigned char*)_data), refcount(0)
  233. {
  234. rows = _rows;
  235. cols = _cols;
  236. c = flags;
  237. }
  238. ~Mat()
  239. {
  240. release();
  241. }
  242. // assign
  243. Mat& operator=(const Mat& m)
  244. {
  245. if (this == &m)
  246. return *this;
  247. if (m.refcount)
  248. NCNN_XADD(m.refcount, 1);
  249. release();
  250. data = m.data;
  251. refcount = m.refcount;
  252. rows = m.rows;
  253. cols = m.cols;
  254. c = m.c;
  255. return *this;
  256. }
  257. Mat& operator=(const Scalar& s)
  258. {
  259. if (total() > 0)
  260. {
  261. uchar* p = data;
  262. for (int i = 0; i < cols * rows; i++)
  263. {
  264. for (int j = 0; j < c; j++)
  265. {
  266. *p++ = s[j];
  267. }
  268. }
  269. }
  270. return *this;
  271. }
  272. void create(int _rows, int _cols, int flags)
  273. {
  274. release();
  275. rows = _rows;
  276. cols = _cols;
  277. c = flags;
  278. if (total() > 0)
  279. {
  280. // refcount address must be aligned, so we expand totalsize here
  281. size_t totalsize = (total() + 3) >> 2 << 2;
  282. data = (uchar*)ncnn::fastMalloc(totalsize + (int)sizeof(*refcount));
  283. refcount = (int*)(((uchar*)data) + totalsize);
  284. *refcount = 1;
  285. }
  286. }
  287. void release()
  288. {
  289. if (refcount && NCNN_XADD(refcount, -1) == 1)
  290. ncnn::fastFree(data);
  291. data = 0;
  292. rows = 0;
  293. cols = 0;
  294. c = 0;
  295. refcount = 0;
  296. }
  297. Mat clone() const
  298. {
  299. if (empty())
  300. return Mat();
  301. Mat m(rows, cols, c);
  302. if (total() > 0)
  303. {
  304. memcpy(m.data, data, total());
  305. }
  306. return m;
  307. }
  308. bool empty() const
  309. {
  310. return data == 0 || total() == 0;
  311. }
  312. int channels() const
  313. {
  314. return c;
  315. }
  316. int type() const
  317. {
  318. return c;
  319. }
  320. size_t total() const
  321. {
  322. return cols * rows * c;
  323. }
  324. const uchar* ptr(int y) const
  325. {
  326. return data + y * cols * c;
  327. }
  328. uchar* ptr(int y)
  329. {
  330. return data + y * cols * c;
  331. }
  332. template<typename _Tp>
  333. const _Tp* ptr(int y) const
  334. {
  335. return (const _Tp*)data + y * cols * c;
  336. }
  337. template<typename _Tp>
  338. _Tp* ptr(int y)
  339. {
  340. return (_Tp*)data + y * cols * c;
  341. }
  342. // roi
  343. Mat operator()(const Rect& roi) const
  344. {
  345. if (empty())
  346. return Mat();
  347. Mat m(roi.height, roi.width, c);
  348. int sy = roi.y;
  349. for (int y = 0; y < roi.height; y++)
  350. {
  351. const uchar* sptr = ptr(sy) + roi.x * c;
  352. uchar* dptr = m.ptr(y);
  353. memcpy(dptr, sptr, roi.width * c);
  354. sy++;
  355. }
  356. return m;
  357. }
  358. uchar* data;
  359. // pointer to the reference counter;
  360. // when points to user-allocated data, the pointer is NULL
  361. int* refcount;
  362. int rows;
  363. int cols;
  364. int c;
  365. };
  366. enum ImreadModes
  367. {
  368. IMREAD_UNCHANGED = -1,
  369. IMREAD_GRAYSCALE = 0,
  370. IMREAD_COLOR = 1
  371. };
  372. NCNN_EXPORT Mat imread(const std::string& path, int flags = IMREAD_COLOR);
  373. enum ImwriteFlags
  374. {
  375. IMWRITE_JPEG_QUALITY = 1
  376. };
  377. NCNN_EXPORT bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params = std::vector<int>());
  378. NCNN_EXPORT void imshow(const std::string& name, const Mat& m);
  379. NCNN_EXPORT int waitKey(int delay = 0);
  380. #if NCNN_PIXEL
  381. NCNN_EXPORT void resize(const Mat& src, Mat& dst, const Size& size, float sw = 0.f, float sh = 0.f, int flags = 0);
  382. #endif // NCNN_PIXEL
  383. #if NCNN_PIXEL_DRAWING
  384. enum
  385. {
  386. FILLED = -1
  387. };
  388. NCNN_EXPORT void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness = 1);
  389. NCNN_EXPORT void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness = 1);
  390. NCNN_EXPORT void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness = 1);
  391. NCNN_EXPORT void line(Mat& img, Point p0, Point p1, const Scalar& color, int thickness = 1);
  392. enum
  393. {
  394. FONT_HERSHEY_SIMPLEX = 0
  395. };
  396. NCNN_EXPORT void putText(Mat& img, const std::string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1);
  397. NCNN_EXPORT Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine);
  398. #endif // NCNN_PIXEL_DRAWING
  399. } // namespace cv
  400. #if defined(_MSC_VER) || defined(__GNUC__)
  401. #pragma pop_macro("min")
  402. #pragma pop_macro("max")
  403. #endif
  404. #endif // NCNN_SIMPLEOCV
  405. #endif // NCNN_SIMPLEOCV_H