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 16 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 "utils/log_adapter.h"
  22. #include <fstream>
  23. using namespace mindspore::dataset;
  24. class MindDataImageProcess : public UT::Common {
  25. public:
  26. MindDataImageProcess() {}
  27. void SetUp() {}
  28. };
  29. void CompareMat(cv::Mat cv_mat, LiteMat lite_mat) {
  30. int cv_h = cv_mat.rows;
  31. int cv_w = cv_mat.cols;
  32. int cv_c = cv_mat.channels();
  33. int lite_h = lite_mat.height_;
  34. int lite_w = lite_mat.width_;
  35. int lite_c = lite_mat.channel_;
  36. ASSERT_TRUE(cv_h == lite_h);
  37. ASSERT_TRUE(cv_w == lite_w);
  38. ASSERT_TRUE(cv_c == lite_c);
  39. }
  40. LiteMat Lite3CImageProcess(LiteMat &lite_mat_bgr) {
  41. bool ret;
  42. LiteMat lite_mat_resize;
  43. ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  44. if (!ret) {
  45. MS_LOG(ERROR) << "ResizeBilinear error";
  46. }
  47. LiteMat lite_mat_convert_float;
  48. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0);
  49. if (!ret) {
  50. MS_LOG(ERROR) << "ConvertTo error";
  51. }
  52. LiteMat lite_mat_crop;
  53. ret = Crop(lite_mat_convert_float, lite_mat_crop, 16, 16, 224, 224);
  54. if (!ret) {
  55. MS_LOG(ERROR) << "Crop error";
  56. }
  57. std::vector<float> means = {0.485, 0.456, 0.406};
  58. std::vector<float> stds = {0.229, 0.224, 0.225};
  59. LiteMat lite_norm_mat_cut;
  60. SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, stds);
  61. return lite_norm_mat_cut;
  62. }
  63. cv::Mat cv3CImageProcess(cv::Mat &image) {
  64. cv::Mat resize_256_image;
  65. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  66. cv::Mat float_256_image;
  67. resize_256_image.convertTo(float_256_image, CV_32FC3);
  68. cv::Mat roi_224_image;
  69. cv::Rect roi;
  70. roi.x = 16;
  71. roi.y = 16;
  72. roi.width = 224;
  73. roi.height = 224;
  74. float_256_image(roi).copyTo(roi_224_image);
  75. float meanR = 0.485;
  76. float meanG = 0.456;
  77. float meanB = 0.406;
  78. float varR = 0.229;
  79. float varG = 0.224;
  80. float varB = 0.225;
  81. cv::Scalar mean = cv::Scalar(meanR, meanG, meanB);
  82. cv::Scalar var = cv::Scalar(varR, varG, varB);
  83. cv::Mat imgMean(roi_224_image.size(), CV_32FC3, mean);
  84. cv::Mat imgVar(roi_224_image.size(), CV_32FC3, var);
  85. cv::Mat imgR1 = roi_224_image - imgMean;
  86. cv::Mat imgR2 = imgR1 / imgVar;
  87. return imgR2;
  88. }
  89. TEST_F(MindDataImageProcess, test3C) {
  90. std::string filename = "data/dataset/apple.jpg";
  91. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  92. cv::Mat cv_image = cv3CImageProcess(image);
  93. // cv::imwrite("/home/xlei/test_3cv.jpg", cv_image);
  94. // convert to RGBA for Android bitmap(rgba)
  95. cv::Mat rgba_mat;
  96. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  97. bool ret = false;
  98. LiteMat lite_mat_bgr;
  99. ret =
  100. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  101. if (!ret) {
  102. MS_LOG(ERROR) << "Init From RGBA error";
  103. }
  104. LiteMat lite_norm_mat_cut = Lite3CImageProcess(lite_mat_bgr);
  105. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC3, lite_norm_mat_cut.data_ptr_);
  106. // cv::imwrite("/home/xlei/test_3clite.jpg", dst_image);
  107. CompareMat(cv_image, lite_norm_mat_cut);
  108. }
  109. LiteMat Lite1CImageProcess(LiteMat &lite_mat_bgr) {
  110. LiteMat lite_mat_resize;
  111. ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  112. LiteMat lite_mat_convert_float;
  113. ConvertTo(lite_mat_resize, lite_mat_convert_float);
  114. LiteMat lite_mat_cut;
  115. Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224);
  116. std::vector<float> means = {0.485};
  117. std::vector<float> stds = {0.229};
  118. LiteMat lite_norm_mat_cut;
  119. SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, stds);
  120. return lite_norm_mat_cut;
  121. }
  122. cv::Mat cv1CImageProcess(cv::Mat &image) {
  123. cv::Mat gray_image;
  124. cv::cvtColor(image, gray_image, CV_BGR2GRAY);
  125. cv::Mat resize_256_image;
  126. cv::resize(gray_image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  127. cv::Mat float_256_image;
  128. resize_256_image.convertTo(float_256_image, CV_32FC3);
  129. cv::Mat roi_224_image;
  130. cv::Rect roi;
  131. roi.x = 16;
  132. roi.y = 16;
  133. roi.width = 224;
  134. roi.height = 224;
  135. float_256_image(roi).copyTo(roi_224_image);
  136. float meanR = 0.485;
  137. float varR = 0.229;
  138. cv::Scalar mean = cv::Scalar(meanR);
  139. cv::Scalar var = cv::Scalar(varR);
  140. cv::Mat imgMean(roi_224_image.size(), CV_32FC1, mean);
  141. cv::Mat imgVar(roi_224_image.size(), CV_32FC1, var);
  142. cv::Mat imgR1 = roi_224_image - imgMean;
  143. cv::Mat imgR2 = imgR1 / imgVar;
  144. return imgR2;
  145. }
  146. TEST_F(MindDataImageProcess, test1C) {
  147. std::string filename = "data/dataset/apple.jpg";
  148. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  149. cv::Mat cv_image = cv1CImageProcess(image);
  150. // cv::imwrite("/home/xlei/test_c1v.jpg", cv_image);
  151. // convert to RGBA for Android bitmap(rgba)
  152. cv::Mat rgba_mat;
  153. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  154. LiteMat lite_mat_bgr;
  155. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  156. LiteMat lite_norm_mat_cut = Lite1CImageProcess(lite_mat_bgr);
  157. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC1, lite_norm_mat_cut.data_ptr_);
  158. // cv::imwrite("/home/xlei/test_c1lite.jpg", dst_image);
  159. CompareMat(cv_image, lite_norm_mat_cut);
  160. }
  161. TEST_F(MindDataImageProcess, TestPadd) {
  162. std::string filename = "data/dataset/apple.jpg";
  163. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  164. cv::Mat resize_256_image;
  165. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  166. int left = 10;
  167. int right = 10;
  168. int top = 10;
  169. int bottom = 10;
  170. cv::Mat b_image;
  171. cv::Scalar color = cv::Scalar(255, 255, 255);
  172. cv::copyMakeBorder(resize_256_image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  173. // cv::imwrite("/home/xlei/test_ccc.jpg", b_image);
  174. cv::Mat rgba_mat;
  175. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  176. LiteMat lite_mat_bgr;
  177. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  178. LiteMat lite_mat_resize;
  179. ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  180. LiteMat makeborder;
  181. Pad(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  182. cv::Mat dst_image(256 + top + bottom, 256 + left + right, CV_8UC3, makeborder.data_ptr_);
  183. // cv::imwrite("/home/xlei/test_liteccc.jpg", dst_image);
  184. }
  185. TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {
  186. std::string benchmark = "data/dataset/testLite/default_boxes.bin";
  187. BoxesConfig config;
  188. config.img_shape = {300, 300};
  189. config.num_default = {3, 6, 6, 6, 6, 6};
  190. config.feature_size = {19, 10, 5, 3, 2, 1};
  191. config.min_scale = 0.2;
  192. config.max_scale = 0.95;
  193. config.aspect_rations = {{2}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}};
  194. config.steps = {16, 32, 64, 100, 150, 300};
  195. config.prior_scaling = {0.1, 0.2};
  196. int rows = 1917;
  197. int cols = 4;
  198. std::vector<double> benchmark_boxes(rows * cols);
  199. std::ifstream in(benchmark, std::ios::in | std::ios::binary);
  200. in.read(reinterpret_cast<char *>(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double));
  201. in.close();
  202. std::vector<std::vector<float>> default_boxes = GetDefaultBoxes(config);
  203. EXPECT_EQ(default_boxes.size(), rows);
  204. EXPECT_EQ(default_boxes[0].size(), cols);
  205. double distance = 0.0f;
  206. for (int i = 0; i < rows; i++) {
  207. for (int j = 0; j < cols; j++) {
  208. distance += pow(default_boxes[i][j] - benchmark_boxes[i * cols + j], 2);
  209. }
  210. }
  211. distance = sqrt(distance);
  212. EXPECT_LT(distance, 1e-5);
  213. }
  214. TEST_F(MindDataImageProcess, TestApplyNms) {
  215. std::vector<std::vector<float>> all_boxes = {{1, 1, 2, 2}, {3, 3, 4, 4}, {5, 5, 6, 6}, {5, 5, 6, 6}};
  216. std::vector<float> all_scores = {0.6, 0.5, 0.4, 0.9};
  217. std::vector<int> keep = ApplyNms(all_boxes, all_scores, 0.5, 10);
  218. ASSERT_TRUE(keep[0] == 3);
  219. ASSERT_TRUE(keep[1] == 0);
  220. ASSERT_TRUE(keep[2] == 1);
  221. }
  222. TEST_F(MindDataImageProcess, TestAffineInput) {
  223. LiteMat src(3, 3);
  224. LiteMat dst;
  225. double M[6] = {1};
  226. EXPECT_FALSE(Affine(src, dst, M, {}, UINT8_C1(0)));
  227. EXPECT_FALSE(Affine(src, dst, M, {3}, UINT8_C1(0)));
  228. EXPECT_FALSE(Affine(src, dst, M, {0, 0}, UINT8_C1(0)));
  229. }
  230. TEST_F(MindDataImageProcess, TestAffine) {
  231. // The input matrix
  232. // 0 0 1 0 0
  233. // 0 0 1 0 0
  234. // 2 2 3 2 2
  235. // 0 0 1 0 0
  236. // 0 0 1 0 0
  237. size_t rows = 5;
  238. size_t cols = 5;
  239. LiteMat src(rows, cols);
  240. for (size_t i = 0; i < rows; i++) {
  241. for (size_t j = 0; j < cols; j++) {
  242. if (i == 2 && j == 2) {
  243. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 3;
  244. } else if (i == 2) {
  245. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 2;
  246. } else if (j == 2) {
  247. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 1;
  248. } else {
  249. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 0;
  250. }
  251. }
  252. }
  253. // Expect output matrix
  254. // 0 0 2 0 0
  255. // 0 0 2 0 0
  256. // 1 1 3 1 1
  257. // 0 0 2 0 0
  258. // 0 0 2 0 0
  259. LiteMat expect(rows, cols);
  260. for (size_t i = 0; i < rows; i++) {
  261. for (size_t j = 0; j < cols; j++) {
  262. if (i == 2 && j == 2) {
  263. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 3;
  264. } else if (i == 2) {
  265. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 1;
  266. } else if (j == 2) {
  267. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 2;
  268. } else {
  269. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 0;
  270. }
  271. }
  272. }
  273. double angle = 90.0f;
  274. cv::Point2f center(rows / 2, cols / 2);
  275. cv::Mat rotate_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
  276. double M[6];
  277. for (size_t i = 0; i < 6; i++) {
  278. M[i] = rotate_matrix.at<double>(i);
  279. }
  280. LiteMat dst;
  281. EXPECT_TRUE(Affine(src, dst, M, {rows, cols}, UINT8_C1(0)));
  282. for (size_t i = 0; i < rows; i++) {
  283. for (size_t j = 0; j < cols; j++) {
  284. EXPECT_EQ(static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j].c1,
  285. static_cast<UINT8_C1 *>(dst.data_ptr_)[i * cols + j].c1);
  286. }
  287. }
  288. }
  289. TEST_F(MindDataImageProcess, TestSubtractUint8) {
  290. const size_t cols = 4;
  291. // Test uint8
  292. LiteMat src1_uint8(1, cols);
  293. LiteMat src2_uint8(1, cols);
  294. LiteMat expect_uint8(1, cols);
  295. for (size_t i = 0; i < cols; i++) {
  296. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 3;
  297. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 2;
  298. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 1;
  299. }
  300. LiteMat dst_uint8;
  301. EXPECT_TRUE(Subtract(src1_uint8, src2_uint8, dst_uint8));
  302. for (size_t i = 0; i < cols; i++) {
  303. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  304. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  305. }
  306. }
  307. TEST_F(MindDataImageProcess, TestSubtractInt8) {
  308. const size_t cols = 4;
  309. // Test int8
  310. LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
  311. LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
  312. LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
  313. for (size_t i = 0; i < cols; i++) {
  314. static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 2;
  315. static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = 3;
  316. static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -1;
  317. }
  318. LiteMat dst_int8;
  319. EXPECT_TRUE(Subtract(src1_int8, src2_int8, dst_int8));
  320. for (size_t i = 0; i < cols; i++) {
  321. EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1,
  322. static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
  323. }
  324. }
  325. TEST_F(MindDataImageProcess, TestSubtractUInt16) {
  326. const size_t cols = 4;
  327. // Test uint16
  328. LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
  329. LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
  330. LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
  331. for (size_t i = 0; i < cols; i++) {
  332. static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 2;
  333. static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 3;
  334. static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 0;
  335. }
  336. LiteMat dst_uint16;
  337. EXPECT_TRUE(Subtract(src1_uint16, src2_uint16, dst_uint16));
  338. for (size_t i = 0; i < cols; i++) {
  339. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
  340. static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
  341. }
  342. }
  343. TEST_F(MindDataImageProcess, TestSubtractInt16) {
  344. const size_t cols = 4;
  345. // Test int16
  346. LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
  347. LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
  348. LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
  349. for (size_t i = 0; i < cols; i++) {
  350. static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 2;
  351. static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = 3;
  352. static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -1;
  353. }
  354. LiteMat dst_int16;
  355. EXPECT_TRUE(Subtract(src1_int16, src2_int16, dst_int16));
  356. for (size_t i = 0; i < cols; i++) {
  357. EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  358. static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  359. }
  360. }
  361. TEST_F(MindDataImageProcess, TestSubtractUInt32) {
  362. const size_t cols = 4;
  363. // Test uint16
  364. LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
  365. LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
  366. LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
  367. for (size_t i = 0; i < cols; i++) {
  368. static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 2;
  369. static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 3;
  370. static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 0;
  371. }
  372. LiteMat dst_uint32;
  373. EXPECT_TRUE(Subtract(src1_uint32, src2_uint32, dst_uint32));
  374. for (size_t i = 0; i < cols; i++) {
  375. EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
  376. static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
  377. }
  378. }
  379. TEST_F(MindDataImageProcess, TestSubtractInt32) {
  380. const size_t cols = 4;
  381. // Test int32
  382. LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
  383. LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
  384. LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
  385. for (size_t i = 0; i < cols; i++) {
  386. static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2;
  387. static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = 4;
  388. static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -2;
  389. }
  390. LiteMat dst_int32;
  391. EXPECT_TRUE(Subtract(src1_int32, src2_int32, dst_int32));
  392. for (size_t i = 0; i < cols; i++) {
  393. EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
  394. static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
  395. }
  396. }
  397. TEST_F(MindDataImageProcess, TestSubtractFloat) {
  398. const size_t cols = 4;
  399. // Test float
  400. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  401. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  402. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  403. for (size_t i = 0; i < cols; i++) {
  404. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 3.4;
  405. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = 5.7;
  406. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -2.3;
  407. }
  408. LiteMat dst_float;
  409. EXPECT_TRUE(Subtract(src1_float, src2_float, dst_float));
  410. for (size_t i = 0; i < cols; i++) {
  411. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  412. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  413. }
  414. }