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.

test_mat_pixel_resize.cpp 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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 "mat.h"
  15. #include "prng.h"
  16. #include <math.h>
  17. #include <string.h>
  18. static struct prng_rand_t g_prng_rand_state;
  19. #define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
  20. #define RAND() prng_rand(&g_prng_rand_state)
  21. static ncnn::Mat RandomMat(int w, int h, int elempack)
  22. {
  23. ncnn::Mat m(w, h, 1, (size_t)elempack, elempack);
  24. unsigned char* p = m;
  25. for (int i = 0; i < w * h * elempack; i++)
  26. {
  27. p[i] = RAND() % 256;
  28. }
  29. return m;
  30. }
  31. static bool NearlyEqual(float a, float b, float epsilon)
  32. {
  33. if (a == b)
  34. return true;
  35. float diff = fabs(a - b);
  36. if (diff <= epsilon)
  37. return true;
  38. // relative error
  39. return diff < epsilon * std::max(fabs(a), fabs(b));
  40. }
  41. static int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon = 0.001)
  42. {
  43. #define CHECK_MEMBER(m) \
  44. if (a.m != b.m) \
  45. { \
  46. fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \
  47. return -1; \
  48. }
  49. CHECK_MEMBER(dims)
  50. CHECK_MEMBER(w)
  51. CHECK_MEMBER(h)
  52. CHECK_MEMBER(c)
  53. CHECK_MEMBER(elemsize)
  54. CHECK_MEMBER(elempack)
  55. #undef CHECK_MEMBER
  56. for (int q = 0; q < a.c; q++)
  57. {
  58. const ncnn::Mat ma = a.channel(q);
  59. const ncnn::Mat mb = b.channel(q);
  60. for (int i = 0; i < a.h; i++)
  61. {
  62. const float* pa = ma.row(i);
  63. const float* pb = mb.row(i);
  64. for (int j = 0; j < a.w; j++)
  65. {
  66. if (!NearlyEqual(pa[j], pb[j], epsilon))
  67. {
  68. fprintf(stderr, "value not match at c:%d h:%d w:%d expect %f but got %f\n", q, i, j, pa[j], pb[j]);
  69. return -1;
  70. }
  71. }
  72. }
  73. }
  74. return 0;
  75. }
  76. static int test_mat_pixel_resize(int w, int h, int ch, int target_width, int target_height)
  77. {
  78. ncnn::Option opt;
  79. opt.num_threads = 1;
  80. ncnn::Mat a = RandomMat(w, h, ch);
  81. ncnn::Mat b(target_width, target_height, 1, (size_t)ch, ch);
  82. if (ch == 1) resize_bilinear_c1(a, w, h, b, target_width, target_height);
  83. if (ch == 2) resize_bilinear_c2(a, w, h, b, target_width, target_height);
  84. if (ch == 3) resize_bilinear_c3(a, w, h, b, target_width, target_height);
  85. if (ch == 4) resize_bilinear_c4(a, w, h, b, target_width, target_height);
  86. ncnn::Mat a2;
  87. ncnn::convert_packing(a, a2, 1, opt);
  88. ncnn::Mat b2;
  89. ncnn::convert_packing(b, b2, 1, opt);
  90. for (int i = 0; i < ch; i++)
  91. {
  92. ncnn::Mat c = ncnn::Mat::from_pixels(a2.channel(i), ncnn::Mat::PIXEL_GRAY, w, h);
  93. ncnn::Mat d = ncnn::Mat::from_pixels(b2.channel(i), ncnn::Mat::PIXEL_GRAY, target_width, target_height);
  94. ncnn::Mat e;
  95. ncnn::resize_bilinear(c, e, target_width, target_height, opt);
  96. if (Compare(e, d, 0.5) != 0)
  97. {
  98. fprintf(stderr, "test_mat_pixel_resize failed w=%d h=%d ch=%d target_width=%d target_height=%d\n", w, h, ch, target_width, target_height);
  99. return -1;
  100. }
  101. }
  102. return 0;
  103. }
  104. static int test_mat_pixel_roi_resize_gray(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
  105. {
  106. ncnn::Option opt;
  107. opt.num_threads = 1;
  108. int pixel_type_from[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_GRAY2RGB, ncnn::Mat::PIXEL_GRAY2BGR, ncnn::Mat::PIXEL_GRAY2RGBA, ncnn::Mat::PIXEL_GRAY2BGRA};
  109. int pixel_type_to[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_RGB2GRAY, ncnn::Mat::PIXEL_BGR2GRAY, ncnn::Mat::PIXEL_RGBA2GRAY, ncnn::Mat::PIXEL_BGRA2GRAY};
  110. ncnn::Mat a = RandomMat(w, h, 1);
  111. ncnn::Mat a2;
  112. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  113. // FIXME enable more convert types
  114. for (int i = 0; i < 1; i++)
  115. {
  116. ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
  117. ncnn::Mat b2;
  118. ncnn::Mat c2;
  119. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  120. ncnn::convert_packing(b2, c2, 1, opt);
  121. ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
  122. if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
  123. {
  124. fprintf(stderr, "test_mat_pixel_roi_resize_gray failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
  125. return -1;
  126. }
  127. }
  128. return 0;
  129. }
  130. static int test_mat_pixel_roi_resize_rgb(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
  131. {
  132. ncnn::Option opt;
  133. opt.num_threads = 1;
  134. int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
  135. int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
  136. ncnn::Mat a = RandomMat(w, h, 3);
  137. ncnn::Mat a2;
  138. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  139. // FIXME enable more convert types
  140. for (int i = 0; i < 2; i++)
  141. {
  142. ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
  143. ncnn::Mat b2;
  144. ncnn::Mat c2;
  145. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  146. ncnn::convert_packing(b2, c2, 3, opt);
  147. ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
  148. if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
  149. {
  150. fprintf(stderr, "test_mat_pixel_roi_resize_rgb failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
  151. return -1;
  152. }
  153. }
  154. return 0;
  155. }
  156. static int test_mat_pixel_roi_resize_bgr(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
  157. {
  158. ncnn::Option opt;
  159. opt.num_threads = 1;
  160. int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
  161. int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
  162. ncnn::Mat a = RandomMat(w, h, 3);
  163. ncnn::Mat a2;
  164. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  165. // FIXME enable more convert types
  166. for (int i = 0; i < 2; i++)
  167. {
  168. ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
  169. ncnn::Mat b2;
  170. ncnn::Mat c2;
  171. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  172. ncnn::convert_packing(b2, c2, 3, opt);
  173. ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
  174. if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
  175. {
  176. fprintf(stderr, "test_mat_pixel_roi_resize_bgr failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
  177. return -1;
  178. }
  179. }
  180. return 0;
  181. }
  182. static int test_mat_pixel_roi_resize_rgba(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
  183. {
  184. ncnn::Option opt;
  185. opt.num_threads = 1;
  186. int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
  187. int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
  188. ncnn::Mat a = RandomMat(w, h, 4);
  189. ncnn::Mat a2;
  190. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  191. for (int i = 0; i < 2; i++)
  192. {
  193. ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
  194. ncnn::Mat b2;
  195. ncnn::Mat c2;
  196. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  197. ncnn::convert_packing(b2, c2, 4, opt);
  198. ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
  199. if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
  200. {
  201. fprintf(stderr, "test_mat_pixel_roi_resize_rgba failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
  202. return -1;
  203. }
  204. }
  205. return 0;
  206. }
  207. static int test_mat_pixel_roi_resize_bgra(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
  208. {
  209. ncnn::Option opt;
  210. opt.num_threads = 1;
  211. int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
  212. int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
  213. ncnn::Mat a = RandomMat(w, h, 4);
  214. ncnn::Mat a2;
  215. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  216. for (int i = 0; i < 2; i++)
  217. {
  218. ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
  219. ncnn::Mat b2;
  220. ncnn::Mat c2;
  221. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  222. ncnn::convert_packing(b2, c2, 4, opt);
  223. ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
  224. if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
  225. {
  226. fprintf(stderr, "test_mat_pixel_roi_resize_bgra failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
  227. return -1;
  228. }
  229. }
  230. return 0;
  231. }
  232. static int test_mat_pixel_0()
  233. {
  234. for (int c = 1; c <= 4; c++)
  235. {
  236. int ret = 0
  237. || test_mat_pixel_resize(24, 48, c, 24, 48)
  238. || test_mat_pixel_resize(13, 17, c, 11, 14)
  239. || test_mat_pixel_resize(33, 23, c, 5, 6)
  240. || test_mat_pixel_resize(5, 4, c, 11, 16)
  241. || test_mat_pixel_resize(23, 11, c, 15, 21);
  242. if (ret != 0)
  243. return ret;
  244. }
  245. return 0;
  246. }
  247. static int test_mat_pixel_1()
  248. {
  249. return 0
  250. || test_mat_pixel_roi_resize_gray(16, 16, 1, 1, 13, 13, 10, 11)
  251. || test_mat_pixel_roi_resize_rgb(16, 16, 2, 1, 11, 11, 2, 3)
  252. || test_mat_pixel_roi_resize_bgr(16, 16, 1, 2, 11, 9, 22, 13)
  253. || test_mat_pixel_roi_resize_rgba(16, 16, 3, 2, 9, 11, 12, 4)
  254. || test_mat_pixel_roi_resize_bgra(16, 16, 2, 3, 9, 7, 7, 7);
  255. }
  256. static int test_mat_pixel_2()
  257. {
  258. return 0
  259. || test_mat_pixel_roi_resize_gray(15, 15, 2, 3, 2, 3, 2, 2)
  260. || test_mat_pixel_roi_resize_rgb(15, 15, 3, 4, 5, 4, 5, 4)
  261. || test_mat_pixel_roi_resize_bgr(15, 15, 4, 5, 6, 7, 4, 1)
  262. || test_mat_pixel_roi_resize_rgba(15, 15, 6, 6, 3, 4, 1, 3)
  263. || test_mat_pixel_roi_resize_bgra(15, 15, 7, 3, 1, 1, 1, 1);
  264. }
  265. int main()
  266. {
  267. SRAND(7767517);
  268. return test_mat_pixel_0() || test_mat_pixel_1() || test_mat_pixel_2();
  269. }