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 20 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. void Lite1CImageProcess(LiteMat &lite_mat_bgr, LiteMat &lite_norm_mat_cut) {
  208. LiteMat lite_mat_resize;
  209. int ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  210. ASSERT_TRUE(ret == true);
  211. LiteMat lite_mat_convert_float;
  212. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float);
  213. ASSERT_TRUE(ret == true);
  214. LiteMat lite_mat_cut;
  215. ret = Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224);
  216. ASSERT_TRUE(ret == true);
  217. std::vector<float> means = {0.485};
  218. std::vector<float> stds = {0.229};
  219. ret = SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, stds);
  220. ASSERT_TRUE(ret == true);
  221. return;
  222. }
  223. cv::Mat cv1CImageProcess(cv::Mat &image) {
  224. cv::Mat gray_image;
  225. cv::cvtColor(image, gray_image, CV_BGR2GRAY);
  226. cv::Mat resize_256_image;
  227. cv::resize(gray_image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  228. cv::Mat float_256_image;
  229. resize_256_image.convertTo(float_256_image, CV_32FC3);
  230. cv::Mat roi_224_image;
  231. cv::Rect roi;
  232. roi.x = 16;
  233. roi.y = 16;
  234. roi.width = 224;
  235. roi.height = 224;
  236. float_256_image(roi).copyTo(roi_224_image);
  237. float meanR = 0.485;
  238. float varR = 0.229;
  239. cv::Scalar mean = cv::Scalar(meanR);
  240. cv::Scalar var = cv::Scalar(varR);
  241. cv::Mat imgMean(roi_224_image.size(), CV_32FC1, mean);
  242. cv::Mat imgVar(roi_224_image.size(), CV_32FC1, var);
  243. cv::Mat imgR1 = roi_224_image - imgMean;
  244. cv::Mat imgR2 = imgR1 / imgVar;
  245. return imgR2;
  246. }
  247. TEST_F(MindDataImageProcess, test1C) {
  248. std::string filename = "data/dataset/apple.jpg";
  249. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  250. cv::Mat cv_image = cv1CImageProcess(image);
  251. // convert to RGBA for Android bitmap(rgba)
  252. cv::Mat rgba_mat;
  253. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  254. LiteMat lite_mat_bgr;
  255. bool ret =
  256. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  257. ASSERT_TRUE(ret == true);
  258. LiteMat lite_norm_mat_cut;
  259. Lite1CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
  260. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC1, lite_norm_mat_cut.data_ptr_);
  261. CompareMat(cv_image, lite_norm_mat_cut);
  262. }
  263. TEST_F(MindDataImageProcess, TestPadd) {
  264. std::string filename = "data/dataset/apple.jpg";
  265. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  266. cv::Mat resize_256_image;
  267. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  268. int left = 10;
  269. int right = 10;
  270. int top = 10;
  271. int bottom = 10;
  272. cv::Mat b_image;
  273. cv::Scalar color = cv::Scalar(255, 255, 255);
  274. cv::copyMakeBorder(resize_256_image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  275. cv::Mat rgba_mat;
  276. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  277. LiteMat lite_mat_bgr;
  278. bool ret =
  279. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  280. ASSERT_TRUE(ret == true);
  281. LiteMat lite_mat_resize;
  282. ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  283. ASSERT_TRUE(ret == true);
  284. LiteMat makeborder;
  285. ret = Pad(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  286. ASSERT_TRUE(ret == true);
  287. cv::Mat dst_image(256 + top + bottom, 256 + left + right, CV_8UC3, makeborder.data_ptr_);
  288. }
  289. TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {
  290. std::string benchmark = "data/dataset/testLite/default_boxes.bin";
  291. BoxesConfig config;
  292. config.img_shape = {300, 300};
  293. config.num_default = {3, 6, 6, 6, 6, 6};
  294. config.feature_size = {19, 10, 5, 3, 2, 1};
  295. config.min_scale = 0.2;
  296. config.max_scale = 0.95;
  297. config.aspect_rations = {{2}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}};
  298. config.steps = {16, 32, 64, 100, 150, 300};
  299. config.prior_scaling = {0.1, 0.2};
  300. int rows = 1917;
  301. int cols = 4;
  302. std::vector<double> benchmark_boxes(rows * cols);
  303. std::ifstream in(benchmark, std::ios::in | std::ios::binary);
  304. in.read(reinterpret_cast<char *>(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double));
  305. in.close();
  306. std::vector<std::vector<float>> default_boxes = GetDefaultBoxes(config);
  307. EXPECT_EQ(default_boxes.size(), rows);
  308. EXPECT_EQ(default_boxes[0].size(), cols);
  309. double distance = 0.0f;
  310. for (int i = 0; i < rows; i++) {
  311. for (int j = 0; j < cols; j++) {
  312. distance += pow(default_boxes[i][j] - benchmark_boxes[i * cols + j], 2);
  313. }
  314. }
  315. distance = sqrt(distance);
  316. EXPECT_LT(distance, 1e-5);
  317. }
  318. TEST_F(MindDataImageProcess, TestApplyNms) {
  319. std::vector<std::vector<float>> all_boxes = {{1, 1, 2, 2}, {3, 3, 4, 4}, {5, 5, 6, 6}, {5, 5, 6, 6}};
  320. std::vector<float> all_scores = {0.6, 0.5, 0.4, 0.9};
  321. std::vector<int> keep = ApplyNms(all_boxes, all_scores, 0.5, 10);
  322. ASSERT_TRUE(keep[0] == 3);
  323. ASSERT_TRUE(keep[1] == 0);
  324. ASSERT_TRUE(keep[2] == 1);
  325. }
  326. TEST_F(MindDataImageProcess, TestAffineInput) {
  327. LiteMat src(3, 3);
  328. LiteMat dst;
  329. double M[6] = {1};
  330. EXPECT_FALSE(Affine(src, dst, M, {}, UINT8_C1(0)));
  331. EXPECT_FALSE(Affine(src, dst, M, {3}, UINT8_C1(0)));
  332. EXPECT_FALSE(Affine(src, dst, M, {0, 0}, UINT8_C1(0)));
  333. }
  334. TEST_F(MindDataImageProcess, TestAffine) {
  335. // The input matrix
  336. // 0 0 1 0 0
  337. // 0 0 1 0 0
  338. // 2 2 3 2 2
  339. // 0 0 1 0 0
  340. // 0 0 1 0 0
  341. size_t rows = 5;
  342. size_t cols = 5;
  343. LiteMat src(rows, cols);
  344. for (size_t i = 0; i < rows; i++) {
  345. for (size_t j = 0; j < cols; j++) {
  346. if (i == 2 && j == 2) {
  347. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 3;
  348. } else if (i == 2) {
  349. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 2;
  350. } else if (j == 2) {
  351. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 1;
  352. } else {
  353. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 0;
  354. }
  355. }
  356. }
  357. // Expect output matrix
  358. // 0 0 2 0 0
  359. // 0 0 2 0 0
  360. // 1 1 3 1 1
  361. // 0 0 2 0 0
  362. // 0 0 2 0 0
  363. LiteMat expect(rows, cols);
  364. for (size_t i = 0; i < rows; i++) {
  365. for (size_t j = 0; j < cols; j++) {
  366. if (i == 2 && j == 2) {
  367. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 3;
  368. } else if (i == 2) {
  369. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 1;
  370. } else if (j == 2) {
  371. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 2;
  372. } else {
  373. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 0;
  374. }
  375. }
  376. }
  377. double angle = 90.0f;
  378. cv::Point2f center(rows / 2, cols / 2);
  379. cv::Mat rotate_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
  380. double M[6];
  381. for (size_t i = 0; i < 6; i++) {
  382. M[i] = rotate_matrix.at<double>(i);
  383. }
  384. LiteMat dst;
  385. EXPECT_TRUE(Affine(src, dst, M, {rows, cols}, UINT8_C1(0)));
  386. for (size_t i = 0; i < rows; i++) {
  387. for (size_t j = 0; j < cols; j++) {
  388. EXPECT_EQ(static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j].c1,
  389. static_cast<UINT8_C1 *>(dst.data_ptr_)[i * cols + j].c1);
  390. }
  391. }
  392. }
  393. TEST_F(MindDataImageProcess, TestSubtractUint8) {
  394. const size_t cols = 4;
  395. // Test uint8
  396. LiteMat src1_uint8(1, cols);
  397. LiteMat src2_uint8(1, cols);
  398. LiteMat expect_uint8(1, cols);
  399. for (size_t i = 0; i < cols; i++) {
  400. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 3;
  401. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 2;
  402. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 1;
  403. }
  404. LiteMat dst_uint8;
  405. EXPECT_TRUE(Subtract(src1_uint8, src2_uint8, dst_uint8));
  406. for (size_t i = 0; i < cols; i++) {
  407. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  408. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  409. }
  410. }
  411. TEST_F(MindDataImageProcess, TestSubtractInt8) {
  412. const size_t cols = 4;
  413. // Test int8
  414. LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
  415. LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
  416. LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
  417. for (size_t i = 0; i < cols; i++) {
  418. static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 2;
  419. static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = 3;
  420. static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -1;
  421. }
  422. LiteMat dst_int8;
  423. EXPECT_TRUE(Subtract(src1_int8, src2_int8, dst_int8));
  424. for (size_t i = 0; i < cols; i++) {
  425. EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1,
  426. static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
  427. }
  428. }
  429. TEST_F(MindDataImageProcess, TestSubtractUInt16) {
  430. const size_t cols = 4;
  431. // Test uint16
  432. LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
  433. LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
  434. LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
  435. for (size_t i = 0; i < cols; i++) {
  436. static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 2;
  437. static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 3;
  438. static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 0;
  439. }
  440. LiteMat dst_uint16;
  441. EXPECT_TRUE(Subtract(src1_uint16, src2_uint16, dst_uint16));
  442. for (size_t i = 0; i < cols; i++) {
  443. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
  444. static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
  445. }
  446. }
  447. TEST_F(MindDataImageProcess, TestSubtractInt16) {
  448. const size_t cols = 4;
  449. // Test int16
  450. LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
  451. LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
  452. LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
  453. for (size_t i = 0; i < cols; i++) {
  454. static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 2;
  455. static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = 3;
  456. static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -1;
  457. }
  458. LiteMat dst_int16;
  459. EXPECT_TRUE(Subtract(src1_int16, src2_int16, dst_int16));
  460. for (size_t i = 0; i < cols; i++) {
  461. EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  462. static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  463. }
  464. }
  465. TEST_F(MindDataImageProcess, TestSubtractUInt32) {
  466. const size_t cols = 4;
  467. // Test uint16
  468. LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
  469. LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
  470. LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
  471. for (size_t i = 0; i < cols; i++) {
  472. static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 2;
  473. static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 3;
  474. static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 0;
  475. }
  476. LiteMat dst_uint32;
  477. EXPECT_TRUE(Subtract(src1_uint32, src2_uint32, dst_uint32));
  478. for (size_t i = 0; i < cols; i++) {
  479. EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
  480. static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
  481. }
  482. }
  483. TEST_F(MindDataImageProcess, TestSubtractInt32) {
  484. const size_t cols = 4;
  485. // Test int32
  486. LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
  487. LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
  488. LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
  489. for (size_t i = 0; i < cols; i++) {
  490. static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2;
  491. static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = 4;
  492. static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -2;
  493. }
  494. LiteMat dst_int32;
  495. EXPECT_TRUE(Subtract(src1_int32, src2_int32, dst_int32));
  496. for (size_t i = 0; i < cols; i++) {
  497. EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
  498. static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
  499. }
  500. }
  501. TEST_F(MindDataImageProcess, TestSubtractFloat) {
  502. const size_t cols = 4;
  503. // Test float
  504. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  505. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  506. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  507. for (size_t i = 0; i < cols; i++) {
  508. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 3.4;
  509. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = 5.7;
  510. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -2.3;
  511. }
  512. LiteMat dst_float;
  513. EXPECT_TRUE(Subtract(src1_float, src2_float, dst_float));
  514. for (size_t i = 0; i < cols; i++) {
  515. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  516. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  517. }
  518. }