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.

image_process_test.cc 28 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/common.h"
  17. #include "lite_cv/lite_mat.h"
  18. #include "lite_cv/image_process.h"
  19. #include <opencv2/opencv.hpp>
  20. #include <opencv2/imgproc/types_c.h>
  21. #include <fstream>
  22. using namespace mindspore::dataset;
  23. class MindDataImageProcess : public UT::Common {
  24. public:
  25. MindDataImageProcess() {}
  26. void SetUp() {}
  27. };
  28. void CompareMat(cv::Mat cv_mat, LiteMat lite_mat) {
  29. int cv_h = cv_mat.rows;
  30. int cv_w = cv_mat.cols;
  31. int cv_c = cv_mat.channels();
  32. int lite_h = lite_mat.height_;
  33. int lite_w = lite_mat.width_;
  34. int lite_c = lite_mat.channel_;
  35. ASSERT_TRUE(cv_h == lite_h);
  36. ASSERT_TRUE(cv_w == lite_w);
  37. ASSERT_TRUE(cv_c == lite_c);
  38. }
  39. void Lite3CImageProcess(LiteMat &lite_mat_bgr, LiteMat &lite_norm_mat_cut) {
  40. bool ret;
  41. LiteMat lite_mat_resize;
  42. ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  43. ASSERT_TRUE(ret == true);
  44. LiteMat lite_mat_convert_float;
  45. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0);
  46. ASSERT_TRUE(ret == true);
  47. LiteMat lite_mat_crop;
  48. ret = Crop(lite_mat_convert_float, lite_mat_crop, 16, 16, 224, 224);
  49. ASSERT_TRUE(ret == true);
  50. std::vector<float> means = {0.485, 0.456, 0.406};
  51. std::vector<float> stds = {0.229, 0.224, 0.225};
  52. SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, stds);
  53. return;
  54. }
  55. cv::Mat cv3CImageProcess(cv::Mat &image) {
  56. cv::Mat resize_256_image;
  57. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  58. cv::Mat float_256_image;
  59. resize_256_image.convertTo(float_256_image, CV_32FC3);
  60. cv::Mat roi_224_image;
  61. cv::Rect roi;
  62. roi.x = 16;
  63. roi.y = 16;
  64. roi.width = 224;
  65. roi.height = 224;
  66. float_256_image(roi).copyTo(roi_224_image);
  67. float meanR = 0.485;
  68. float meanG = 0.456;
  69. float meanB = 0.406;
  70. float varR = 0.229;
  71. float varG = 0.224;
  72. float varB = 0.225;
  73. cv::Scalar mean = cv::Scalar(meanR, meanG, meanB);
  74. cv::Scalar var = cv::Scalar(varR, varG, varB);
  75. cv::Mat imgMean(roi_224_image.size(), CV_32FC3, mean);
  76. cv::Mat imgVar(roi_224_image.size(), CV_32FC3, var);
  77. cv::Mat imgR1 = roi_224_image - imgMean;
  78. cv::Mat imgR2 = imgR1 / imgVar;
  79. return imgR2;
  80. }
  81. TEST_F(MindDataImageProcess, testRGB) {
  82. std::string filename = "data/dataset/apple.jpg";
  83. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  84. cv::Mat rgba_mat;
  85. cv::cvtColor(image, rgba_mat, CV_BGR2RGB);
  86. bool ret = false;
  87. LiteMat lite_mat_rgb;
  88. ret = InitFromPixel(rgba_mat.data, LPixelType::RGB, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_rgb);
  89. ASSERT_TRUE(ret == true);
  90. cv::Mat dst_image(lite_mat_rgb.height_, lite_mat_rgb.width_, CV_8UC3, lite_mat_rgb.data_ptr_);
  91. }
  92. TEST_F(MindDataImageProcess, testLoadByMemPtr) {
  93. std::string filename = "data/dataset/apple.jpg";
  94. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  95. cv::Mat rgba_mat;
  96. cv::cvtColor(image, rgba_mat, CV_BGR2RGB);
  97. bool ret = false;
  98. int width = rgba_mat.cols;
  99. int height = rgba_mat.rows;
  100. uchar *p_rgb = (uchar *)malloc(width * height * 3 * sizeof(uchar));
  101. for (int i = 0; i < height; i++) {
  102. const uchar *current = rgba_mat.ptr<uchar>(i);
  103. for (int j = 0; j < width; j++) {
  104. p_rgb[i * width * 3 + 3 * j + 0] = current[3 * j + 0];
  105. p_rgb[i * width * 3 + 3 * j + 1] = current[3 * j + 1];
  106. p_rgb[i * width * 3 + 3 * j + 2] = current[3 * j + 2];
  107. }
  108. }
  109. LiteMat lite_mat_rgb(width, height, 3, (void *)p_rgb, LDataType::UINT8);
  110. LiteMat lite_mat_resize;
  111. ret = ResizeBilinear(lite_mat_rgb, lite_mat_resize, 256, 256);
  112. ASSERT_TRUE(ret == true);
  113. LiteMat lite_mat_convert_float;
  114. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0);
  115. ASSERT_TRUE(ret == true);
  116. LiteMat lite_mat_crop;
  117. ret = Crop(lite_mat_convert_float, lite_mat_crop, 16, 16, 224, 224);
  118. ASSERT_TRUE(ret == true);
  119. std::vector<float> means = {0.485, 0.456, 0.406};
  120. std::vector<float> stds = {0.229, 0.224, 0.225};
  121. LiteMat lite_norm_mat_cut;
  122. ret = SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, stds);
  123. int pad_width = lite_norm_mat_cut.width_ + 20;
  124. int pad_height = lite_norm_mat_cut.height_ + 20;
  125. float *p_rgb_pad = (float *)malloc(pad_width * pad_height * 3 * sizeof(float));
  126. LiteMat makeborder(pad_width, pad_height, 3, (void *)p_rgb_pad, LDataType::FLOAT32);
  127. ret = Pad(lite_norm_mat_cut, makeborder, 10, 30, 40, 10, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  128. cv::Mat dst_image(pad_height, pad_width, CV_8UC3, p_rgb_pad);
  129. free(p_rgb);
  130. free(p_rgb_pad);
  131. }
  132. TEST_F(MindDataImageProcess, test3C) {
  133. std::string filename = "data/dataset/apple.jpg";
  134. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  135. cv::Mat cv_image = cv3CImageProcess(image);
  136. // convert to RGBA for Android bitmap(rgba)
  137. cv::Mat rgba_mat;
  138. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  139. bool ret = false;
  140. LiteMat lite_mat_bgr;
  141. ret =
  142. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  143. ASSERT_TRUE(ret == true);
  144. LiteMat lite_norm_mat_cut;
  145. Lite3CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
  146. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC3, lite_norm_mat_cut.data_ptr_);
  147. CompareMat(cv_image, lite_norm_mat_cut);
  148. }
  149. bool ReadYUV(const char *filename, int w, int h, uint8_t **data) {
  150. FILE *f = fopen(filename, "rb");
  151. if (f == nullptr) {
  152. return false;
  153. }
  154. fseek(f, 0, SEEK_END);
  155. int size = ftell(f);
  156. int expect_size = w * h + 2 * ((w + 1) / 2) * ((h + 1) / 2);
  157. if (size != expect_size) {
  158. fclose(f);
  159. return false;
  160. }
  161. fseek(f, 0, SEEK_SET);
  162. *data = (uint8_t *)malloc(size);
  163. size_t re = fread(*data, 1, size, f);
  164. if (re != size) {
  165. fclose(f);
  166. return false;
  167. }
  168. fclose(f);
  169. return true;
  170. }
  171. TEST_F(MindDataImageProcess, testNV21ToBGR) {
  172. // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv21 ./data/dataset/yuv/test_nv21.yuv
  173. const char *filename = "data/dataset/yuv/test_nv21.yuv";
  174. int w = 1024;
  175. int h = 800;
  176. uint8_t *yuv_data = nullptr;
  177. bool ret = ReadYUV(filename, w, h, &yuv_data);
  178. ASSERT_TRUE(ret == true);
  179. cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1);
  180. memcpy(yuvimg.data, yuv_data, w * h * 3 / 2);
  181. cv::Mat rgbimage;
  182. cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV21);
  183. LiteMat lite_mat_bgr;
  184. ret = InitFromPixel(yuv_data, LPixelType::NV212BGR, LDataType::UINT8, w, h, lite_mat_bgr);
  185. ASSERT_TRUE(ret == true);
  186. cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
  187. }
  188. TEST_F(MindDataImageProcess, testNV12ToBGR) {
  189. // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv12 ./data/dataset/yuv/test_nv12.yuv
  190. const char *filename = "data/dataset/yuv/test_nv12.yuv";
  191. int w = 1024;
  192. int h = 800;
  193. uint8_t *yuv_data = nullptr;
  194. bool ret = ReadYUV(filename, w, h, &yuv_data);
  195. ASSERT_TRUE(ret == true);
  196. cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1);
  197. memcpy(yuvimg.data, yuv_data, w * h * 3 / 2);
  198. cv::Mat rgbimage;
  199. cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV12);
  200. LiteMat lite_mat_bgr;
  201. ret = InitFromPixel(yuv_data, LPixelType::NV122BGR, LDataType::UINT8, w, h, lite_mat_bgr);
  202. ASSERT_TRUE(ret == true);
  203. cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
  204. }
  205. TEST_F(MindDataImageProcess, testExtractChannel) {
  206. std::string filename = "data/dataset/apple.jpg";
  207. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  208. cv::Mat dst_image;
  209. cv::extractChannel(src_image, dst_image, 2);
  210. // convert to RGBA for Android bitmap(rgba)
  211. cv::Mat rgba_mat;
  212. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  213. bool ret = false;
  214. LiteMat lite_mat_bgr;
  215. ret =
  216. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  217. ASSERT_TRUE(ret == true);
  218. LiteMat lite_B;
  219. ret = ExtractChannel(lite_mat_bgr, lite_B, 0);
  220. ASSERT_TRUE(ret == true);
  221. LiteMat lite_R;
  222. ret = ExtractChannel(lite_mat_bgr, lite_R, 2);
  223. ASSERT_TRUE(ret == true);
  224. cv::Mat dst_imageR(lite_R.height_, lite_R.width_, CV_8UC1, lite_R.data_ptr_);
  225. // cv::imwrite("./test_lite_r.jpg", dst_imageR);
  226. }
  227. TEST_F(MindDataImageProcess, testSplit) {
  228. std::string filename = "data/dataset/apple.jpg";
  229. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  230. std::vector<cv::Mat> dst_images;
  231. cv::split(src_image, dst_images);
  232. // convert to RGBA for Android bitmap(rgba)
  233. cv::Mat rgba_mat;
  234. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  235. bool ret = false;
  236. LiteMat lite_mat_bgr;
  237. ret =
  238. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  239. ASSERT_TRUE(ret == true);
  240. std::vector<LiteMat> lite_all;
  241. ret = Split(lite_mat_bgr, lite_all);
  242. ASSERT_TRUE(ret == true);
  243. ASSERT_TRUE(lite_all.size() == 3);
  244. LiteMat lite_r = lite_all[2];
  245. cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
  246. }
  247. TEST_F(MindDataImageProcess, testMerge) {
  248. std::string filename = "data/dataset/apple.jpg";
  249. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  250. std::vector<cv::Mat> dst_images;
  251. cv::split(src_image, dst_images);
  252. // convert to RGBA for Android bitmap(rgba)
  253. cv::Mat rgba_mat;
  254. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  255. bool ret = false;
  256. LiteMat lite_mat_bgr;
  257. ret =
  258. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  259. ASSERT_TRUE(ret == true);
  260. std::vector<LiteMat> lite_all;
  261. ret = Split(lite_mat_bgr, lite_all);
  262. ASSERT_TRUE(ret == true);
  263. ASSERT_TRUE(lite_all.size() == 3);
  264. LiteMat lite_r = lite_all[2];
  265. cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
  266. LiteMat merge_mat;
  267. EXPECT_TRUE(Merge(lite_all, merge_mat));
  268. EXPECT_EQ(merge_mat.height_, lite_mat_bgr.height_);
  269. EXPECT_EQ(merge_mat.width_, lite_mat_bgr.width_);
  270. EXPECT_EQ(merge_mat.channel_, lite_mat_bgr.channel_);
  271. }
  272. void Lite1CImageProcess(LiteMat &lite_mat_bgr, LiteMat &lite_norm_mat_cut) {
  273. LiteMat lite_mat_resize;
  274. int ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  275. ASSERT_TRUE(ret == true);
  276. LiteMat lite_mat_convert_float;
  277. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float);
  278. ASSERT_TRUE(ret == true);
  279. LiteMat lite_mat_cut;
  280. ret = Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224);
  281. ASSERT_TRUE(ret == true);
  282. std::vector<float> means = {0.485};
  283. std::vector<float> stds = {0.229};
  284. ret = SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, stds);
  285. ASSERT_TRUE(ret == true);
  286. return;
  287. }
  288. cv::Mat cv1CImageProcess(cv::Mat &image) {
  289. cv::Mat gray_image;
  290. cv::cvtColor(image, gray_image, CV_BGR2GRAY);
  291. cv::Mat resize_256_image;
  292. cv::resize(gray_image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  293. cv::Mat float_256_image;
  294. resize_256_image.convertTo(float_256_image, CV_32FC3);
  295. cv::Mat roi_224_image;
  296. cv::Rect roi;
  297. roi.x = 16;
  298. roi.y = 16;
  299. roi.width = 224;
  300. roi.height = 224;
  301. float_256_image(roi).copyTo(roi_224_image);
  302. float meanR = 0.485;
  303. float varR = 0.229;
  304. cv::Scalar mean = cv::Scalar(meanR);
  305. cv::Scalar var = cv::Scalar(varR);
  306. cv::Mat imgMean(roi_224_image.size(), CV_32FC1, mean);
  307. cv::Mat imgVar(roi_224_image.size(), CV_32FC1, var);
  308. cv::Mat imgR1 = roi_224_image - imgMean;
  309. cv::Mat imgR2 = imgR1 / imgVar;
  310. return imgR2;
  311. }
  312. TEST_F(MindDataImageProcess, test1C) {
  313. std::string filename = "data/dataset/apple.jpg";
  314. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  315. cv::Mat cv_image = cv1CImageProcess(image);
  316. // convert to RGBA for Android bitmap(rgba)
  317. cv::Mat rgba_mat;
  318. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  319. LiteMat lite_mat_bgr;
  320. bool ret =
  321. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  322. ASSERT_TRUE(ret == true);
  323. LiteMat lite_norm_mat_cut;
  324. Lite1CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
  325. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC1, lite_norm_mat_cut.data_ptr_);
  326. CompareMat(cv_image, lite_norm_mat_cut);
  327. }
  328. TEST_F(MindDataImageProcess, TestPadd) {
  329. std::string filename = "data/dataset/apple.jpg";
  330. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  331. cv::Mat resize_256_image;
  332. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  333. int left = 10;
  334. int right = 10;
  335. int top = 10;
  336. int bottom = 10;
  337. cv::Mat b_image;
  338. cv::Scalar color = cv::Scalar(255, 255, 255);
  339. cv::copyMakeBorder(resize_256_image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  340. cv::Mat rgba_mat;
  341. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  342. LiteMat lite_mat_bgr;
  343. bool ret =
  344. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  345. ASSERT_TRUE(ret == true);
  346. LiteMat lite_mat_resize;
  347. ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  348. ASSERT_TRUE(ret == true);
  349. LiteMat makeborder;
  350. ret = Pad(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  351. ASSERT_TRUE(ret == true);
  352. cv::Mat dst_image(256 + top + bottom, 256 + left + right, CV_8UC3, makeborder.data_ptr_);
  353. }
  354. TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {
  355. std::string benchmark = "data/dataset/testLite/default_boxes.bin";
  356. BoxesConfig config;
  357. config.img_shape = {300, 300};
  358. config.num_default = {3, 6, 6, 6, 6, 6};
  359. config.feature_size = {19, 10, 5, 3, 2, 1};
  360. config.min_scale = 0.2;
  361. config.max_scale = 0.95;
  362. config.aspect_rations = {{2}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}};
  363. config.steps = {16, 32, 64, 100, 150, 300};
  364. config.prior_scaling = {0.1, 0.2};
  365. int rows = 1917;
  366. int cols = 4;
  367. std::vector<double> benchmark_boxes(rows * cols);
  368. std::ifstream in(benchmark, std::ios::in | std::ios::binary);
  369. in.read(reinterpret_cast<char *>(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double));
  370. in.close();
  371. std::vector<std::vector<float>> default_boxes = GetDefaultBoxes(config);
  372. EXPECT_EQ(default_boxes.size(), rows);
  373. EXPECT_EQ(default_boxes[0].size(), cols);
  374. double distance = 0.0f;
  375. for (int i = 0; i < rows; i++) {
  376. for (int j = 0; j < cols; j++) {
  377. distance += pow(default_boxes[i][j] - benchmark_boxes[i * cols + j], 2);
  378. }
  379. }
  380. distance = sqrt(distance);
  381. EXPECT_LT(distance, 1e-5);
  382. }
  383. TEST_F(MindDataImageProcess, TestApplyNms) {
  384. std::vector<std::vector<float>> all_boxes = {{1, 1, 2, 2}, {3, 3, 4, 4}, {5, 5, 6, 6}, {5, 5, 6, 6}};
  385. std::vector<float> all_scores = {0.6, 0.5, 0.4, 0.9};
  386. std::vector<int> keep = ApplyNms(all_boxes, all_scores, 0.5, 10);
  387. ASSERT_TRUE(keep[0] == 3);
  388. ASSERT_TRUE(keep[1] == 0);
  389. ASSERT_TRUE(keep[2] == 1);
  390. }
  391. TEST_F(MindDataImageProcess, TestAffineInput) {
  392. LiteMat src(3, 3);
  393. LiteMat dst;
  394. double M[6] = {1};
  395. EXPECT_FALSE(Affine(src, dst, M, {}, UINT8_C1(0)));
  396. EXPECT_FALSE(Affine(src, dst, M, {3}, UINT8_C1(0)));
  397. EXPECT_FALSE(Affine(src, dst, M, {0, 0}, UINT8_C1(0)));
  398. }
  399. TEST_F(MindDataImageProcess, TestAffine) {
  400. // The input matrix
  401. // 0 0 1 0 0
  402. // 0 0 1 0 0
  403. // 2 2 3 2 2
  404. // 0 0 1 0 0
  405. // 0 0 1 0 0
  406. size_t rows = 5;
  407. size_t cols = 5;
  408. LiteMat src(rows, cols);
  409. for (size_t i = 0; i < rows; i++) {
  410. for (size_t j = 0; j < cols; j++) {
  411. if (i == 2 && j == 2) {
  412. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 3;
  413. } else if (i == 2) {
  414. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 2;
  415. } else if (j == 2) {
  416. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 1;
  417. } else {
  418. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 0;
  419. }
  420. }
  421. }
  422. // Expect output matrix
  423. // 0 0 2 0 0
  424. // 0 0 2 0 0
  425. // 1 1 3 1 1
  426. // 0 0 2 0 0
  427. // 0 0 2 0 0
  428. LiteMat expect(rows, cols);
  429. for (size_t i = 0; i < rows; i++) {
  430. for (size_t j = 0; j < cols; j++) {
  431. if (i == 2 && j == 2) {
  432. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 3;
  433. } else if (i == 2) {
  434. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 1;
  435. } else if (j == 2) {
  436. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 2;
  437. } else {
  438. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 0;
  439. }
  440. }
  441. }
  442. double angle = 90.0f;
  443. cv::Point2f center(rows / 2, cols / 2);
  444. cv::Mat rotate_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
  445. double M[6];
  446. for (size_t i = 0; i < 6; i++) {
  447. M[i] = rotate_matrix.at<double>(i);
  448. }
  449. LiteMat dst;
  450. EXPECT_TRUE(Affine(src, dst, M, {rows, cols}, UINT8_C1(0)));
  451. for (size_t i = 0; i < rows; i++) {
  452. for (size_t j = 0; j < cols; j++) {
  453. EXPECT_EQ(static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j].c1,
  454. static_cast<UINT8_C1 *>(dst.data_ptr_)[i * cols + j].c1);
  455. }
  456. }
  457. }
  458. TEST_F(MindDataImageProcess, TestSubtractUint8) {
  459. const size_t cols = 4;
  460. // Test uint8
  461. LiteMat src1_uint8(1, cols);
  462. LiteMat src2_uint8(1, cols);
  463. LiteMat expect_uint8(1, cols);
  464. for (size_t i = 0; i < cols; i++) {
  465. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 3;
  466. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 2;
  467. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 1;
  468. }
  469. LiteMat dst_uint8;
  470. EXPECT_TRUE(Subtract(src1_uint8, src2_uint8, dst_uint8));
  471. for (size_t i = 0; i < cols; i++) {
  472. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  473. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  474. }
  475. }
  476. TEST_F(MindDataImageProcess, TestSubtractInt8) {
  477. const size_t cols = 4;
  478. // Test int8
  479. LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
  480. LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
  481. LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
  482. for (size_t i = 0; i < cols; i++) {
  483. static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 2;
  484. static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = 3;
  485. static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -1;
  486. }
  487. LiteMat dst_int8;
  488. EXPECT_TRUE(Subtract(src1_int8, src2_int8, dst_int8));
  489. for (size_t i = 0; i < cols; i++) {
  490. EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1, static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
  491. }
  492. }
  493. TEST_F(MindDataImageProcess, TestSubtractUInt16) {
  494. const size_t cols = 4;
  495. // Test uint16
  496. LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
  497. LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
  498. LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
  499. for (size_t i = 0; i < cols; i++) {
  500. static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 2;
  501. static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 3;
  502. static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 0;
  503. }
  504. LiteMat dst_uint16;
  505. EXPECT_TRUE(Subtract(src1_uint16, src2_uint16, dst_uint16));
  506. for (size_t i = 0; i < cols; i++) {
  507. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
  508. static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
  509. }
  510. }
  511. TEST_F(MindDataImageProcess, TestSubtractInt16) {
  512. const size_t cols = 4;
  513. // Test int16
  514. LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
  515. LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
  516. LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
  517. for (size_t i = 0; i < cols; i++) {
  518. static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 2;
  519. static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = 3;
  520. static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -1;
  521. }
  522. LiteMat dst_int16;
  523. EXPECT_TRUE(Subtract(src1_int16, src2_int16, dst_int16));
  524. for (size_t i = 0; i < cols; i++) {
  525. EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  526. static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  527. }
  528. }
  529. TEST_F(MindDataImageProcess, TestSubtractUInt32) {
  530. const size_t cols = 4;
  531. // Test uint16
  532. LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
  533. LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
  534. LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
  535. for (size_t i = 0; i < cols; i++) {
  536. static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 2;
  537. static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 3;
  538. static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 0;
  539. }
  540. LiteMat dst_uint32;
  541. EXPECT_TRUE(Subtract(src1_uint32, src2_uint32, dst_uint32));
  542. for (size_t i = 0; i < cols; i++) {
  543. EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
  544. static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
  545. }
  546. }
  547. TEST_F(MindDataImageProcess, TestSubtractInt32) {
  548. const size_t cols = 4;
  549. // Test int32
  550. LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
  551. LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
  552. LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
  553. for (size_t i = 0; i < cols; i++) {
  554. static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2;
  555. static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = 4;
  556. static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -2;
  557. }
  558. LiteMat dst_int32;
  559. EXPECT_TRUE(Subtract(src1_int32, src2_int32, dst_int32));
  560. for (size_t i = 0; i < cols; i++) {
  561. EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
  562. static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
  563. }
  564. }
  565. TEST_F(MindDataImageProcess, TestSubtractFloat) {
  566. const size_t cols = 4;
  567. // Test float
  568. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  569. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  570. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  571. for (size_t i = 0; i < cols; i++) {
  572. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 3.4;
  573. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = 5.7;
  574. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -2.3;
  575. }
  576. LiteMat dst_float;
  577. EXPECT_TRUE(Subtract(src1_float, src2_float, dst_float));
  578. for (size_t i = 0; i < cols; i++) {
  579. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  580. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  581. }
  582. }
  583. TEST_F(MindDataImageProcess, TestDivideUint8) {
  584. const size_t cols = 4;
  585. // Test uint8
  586. LiteMat src1_uint8(1, cols);
  587. LiteMat src2_uint8(1, cols);
  588. LiteMat expect_uint8(1, cols);
  589. for (size_t i = 0; i < cols; i++) {
  590. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 8;
  591. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 4;
  592. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 2;
  593. }
  594. LiteMat dst_uint8;
  595. EXPECT_TRUE(Divide(src1_uint8, src2_uint8, dst_uint8));
  596. for (size_t i = 0; i < cols; i++) {
  597. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  598. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  599. }
  600. }
  601. TEST_F(MindDataImageProcess, TestDivideInt8) {
  602. const size_t cols = 4;
  603. // Test int8
  604. LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
  605. LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
  606. LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
  607. for (size_t i = 0; i < cols; i++) {
  608. static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 8;
  609. static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = -4;
  610. static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -2;
  611. }
  612. LiteMat dst_int8;
  613. EXPECT_TRUE(Divide(src1_int8, src2_int8, dst_int8));
  614. for (size_t i = 0; i < cols; i++) {
  615. EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1, static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
  616. }
  617. }
  618. TEST_F(MindDataImageProcess, TestDivideUInt16) {
  619. const size_t cols = 4;
  620. // Test uint16
  621. LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
  622. LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
  623. LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
  624. for (size_t i = 0; i < cols; i++) {
  625. static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 40000;
  626. static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 20000;
  627. static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 2;
  628. }
  629. LiteMat dst_uint16;
  630. EXPECT_TRUE(Divide(src1_uint16, src2_uint16, dst_uint16));
  631. for (size_t i = 0; i < cols; i++) {
  632. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
  633. static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
  634. }
  635. }
  636. TEST_F(MindDataImageProcess, TestDivideInt16) {
  637. const size_t cols = 4;
  638. // Test int16
  639. LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
  640. LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
  641. LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
  642. for (size_t i = 0; i < cols; i++) {
  643. static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 30000;
  644. static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = -3;
  645. static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -10000;
  646. }
  647. LiteMat dst_int16;
  648. EXPECT_TRUE(Divide(src1_int16, src2_int16, dst_int16));
  649. for (size_t i = 0; i < cols; i++) {
  650. EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  651. static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  652. }
  653. }
  654. TEST_F(MindDataImageProcess, TestDivideUInt32) {
  655. const size_t cols = 4;
  656. // Test uint16
  657. LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
  658. LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
  659. LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
  660. for (size_t i = 0; i < cols; i++) {
  661. static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 4000000000;
  662. static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 4;
  663. static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 1000000000;
  664. }
  665. LiteMat dst_uint32;
  666. EXPECT_TRUE(Divide(src1_uint32, src2_uint32, dst_uint32));
  667. for (size_t i = 0; i < cols; i++) {
  668. EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
  669. static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
  670. }
  671. }
  672. TEST_F(MindDataImageProcess, TestDivideInt32) {
  673. const size_t cols = 4;
  674. // Test int32
  675. LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
  676. LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
  677. LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
  678. for (size_t i = 0; i < cols; i++) {
  679. static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2000000000;
  680. static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = -2;
  681. static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -1000000000;
  682. }
  683. LiteMat dst_int32;
  684. EXPECT_TRUE(Divide(src1_int32, src2_int32, dst_int32));
  685. for (size_t i = 0; i < cols; i++) {
  686. EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
  687. static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
  688. }
  689. }
  690. TEST_F(MindDataImageProcess, TestDivideFloat) {
  691. const size_t cols = 4;
  692. // Test float
  693. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  694. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  695. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  696. for (size_t i = 0; i < cols; i++) {
  697. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 12.34f;
  698. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = -2.0f;
  699. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -6.17f;
  700. }
  701. LiteMat dst_float;
  702. EXPECT_TRUE(Divide(src1_float, src2_float, dst_float));
  703. for (size_t i = 0; i < cols; i++) {
  704. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  705. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  706. }
  707. }