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_affine.cpp 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 generate_ncnn_logo(int w, int h)
  22. {
  23. // clang-format off
  24. // *INDENT-OFF*
  25. static const unsigned char ncnn_logo_data[16][16] =
  26. {
  27. {245, 245, 33, 245, 245, 245, 245, 245, 245, 245, 245, 245, 245, 33, 245, 245},
  28. {245, 33, 33, 33, 245, 245, 245, 245, 245, 245, 245, 245, 33, 33, 33, 245},
  29. {245, 33, 158, 158, 33, 245, 245, 245, 245, 245, 245, 33, 158, 158, 33, 245},
  30. { 33, 117, 158, 224, 158, 33, 245, 245, 245, 245, 33, 158, 224, 158, 117, 33},
  31. { 33, 117, 224, 224, 224, 66, 33, 33, 33, 33, 66, 224, 224, 224, 117, 33},
  32. { 33, 189, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 189, 33},
  33. { 33, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 33},
  34. { 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 97, 97, 97, 224, 224, 33},
  35. { 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
  36. { 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
  37. { 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 189, 189, 97, 224, 224, 33},
  38. { 33, 66, 66, 66, 224, 224, 224, 224, 224, 224, 224, 224, 66, 66, 66, 33},
  39. { 66, 158, 158, 66, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 66, 66},
  40. { 66, 158, 158, 208, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 208, 66},
  41. { 66, 224, 202, 158, 66, 224, 224, 224, 224, 224, 224, 66, 224, 202, 158, 66},
  42. { 66, 158, 224, 158, 66, 224, 224, 224, 224, 224, 224, 66, 158, 224, 158, 66}
  43. };
  44. // *INDENT-ON*
  45. // clang-format on
  46. ncnn::Mat m(w, h, (size_t)1, 1);
  47. resize_bilinear_c1((const unsigned char*)ncnn_logo_data, 16, 16, m, w, h);
  48. return m;
  49. }
  50. static ncnn::Mat RandomMat(int w, int h, int elempack)
  51. {
  52. ncnn::Mat image = generate_ncnn_logo(w, h);
  53. ncnn::Mat m(w, h, 1, (size_t)elempack, elempack);
  54. for (int i = 0; i < h; i++)
  55. {
  56. unsigned char* p = m.row<unsigned char>(i);
  57. const unsigned char* pb = image.row<const unsigned char>(i);
  58. for (int j = 0; j < w; j++)
  59. {
  60. for (int k = 0; k < elempack; k++)
  61. {
  62. p[k] = pb[0];
  63. }
  64. p += elempack;
  65. pb += 1;
  66. }
  67. }
  68. return m;
  69. }
  70. static bool NearlyEqual(unsigned char a, unsigned char b)
  71. {
  72. return abs(a - b) <= 10;
  73. }
  74. static int CompareNearlyEqual(const ncnn::Mat& a, const ncnn::Mat& b)
  75. {
  76. for (int i = 0; i < a.h; i++)
  77. {
  78. const unsigned char* pa = a.row<const unsigned char>(i);
  79. const unsigned char* pb = b.row<const unsigned char>(i);
  80. for (int j = 0; j < a.w; j++)
  81. {
  82. for (int k = 0; k < a.elempack; k++)
  83. {
  84. if (!NearlyEqual(pa[k], pb[k]))
  85. {
  86. fprintf(stderr, "value not match at h:%d w:%d [%d] expect %d but got %d\n", i, j, k, pa[k], pb[k]);
  87. return -1;
  88. }
  89. }
  90. pa += a.elempack;
  91. pb += a.elempack;
  92. }
  93. }
  94. return 0;
  95. }
  96. static int test_mat_pixel_affine_c1(int w, int h)
  97. {
  98. ncnn::Mat a0 = RandomMat(w, h, 1);
  99. float tm[6];
  100. float tm_inv[6];
  101. ncnn::get_rotation_matrix(10.f, 0.15f, w / 2, h / 2, tm);
  102. ncnn::invert_affine_transform(tm, tm_inv);
  103. ncnn::Mat a1(w / 2, h / 2, 1u, 1);
  104. ncnn::Mat a2 = a0.clone();
  105. ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w / 2, h / 2, tm, 0);
  106. ncnn::warpaffine_bilinear_c1(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
  107. if (CompareNearlyEqual(a0, a2) != 0)
  108. {
  109. fprintf(stderr, "test_mat_pixel_affine_c1 failed w=%d h=%d\n", w, h);
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. static int test_mat_pixel_affine_c2(int w, int h)
  115. {
  116. ncnn::Mat a0 = RandomMat(w, h, 2);
  117. float tm[6];
  118. float tm_inv[6];
  119. ncnn::get_rotation_matrix(20.f, 0.25f, w / 4, h / 4, tm);
  120. ncnn::invert_affine_transform(tm, tm_inv);
  121. ncnn::Mat a1(w / 4, h / 4, 2u, 2);
  122. ncnn::Mat a2 = a0.clone();
  123. ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w / 4, h / 4, tm, 0);
  124. ncnn::warpaffine_bilinear_c2(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
  125. if (CompareNearlyEqual(a0, a2) != 0)
  126. {
  127. fprintf(stderr, "test_mat_pixel_affine_c2 failed w=%d h=%d\n", w, h);
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. static int test_mat_pixel_affine_c3(int w, int h)
  133. {
  134. ncnn::Mat a0 = RandomMat(w, h, 3);
  135. float tm[6];
  136. float tm_inv[6];
  137. ncnn::get_rotation_matrix(-30.f, 0.6f, w / 2, h / 2, tm);
  138. ncnn::invert_affine_transform(tm, tm_inv);
  139. ncnn::Mat a1(w / 2, h / 2, 3u, 3);
  140. ncnn::Mat a2 = a0.clone();
  141. ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w / 2, h / 2, tm, 0);
  142. ncnn::warpaffine_bilinear_c3(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
  143. if (CompareNearlyEqual(a0, a2) != 0)
  144. {
  145. fprintf(stderr, "test_mat_pixel_affine_c3 failed w=%d h=%d\n", w, h);
  146. return -1;
  147. }
  148. return 0;
  149. }
  150. static int test_mat_pixel_affine_c4(int w, int h)
  151. {
  152. ncnn::Mat a0 = RandomMat(w, h, 4);
  153. const float points_from[4] = {w / 8.f, h / 8.f, w / 8.f + 1.f, h / 8.f + 3.f};
  154. const float points_to[4] = {w / 2.f, h / 2.f, w / 2.f + 2.f, h / 2.f};
  155. float tm[6];
  156. float tm_inv[6];
  157. ncnn::get_affine_transform(points_from, points_to, 2, tm);
  158. ncnn::invert_affine_transform(tm, tm_inv);
  159. ncnn::Mat a1(w / 4, h / 4, 4u, 4);
  160. ncnn::Mat a2 = a0.clone();
  161. ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w / 4, h / 4, tm, 0);
  162. ncnn::warpaffine_bilinear_c4(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
  163. if (CompareNearlyEqual(a0, a2) != 0)
  164. {
  165. fprintf(stderr, "test_mat_pixel_affine_c4 failed w=%d h=%d\n", w, h);
  166. return -1;
  167. }
  168. return 0;
  169. }
  170. static int test_mat_pixel_affine_0()
  171. {
  172. return 0
  173. || test_mat_pixel_affine_c1(60, 70)
  174. || test_mat_pixel_affine_c2(60, 70)
  175. || test_mat_pixel_affine_c3(60, 70)
  176. || test_mat_pixel_affine_c4(60, 70)
  177. || test_mat_pixel_affine_c1(120, 160)
  178. || test_mat_pixel_affine_c2(120, 160)
  179. || test_mat_pixel_affine_c3(120, 160)
  180. || test_mat_pixel_affine_c4(120, 160)
  181. || test_mat_pixel_affine_c1(220, 330)
  182. || test_mat_pixel_affine_c2(220, 330)
  183. || test_mat_pixel_affine_c3(220, 330)
  184. || test_mat_pixel_affine_c4(220, 330);
  185. }
  186. static int test_mat_pixel_affine_yuv420sp(int w, int h)
  187. {
  188. ncnn::Mat a0(w, h * 3 / 2, 1u, 1);
  189. ncnn::Mat a0_y = RandomMat(w, h, 1);
  190. ncnn::Mat a0_uv = RandomMat(w / 2, h / 2, 2);
  191. memcpy(a0, a0_y, w * h);
  192. memcpy((unsigned char*)a0 + w * h, a0_uv, w * h / 2);
  193. float tm[6];
  194. float tm_inv[6];
  195. ncnn::get_rotation_matrix(-70.f, 0.3f, w / 2, h / 2, tm);
  196. ncnn::invert_affine_transform(tm, tm_inv);
  197. ncnn::Mat a1(w / 2, (h / 2) * 3 / 2, 1u, 1);
  198. ncnn::Mat a2 = a0.clone();
  199. ncnn::warpaffine_bilinear_yuv420sp(a0, w, h, a1, w / 2, h / 2, tm, 0);
  200. ncnn::warpaffine_bilinear_yuv420sp(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
  201. // Y
  202. if (CompareNearlyEqual(ncnn::Mat(w, h, (unsigned char*)a0, 1u, 1), ncnn::Mat(w, h, (unsigned char*)a2, 1u, 1)) != 0)
  203. {
  204. fprintf(stderr, "test_mat_pixel_affine_yuv420sp Y failed w=%d h=%d\n", w, h);
  205. return -1;
  206. }
  207. // UV
  208. if (CompareNearlyEqual(ncnn::Mat(w / 2, h / 2, (unsigned char*)a0 + w * h, 2u, 2), ncnn::Mat(w / 2, h / 2, (unsigned char*)a2 + w * h, 2u, 2)) != 0)
  209. {
  210. fprintf(stderr, "test_mat_pixel_affine_yuv420sp UV failed w=%d h=%d\n", w, h);
  211. return -1;
  212. }
  213. return 0;
  214. }
  215. static int test_mat_pixel_affine_1()
  216. {
  217. return 0
  218. || test_mat_pixel_affine_yuv420sp(60, 40)
  219. || test_mat_pixel_affine_yuv420sp(120, 160)
  220. || test_mat_pixel_affine_yuv420sp(220, 340);
  221. }
  222. int main()
  223. {
  224. SRAND(7767517);
  225. return test_mat_pixel_affine_0() || test_mat_pixel_affine_1();
  226. }