You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

simpleocv.cpp 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Copyright 2017 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "simpleocv.h"
  4. #if NCNN_SIMPLEOCV
  5. #include <stdio.h>
  6. #define STB_IMAGE_IMPLEMENTATION
  7. #if __ARM_NEON
  8. #define STBI_NEON
  9. #endif
  10. #define STBI_NO_THREAD_LOCALS
  11. #define STBI_ONLY_JPEG
  12. #define STBI_ONLY_PNG
  13. #define STBI_ONLY_BMP
  14. #define STBI_ONLY_PNM
  15. #include "stb_image.h"
  16. #define STB_IMAGE_WRITE_IMPLEMENTATION
  17. #include "stb_image_write.h"
  18. namespace cv {
  19. Mat imread(const std::string& path, int flags)
  20. {
  21. int desired_channels = 0;
  22. if (flags == IMREAD_UNCHANGED)
  23. {
  24. desired_channels = 0;
  25. }
  26. else if (flags == IMREAD_GRAYSCALE)
  27. {
  28. desired_channels = 1;
  29. }
  30. else if (flags == IMREAD_COLOR)
  31. {
  32. desired_channels = 3;
  33. }
  34. else
  35. {
  36. // unknown flags
  37. return Mat();
  38. }
  39. int w;
  40. int h;
  41. int c;
  42. unsigned char* pixeldata = stbi_load(path.c_str(), &w, &h, &c, desired_channels);
  43. if (!pixeldata)
  44. {
  45. // load failed
  46. return Mat();
  47. }
  48. if (desired_channels)
  49. {
  50. c = desired_channels;
  51. }
  52. // copy pixeldata to Mat
  53. Mat img;
  54. if (c == 1)
  55. {
  56. img.create(h, w, CV_8UC1);
  57. }
  58. else if (c == 3)
  59. {
  60. img.create(h, w, CV_8UC3);
  61. }
  62. else if (c == 4)
  63. {
  64. img.create(h, w, CV_8UC4);
  65. }
  66. else
  67. {
  68. // unexpected channels
  69. stbi_image_free(pixeldata);
  70. return Mat();
  71. }
  72. memcpy(img.data, pixeldata, w * h * c);
  73. stbi_image_free(pixeldata);
  74. // // resolve exif orientation
  75. // {
  76. // std::ifstream ifs;
  77. // ifs.open(filename.c_str(), std::ifstream::in);
  78. //
  79. // if (ifs.good())
  80. // {
  81. // ExifReader exif_reader(ifs);
  82. // if (exif_reader.parse())
  83. // {
  84. // ExifEntry_t e = exif_reader.getTag(ORIENTATION);
  85. // int orientation = e.field_u16;
  86. // if (orientation >= 1 && orientation <= 8)
  87. // rotate_by_orientation(img, img, orientation);
  88. // }
  89. // }
  90. //
  91. // ifs.close();
  92. // }
  93. // rgb to bgr
  94. if (c == 3)
  95. {
  96. uchar* p = img.data;
  97. for (int i = 0; i < w * h; i++)
  98. {
  99. std::swap(p[0], p[2]);
  100. p += 3;
  101. }
  102. }
  103. if (c == 4)
  104. {
  105. uchar* p = img.data;
  106. for (int i = 0; i < w * h; i++)
  107. {
  108. std::swap(p[0], p[2]);
  109. p += 4;
  110. }
  111. }
  112. return img;
  113. }
  114. Mat imdecode(const std::vector<uchar>& buf, int flags)
  115. {
  116. int desired_channels = 0;
  117. if (flags == IMREAD_UNCHANGED)
  118. {
  119. desired_channels = 0;
  120. }
  121. else if (flags == IMREAD_GRAYSCALE)
  122. {
  123. desired_channels = 1;
  124. }
  125. else if (flags == IMREAD_COLOR)
  126. {
  127. desired_channels = 3;
  128. }
  129. else
  130. {
  131. // unknown flags
  132. return Mat();
  133. }
  134. int w;
  135. int h;
  136. int c;
  137. unsigned char* pixeldata = stbi_load_from_memory(&buf.front(), buf.size(), &w, &h, &c, desired_channels);
  138. if (!pixeldata)
  139. {
  140. // load failed
  141. return Mat();
  142. }
  143. if (desired_channels)
  144. {
  145. c = desired_channels;
  146. }
  147. // copy pixeldata to Mat
  148. Mat img;
  149. if (c == 1)
  150. {
  151. img.create(h, w, CV_8UC1);
  152. }
  153. else if (c == 3)
  154. {
  155. img.create(h, w, CV_8UC3);
  156. }
  157. else if (c == 4)
  158. {
  159. img.create(h, w, CV_8UC4);
  160. }
  161. else
  162. {
  163. // unexpected channels
  164. stbi_image_free(pixeldata);
  165. return Mat();
  166. }
  167. memcpy(img.data, pixeldata, w * h * c);
  168. stbi_image_free(pixeldata);
  169. // rgb to bgr
  170. if (c == 3)
  171. {
  172. uchar* p = img.data;
  173. for (int i = 0; i < w * h; i++)
  174. {
  175. std::swap(p[0], p[2]);
  176. p += 3;
  177. }
  178. }
  179. if (c == 4)
  180. {
  181. uchar* p = img.data;
  182. for (int i = 0; i < w * h; i++)
  183. {
  184. std::swap(p[0], p[2]);
  185. p += 4;
  186. }
  187. }
  188. return img;
  189. }
  190. bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params)
  191. {
  192. const char* _ext = strrchr(path.c_str(), '.');
  193. if (!_ext)
  194. {
  195. // missing extension
  196. return false;
  197. }
  198. std::string ext = _ext;
  199. Mat img = m.clone();
  200. // bgr to rgb
  201. int c = 0;
  202. if (img.type() == CV_8UC1)
  203. {
  204. c = 1;
  205. }
  206. else if (img.type() == CV_8UC3)
  207. {
  208. c = 3;
  209. uchar* p = img.data;
  210. for (int i = 0; i < img.cols * img.rows; i++)
  211. {
  212. std::swap(p[0], p[2]);
  213. p += 3;
  214. }
  215. }
  216. else if (img.type() == CV_8UC4)
  217. {
  218. c = 4;
  219. uchar* p = img.data;
  220. for (int i = 0; i < img.cols * img.rows; i++)
  221. {
  222. std::swap(p[0], p[2]);
  223. p += 4;
  224. }
  225. }
  226. else
  227. {
  228. // unexpected image channels
  229. return false;
  230. }
  231. bool success = false;
  232. if (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG")
  233. {
  234. int quality = 95;
  235. for (size_t i = 0; i < params.size(); i += 2)
  236. {
  237. if (params[i] == IMWRITE_JPEG_QUALITY)
  238. {
  239. quality = params[i + 1];
  240. break;
  241. }
  242. }
  243. success = stbi_write_jpg(path.c_str(), img.cols, img.rows, c, img.data, quality);
  244. }
  245. else if (ext == ".png" || ext == ".PNG")
  246. {
  247. success = stbi_write_png(path.c_str(), img.cols, img.rows, c, img.data, 0);
  248. }
  249. else if (ext == ".bmp" || ext == ".BMP")
  250. {
  251. success = stbi_write_bmp(path.c_str(), img.cols, img.rows, c, img.data);
  252. }
  253. else
  254. {
  255. // unknown extension type
  256. return false;
  257. }
  258. return success;
  259. }
  260. void imshow(const std::string& name, const Mat& m)
  261. {
  262. NCNN_LOGE("imshow save image to %s.png", name.c_str());
  263. imwrite(name + ".png", m);
  264. }
  265. int waitKey(int delay)
  266. {
  267. NCNN_LOGE("waitKey stub");
  268. return -1;
  269. }
  270. #if NCNN_PIXEL
  271. void resize(const Mat& src, Mat& dst, const Size& size, float sw, float sh, int flags)
  272. {
  273. (void)flags;
  274. int srcw = src.cols;
  275. int srch = src.rows;
  276. int w = size.width;
  277. int h = size.height;
  278. if (w == 0 || h == 0)
  279. {
  280. w = srcw * sw;
  281. h = srch * sh;
  282. }
  283. if (w == 0 || h == 0)
  284. return;
  285. if (w == srcw && h == srch)
  286. {
  287. dst = src.clone();
  288. return;
  289. }
  290. cv::Mat tmp(h, w, src.c);
  291. if (tmp.empty())
  292. return;
  293. if (src.c == 1)
  294. ncnn::resize_bilinear_c1(src.data, srcw, srch, tmp.data, w, h);
  295. else if (src.c == 3)
  296. ncnn::resize_bilinear_c3(src.data, srcw, srch, tmp.data, w, h);
  297. else if (src.c == 4)
  298. ncnn::resize_bilinear_c4(src.data, srcw, srch, tmp.data, w, h);
  299. dst = tmp;
  300. }
  301. #endif // NCNN_PIXEL
  302. #if NCNN_PIXEL_DRAWING
  303. void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness)
  304. {
  305. Rect rec;
  306. rec.x = std::min(pt1.x, pt2.x);
  307. rec.y = std::min(pt1.y, pt2.y);
  308. rec.width = std::max(pt1.x, pt2.x) - rec.x;
  309. rec.height = std::max(pt1.y, pt2.y) - rec.y;
  310. rectangle(img, rec, color, thickness);
  311. }
  312. void rectangle(Mat& img, Rect rec, const Scalar& _color, int thickness)
  313. {
  314. unsigned int color = 0;
  315. unsigned char* border_color = (unsigned char*)&color;
  316. if (img.c == 1)
  317. {
  318. border_color[0] = _color[0];
  319. ncnn::draw_rectangle_c1(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
  320. }
  321. else if (img.c == 3)
  322. {
  323. border_color[0] = _color[0];
  324. border_color[1] = _color[1];
  325. border_color[2] = _color[2];
  326. ncnn::draw_rectangle_c3(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
  327. }
  328. else if (img.c == 4)
  329. {
  330. border_color[0] = _color[0];
  331. border_color[1] = _color[1];
  332. border_color[2] = _color[2];
  333. border_color[3] = _color[3];
  334. ncnn::draw_rectangle_c4(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
  335. }
  336. }
  337. void circle(Mat& img, Point center, int radius, const Scalar& _color, int thickness)
  338. {
  339. unsigned int color = 0;
  340. unsigned char* border_color = (unsigned char*)&color;
  341. if (img.c == 1)
  342. {
  343. border_color[0] = _color[0];
  344. ncnn::draw_circle_c1(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
  345. }
  346. else if (img.c == 3)
  347. {
  348. border_color[0] = _color[0];
  349. border_color[1] = _color[1];
  350. border_color[2] = _color[2];
  351. ncnn::draw_circle_c3(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
  352. }
  353. else if (img.c == 4)
  354. {
  355. border_color[0] = _color[0];
  356. border_color[1] = _color[1];
  357. border_color[2] = _color[2];
  358. border_color[3] = _color[3];
  359. ncnn::draw_circle_c4(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
  360. }
  361. }
  362. void line(Mat& img, Point p0, Point p1, const Scalar& _color, int thickness)
  363. {
  364. unsigned int color = 0;
  365. unsigned char* border_color = (unsigned char*)&color;
  366. if (img.c == 1)
  367. {
  368. border_color[0] = _color[0];
  369. ncnn::draw_line_c1(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
  370. }
  371. else if (img.c == 3)
  372. {
  373. border_color[0] = _color[0];
  374. border_color[1] = _color[1];
  375. border_color[2] = _color[2];
  376. ncnn::draw_line_c3(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
  377. }
  378. else if (img.c == 4)
  379. {
  380. border_color[0] = _color[0];
  381. border_color[1] = _color[1];
  382. border_color[2] = _color[2];
  383. border_color[3] = _color[3];
  384. ncnn::draw_line_c4(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
  385. }
  386. }
  387. void putText(Mat& img, const std::string& text, Point org, int fontFace, double fontScale, Scalar _color, int thickness)
  388. {
  389. const int fontpixelsize = 20 * fontScale;
  390. unsigned int color = 0;
  391. unsigned char* border_color = (unsigned char*)&color;
  392. if (img.c == 1)
  393. {
  394. border_color[0] = _color[0];
  395. ncnn::draw_text_c1(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
  396. }
  397. else if (img.c == 3)
  398. {
  399. border_color[0] = _color[0];
  400. border_color[1] = _color[1];
  401. border_color[2] = _color[2];
  402. ncnn::draw_text_c3(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
  403. }
  404. else if (img.c == 4)
  405. {
  406. border_color[0] = _color[0];
  407. border_color[1] = _color[1];
  408. border_color[2] = _color[2];
  409. border_color[3] = _color[3];
  410. ncnn::draw_text_c4(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
  411. }
  412. }
  413. Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine)
  414. {
  415. const int fontpixelsize = 20 * fontScale;
  416. int w;
  417. int h;
  418. ncnn::get_text_drawing_size(text.c_str(), fontpixelsize, &w, &h);
  419. *baseLine = 0;
  420. return Size(w, h);
  421. }
  422. #endif // NCNN_PIXEL_DRAWING
  423. } // namespace cv
  424. #endif // NCNN_SIMPLEOCV