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 10 kB

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