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 21 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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, test3C) {
  93. std::string filename = "data/dataset/apple.jpg";
  94. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  95. cv::Mat cv_image = cv3CImageProcess(image);
  96. // convert to RGBA for Android bitmap(rgba)
  97. cv::Mat rgba_mat;
  98. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  99. bool ret = false;
  100. LiteMat lite_mat_bgr;
  101. ret =
  102. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  103. ASSERT_TRUE(ret == true);
  104. LiteMat lite_norm_mat_cut;
  105. Lite3CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
  106. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC3, lite_norm_mat_cut.data_ptr_);
  107. CompareMat(cv_image, lite_norm_mat_cut);
  108. }
  109. bool ReadYUV(const char *filename, int w, int h, uint8_t **data) {
  110. FILE *f = fopen(filename, "rb");
  111. if (f == nullptr) {
  112. return false;
  113. }
  114. fseek(f, 0, SEEK_END);
  115. int size = ftell(f);
  116. int expect_size = w * h + 2 * ((w + 1) / 2) * ((h + 1) / 2);
  117. if (size != expect_size) {
  118. fclose(f);
  119. return false;
  120. }
  121. fseek(f, 0, SEEK_SET);
  122. *data = (uint8_t *)malloc(size);
  123. size_t re = fread(*data, 1, size, f);
  124. if (re != size) {
  125. fclose(f);
  126. return false;
  127. }
  128. fclose(f);
  129. return true;
  130. }
  131. TEST_F(MindDataImageProcess, testNV21ToBGR) {
  132. // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv21 ./data/dataset/yuv/test_nv21.yuv
  133. const char *filename = "data/dataset/yuv/test_nv21.yuv";
  134. int w = 1024;
  135. int h = 800;
  136. uint8_t *yuv_data = nullptr;
  137. bool ret = ReadYUV(filename, w, h, &yuv_data);
  138. ASSERT_TRUE(ret == true);
  139. cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1);
  140. memcpy(yuvimg.data, yuv_data, w * h * 3 / 2);
  141. cv::Mat rgbimage;
  142. cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV21);
  143. LiteMat lite_mat_bgr;
  144. ret = InitFromPixel(yuv_data, LPixelType::NV212BGR, LDataType::UINT8, w, h, lite_mat_bgr);
  145. ASSERT_TRUE(ret == true);
  146. cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
  147. }
  148. TEST_F(MindDataImageProcess, testNV12ToBGR) {
  149. // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv12 ./data/dataset/yuv/test_nv12.yuv
  150. const char *filename = "data/dataset/yuv/test_nv12.yuv";
  151. int w = 1024;
  152. int h = 800;
  153. uint8_t *yuv_data = nullptr;
  154. bool ret = ReadYUV(filename, w, h, &yuv_data);
  155. ASSERT_TRUE(ret == true);
  156. cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1);
  157. memcpy(yuvimg.data, yuv_data, w * h * 3 / 2);
  158. cv::Mat rgbimage;
  159. cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV12);
  160. LiteMat lite_mat_bgr;
  161. ret = InitFromPixel(yuv_data, LPixelType::NV122BGR, LDataType::UINT8, w, h, lite_mat_bgr);
  162. ASSERT_TRUE(ret == true);
  163. cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
  164. }
  165. TEST_F(MindDataImageProcess, testExtractChannel) {
  166. std::string filename = "data/dataset/apple.jpg";
  167. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  168. cv::Mat dst_image;
  169. cv::extractChannel(src_image, dst_image, 2);
  170. // convert to RGBA for Android bitmap(rgba)
  171. cv::Mat rgba_mat;
  172. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  173. bool ret = false;
  174. LiteMat lite_mat_bgr;
  175. ret =
  176. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  177. ASSERT_TRUE(ret == true);
  178. LiteMat lite_B;
  179. ret = ExtractChannel(lite_mat_bgr, lite_B, 0);
  180. ASSERT_TRUE(ret == true);
  181. LiteMat lite_R;
  182. ret = ExtractChannel(lite_mat_bgr, lite_R, 2);
  183. ASSERT_TRUE(ret == true);
  184. cv::Mat dst_imageR(lite_R.height_, lite_R.width_, CV_8UC1, lite_R.data_ptr_);
  185. // cv::imwrite("./test_lite_r.jpg", dst_imageR);
  186. }
  187. TEST_F(MindDataImageProcess, testSplit) {
  188. std::string filename = "data/dataset/apple.jpg";
  189. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  190. std::vector<cv::Mat> dst_images;
  191. cv::split(src_image, dst_images);
  192. // convert to RGBA for Android bitmap(rgba)
  193. cv::Mat rgba_mat;
  194. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  195. bool ret = false;
  196. LiteMat lite_mat_bgr;
  197. ret =
  198. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  199. ASSERT_TRUE(ret == true);
  200. std::vector<LiteMat> lite_all;
  201. ret = Split(lite_mat_bgr, lite_all);
  202. ASSERT_TRUE(ret == true);
  203. ASSERT_TRUE(lite_all.size() == 3);
  204. LiteMat lite_r = lite_all[2];
  205. cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
  206. }
  207. TEST_F(MindDataImageProcess, testMerge) {
  208. std::string filename = "data/dataset/apple.jpg";
  209. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  210. std::vector<cv::Mat> dst_images;
  211. cv::split(src_image, dst_images);
  212. // convert to RGBA for Android bitmap(rgba)
  213. cv::Mat rgba_mat;
  214. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  215. bool ret = false;
  216. LiteMat lite_mat_bgr;
  217. ret =
  218. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  219. ASSERT_TRUE(ret == true);
  220. std::vector<LiteMat> lite_all;
  221. ret = Split(lite_mat_bgr, lite_all);
  222. ASSERT_TRUE(ret == true);
  223. ASSERT_TRUE(lite_all.size() == 3);
  224. LiteMat lite_r = lite_all[2];
  225. cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
  226. LiteMat merge_mat;
  227. EXPECT_TRUE(Merge(lite_all, merge_mat));
  228. EXPECT_EQ(merge_mat.height_, lite_mat_bgr.height_);
  229. EXPECT_EQ(merge_mat.width_, lite_mat_bgr.width_);
  230. EXPECT_EQ(merge_mat.channel_, lite_mat_bgr.channel_);
  231. }
  232. void Lite1CImageProcess(LiteMat &lite_mat_bgr, LiteMat &lite_norm_mat_cut) {
  233. LiteMat lite_mat_resize;
  234. int ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  235. ASSERT_TRUE(ret == true);
  236. LiteMat lite_mat_convert_float;
  237. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float);
  238. ASSERT_TRUE(ret == true);
  239. LiteMat lite_mat_cut;
  240. ret = Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224);
  241. ASSERT_TRUE(ret == true);
  242. std::vector<float> means = {0.485};
  243. std::vector<float> stds = {0.229};
  244. ret = SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, stds);
  245. ASSERT_TRUE(ret == true);
  246. return;
  247. }
  248. cv::Mat cv1CImageProcess(cv::Mat &image) {
  249. cv::Mat gray_image;
  250. cv::cvtColor(image, gray_image, CV_BGR2GRAY);
  251. cv::Mat resize_256_image;
  252. cv::resize(gray_image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  253. cv::Mat float_256_image;
  254. resize_256_image.convertTo(float_256_image, CV_32FC3);
  255. cv::Mat roi_224_image;
  256. cv::Rect roi;
  257. roi.x = 16;
  258. roi.y = 16;
  259. roi.width = 224;
  260. roi.height = 224;
  261. float_256_image(roi).copyTo(roi_224_image);
  262. float meanR = 0.485;
  263. float varR = 0.229;
  264. cv::Scalar mean = cv::Scalar(meanR);
  265. cv::Scalar var = cv::Scalar(varR);
  266. cv::Mat imgMean(roi_224_image.size(), CV_32FC1, mean);
  267. cv::Mat imgVar(roi_224_image.size(), CV_32FC1, var);
  268. cv::Mat imgR1 = roi_224_image - imgMean;
  269. cv::Mat imgR2 = imgR1 / imgVar;
  270. return imgR2;
  271. }
  272. TEST_F(MindDataImageProcess, test1C) {
  273. std::string filename = "data/dataset/apple.jpg";
  274. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  275. cv::Mat cv_image = cv1CImageProcess(image);
  276. // convert to RGBA for Android bitmap(rgba)
  277. cv::Mat rgba_mat;
  278. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  279. LiteMat lite_mat_bgr;
  280. bool ret =
  281. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  282. ASSERT_TRUE(ret == true);
  283. LiteMat lite_norm_mat_cut;
  284. Lite1CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
  285. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC1, lite_norm_mat_cut.data_ptr_);
  286. CompareMat(cv_image, lite_norm_mat_cut);
  287. }
  288. TEST_F(MindDataImageProcess, TestPadd) {
  289. std::string filename = "data/dataset/apple.jpg";
  290. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  291. cv::Mat resize_256_image;
  292. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  293. int left = 10;
  294. int right = 10;
  295. int top = 10;
  296. int bottom = 10;
  297. cv::Mat b_image;
  298. cv::Scalar color = cv::Scalar(255, 255, 255);
  299. cv::copyMakeBorder(resize_256_image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  300. cv::Mat rgba_mat;
  301. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  302. LiteMat lite_mat_bgr;
  303. bool ret =
  304. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  305. ASSERT_TRUE(ret == true);
  306. LiteMat lite_mat_resize;
  307. ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  308. ASSERT_TRUE(ret == true);
  309. LiteMat makeborder;
  310. ret = Pad(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  311. ASSERT_TRUE(ret == true);
  312. cv::Mat dst_image(256 + top + bottom, 256 + left + right, CV_8UC3, makeborder.data_ptr_);
  313. }
  314. TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {
  315. std::string benchmark = "data/dataset/testLite/default_boxes.bin";
  316. BoxesConfig config;
  317. config.img_shape = {300, 300};
  318. config.num_default = {3, 6, 6, 6, 6, 6};
  319. config.feature_size = {19, 10, 5, 3, 2, 1};
  320. config.min_scale = 0.2;
  321. config.max_scale = 0.95;
  322. config.aspect_rations = {{2}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}};
  323. config.steps = {16, 32, 64, 100, 150, 300};
  324. config.prior_scaling = {0.1, 0.2};
  325. int rows = 1917;
  326. int cols = 4;
  327. std::vector<double> benchmark_boxes(rows * cols);
  328. std::ifstream in(benchmark, std::ios::in | std::ios::binary);
  329. in.read(reinterpret_cast<char *>(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double));
  330. in.close();
  331. std::vector<std::vector<float>> default_boxes = GetDefaultBoxes(config);
  332. EXPECT_EQ(default_boxes.size(), rows);
  333. EXPECT_EQ(default_boxes[0].size(), cols);
  334. double distance = 0.0f;
  335. for (int i = 0; i < rows; i++) {
  336. for (int j = 0; j < cols; j++) {
  337. distance += pow(default_boxes[i][j] - benchmark_boxes[i * cols + j], 2);
  338. }
  339. }
  340. distance = sqrt(distance);
  341. EXPECT_LT(distance, 1e-5);
  342. }
  343. TEST_F(MindDataImageProcess, TestApplyNms) {
  344. std::vector<std::vector<float>> all_boxes = {{1, 1, 2, 2}, {3, 3, 4, 4}, {5, 5, 6, 6}, {5, 5, 6, 6}};
  345. std::vector<float> all_scores = {0.6, 0.5, 0.4, 0.9};
  346. std::vector<int> keep = ApplyNms(all_boxes, all_scores, 0.5, 10);
  347. ASSERT_TRUE(keep[0] == 3);
  348. ASSERT_TRUE(keep[1] == 0);
  349. ASSERT_TRUE(keep[2] == 1);
  350. }
  351. TEST_F(MindDataImageProcess, TestAffineInput) {
  352. LiteMat src(3, 3);
  353. LiteMat dst;
  354. double M[6] = {1};
  355. EXPECT_FALSE(Affine(src, dst, M, {}, UINT8_C1(0)));
  356. EXPECT_FALSE(Affine(src, dst, M, {3}, UINT8_C1(0)));
  357. EXPECT_FALSE(Affine(src, dst, M, {0, 0}, UINT8_C1(0)));
  358. }
  359. TEST_F(MindDataImageProcess, TestAffine) {
  360. // The input matrix
  361. // 0 0 1 0 0
  362. // 0 0 1 0 0
  363. // 2 2 3 2 2
  364. // 0 0 1 0 0
  365. // 0 0 1 0 0
  366. size_t rows = 5;
  367. size_t cols = 5;
  368. LiteMat src(rows, cols);
  369. for (size_t i = 0; i < rows; i++) {
  370. for (size_t j = 0; j < cols; j++) {
  371. if (i == 2 && j == 2) {
  372. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 3;
  373. } else if (i == 2) {
  374. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 2;
  375. } else if (j == 2) {
  376. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 1;
  377. } else {
  378. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 0;
  379. }
  380. }
  381. }
  382. // Expect output matrix
  383. // 0 0 2 0 0
  384. // 0 0 2 0 0
  385. // 1 1 3 1 1
  386. // 0 0 2 0 0
  387. // 0 0 2 0 0
  388. LiteMat expect(rows, cols);
  389. for (size_t i = 0; i < rows; i++) {
  390. for (size_t j = 0; j < cols; j++) {
  391. if (i == 2 && j == 2) {
  392. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 3;
  393. } else if (i == 2) {
  394. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 1;
  395. } else if (j == 2) {
  396. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 2;
  397. } else {
  398. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 0;
  399. }
  400. }
  401. }
  402. double angle = 90.0f;
  403. cv::Point2f center(rows / 2, cols / 2);
  404. cv::Mat rotate_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
  405. double M[6];
  406. for (size_t i = 0; i < 6; i++) {
  407. M[i] = rotate_matrix.at<double>(i);
  408. }
  409. LiteMat dst;
  410. EXPECT_TRUE(Affine(src, dst, M, {rows, cols}, UINT8_C1(0)));
  411. for (size_t i = 0; i < rows; i++) {
  412. for (size_t j = 0; j < cols; j++) {
  413. EXPECT_EQ(static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j].c1,
  414. static_cast<UINT8_C1 *>(dst.data_ptr_)[i * cols + j].c1);
  415. }
  416. }
  417. }
  418. TEST_F(MindDataImageProcess, TestSubtractUint8) {
  419. const size_t cols = 4;
  420. // Test uint8
  421. LiteMat src1_uint8(1, cols);
  422. LiteMat src2_uint8(1, cols);
  423. LiteMat expect_uint8(1, cols);
  424. for (size_t i = 0; i < cols; i++) {
  425. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 3;
  426. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 2;
  427. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 1;
  428. }
  429. LiteMat dst_uint8;
  430. EXPECT_TRUE(Subtract(src1_uint8, src2_uint8, dst_uint8));
  431. for (size_t i = 0; i < cols; i++) {
  432. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  433. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  434. }
  435. }
  436. TEST_F(MindDataImageProcess, TestSubtractInt8) {
  437. const size_t cols = 4;
  438. // Test int8
  439. LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
  440. LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
  441. LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
  442. for (size_t i = 0; i < cols; i++) {
  443. static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 2;
  444. static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = 3;
  445. static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -1;
  446. }
  447. LiteMat dst_int8;
  448. EXPECT_TRUE(Subtract(src1_int8, src2_int8, dst_int8));
  449. for (size_t i = 0; i < cols; i++) {
  450. EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1,
  451. static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
  452. }
  453. }
  454. TEST_F(MindDataImageProcess, TestSubtractUInt16) {
  455. const size_t cols = 4;
  456. // Test uint16
  457. LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
  458. LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
  459. LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
  460. for (size_t i = 0; i < cols; i++) {
  461. static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 2;
  462. static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 3;
  463. static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 0;
  464. }
  465. LiteMat dst_uint16;
  466. EXPECT_TRUE(Subtract(src1_uint16, src2_uint16, dst_uint16));
  467. for (size_t i = 0; i < cols; i++) {
  468. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
  469. static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
  470. }
  471. }
  472. TEST_F(MindDataImageProcess, TestSubtractInt16) {
  473. const size_t cols = 4;
  474. // Test int16
  475. LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
  476. LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
  477. LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
  478. for (size_t i = 0; i < cols; i++) {
  479. static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 2;
  480. static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = 3;
  481. static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -1;
  482. }
  483. LiteMat dst_int16;
  484. EXPECT_TRUE(Subtract(src1_int16, src2_int16, dst_int16));
  485. for (size_t i = 0; i < cols; i++) {
  486. EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  487. static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  488. }
  489. }
  490. TEST_F(MindDataImageProcess, TestSubtractUInt32) {
  491. const size_t cols = 4;
  492. // Test uint16
  493. LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
  494. LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
  495. LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
  496. for (size_t i = 0; i < cols; i++) {
  497. static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 2;
  498. static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 3;
  499. static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 0;
  500. }
  501. LiteMat dst_uint32;
  502. EXPECT_TRUE(Subtract(src1_uint32, src2_uint32, dst_uint32));
  503. for (size_t i = 0; i < cols; i++) {
  504. EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
  505. static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
  506. }
  507. }
  508. TEST_F(MindDataImageProcess, TestSubtractInt32) {
  509. const size_t cols = 4;
  510. // Test int32
  511. LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
  512. LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
  513. LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
  514. for (size_t i = 0; i < cols; i++) {
  515. static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2;
  516. static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = 4;
  517. static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -2;
  518. }
  519. LiteMat dst_int32;
  520. EXPECT_TRUE(Subtract(src1_int32, src2_int32, dst_int32));
  521. for (size_t i = 0; i < cols; i++) {
  522. EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
  523. static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
  524. }
  525. }
  526. TEST_F(MindDataImageProcess, TestSubtractFloat) {
  527. const size_t cols = 4;
  528. // Test float
  529. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  530. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  531. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  532. for (size_t i = 0; i < cols; i++) {
  533. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 3.4;
  534. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = 5.7;
  535. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -2.3;
  536. }
  537. LiteMat dst_float;
  538. EXPECT_TRUE(Subtract(src1_float, src2_float, dst_float));
  539. for (size_t i = 0; i < cols; i++) {
  540. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  541. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  542. }
  543. }