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 63 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
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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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. void AccuracyComparison(const std::vector<std::vector<double>> &expect, LiteMat &value) {
  82. for (int i = 0; i < expect.size(); i++) {
  83. for (int j = 0; j < expect[0].size(); j++) {
  84. double middle = std::fabs(expect[i][j] - value.ptr<double>(i)[j]);
  85. ASSERT_TRUE(middle <= 0.005);
  86. }
  87. }
  88. }
  89. TEST_F(MindDataImageProcess, testRGB) {
  90. std::string filename = "data/dataset/apple.jpg";
  91. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  92. cv::Mat rgba_mat;
  93. cv::cvtColor(image, rgba_mat, CV_BGR2RGB);
  94. bool ret = false;
  95. LiteMat lite_mat_rgb;
  96. ret = InitFromPixel(rgba_mat.data, LPixelType::RGB, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_rgb);
  97. ASSERT_TRUE(ret == true);
  98. cv::Mat dst_image(lite_mat_rgb.height_, lite_mat_rgb.width_, CV_8UC3, lite_mat_rgb.data_ptr_);
  99. }
  100. TEST_F(MindDataImageProcess, testLoadByMemPtr) {
  101. std::string filename = "data/dataset/apple.jpg";
  102. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  103. cv::Mat rgba_mat;
  104. cv::cvtColor(image, rgba_mat, CV_BGR2RGB);
  105. bool ret = false;
  106. int width = rgba_mat.cols;
  107. int height = rgba_mat.rows;
  108. uchar *p_rgb = (uchar *)malloc(width * height * 3 * sizeof(uchar));
  109. for (int i = 0; i < height; i++) {
  110. const uchar *current = rgba_mat.ptr<uchar>(i);
  111. for (int j = 0; j < width; j++) {
  112. p_rgb[i * width * 3 + 3 * j + 0] = current[3 * j + 0];
  113. p_rgb[i * width * 3 + 3 * j + 1] = current[3 * j + 1];
  114. p_rgb[i * width * 3 + 3 * j + 2] = current[3 * j + 2];
  115. }
  116. }
  117. LiteMat lite_mat_rgb(width, height, 3, (void *)p_rgb, LDataType::UINT8);
  118. LiteMat lite_mat_resize;
  119. ret = ResizeBilinear(lite_mat_rgb, lite_mat_resize, 256, 256);
  120. ASSERT_TRUE(ret == true);
  121. LiteMat lite_mat_convert_float;
  122. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0);
  123. ASSERT_TRUE(ret == true);
  124. LiteMat lite_mat_crop;
  125. ret = Crop(lite_mat_convert_float, lite_mat_crop, 16, 16, 224, 224);
  126. ASSERT_TRUE(ret == true);
  127. std::vector<float> means = {0.485, 0.456, 0.406};
  128. std::vector<float> stds = {0.229, 0.224, 0.225};
  129. LiteMat lite_norm_mat_cut;
  130. ret = SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, stds);
  131. int pad_width = lite_norm_mat_cut.width_ + 20;
  132. int pad_height = lite_norm_mat_cut.height_ + 20;
  133. float *p_rgb_pad = (float *)malloc(pad_width * pad_height * 3 * sizeof(float));
  134. LiteMat makeborder(pad_width, pad_height, 3, (void *)p_rgb_pad, LDataType::FLOAT32);
  135. ret = Pad(lite_norm_mat_cut, makeborder, 10, 30, 40, 10, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  136. cv::Mat dst_image(pad_height, pad_width, CV_8UC3, p_rgb_pad);
  137. free(p_rgb);
  138. free(p_rgb_pad);
  139. }
  140. TEST_F(MindDataImageProcess, test3C) {
  141. std::string filename = "data/dataset/apple.jpg";
  142. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  143. cv::Mat cv_image = cv3CImageProcess(image);
  144. // convert to RGBA for Android bitmap(rgba)
  145. cv::Mat rgba_mat;
  146. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  147. bool ret = false;
  148. LiteMat lite_mat_bgr;
  149. ret =
  150. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  151. ASSERT_TRUE(ret == true);
  152. LiteMat lite_norm_mat_cut;
  153. Lite3CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
  154. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC3, lite_norm_mat_cut.data_ptr_);
  155. CompareMat(cv_image, lite_norm_mat_cut);
  156. }
  157. bool ReadYUV(const char *filename, int w, int h, uint8_t **data) {
  158. FILE *f = fopen(filename, "rb");
  159. if (f == nullptr) {
  160. return false;
  161. }
  162. fseek(f, 0, SEEK_END);
  163. int size = ftell(f);
  164. int expect_size = w * h + 2 * ((w + 1) / 2) * ((h + 1) / 2);
  165. if (size != expect_size) {
  166. fclose(f);
  167. return false;
  168. }
  169. fseek(f, 0, SEEK_SET);
  170. *data = (uint8_t *)malloc(size);
  171. size_t re = fread(*data, 1, size, f);
  172. if (re != size) {
  173. fclose(f);
  174. return false;
  175. }
  176. fclose(f);
  177. return true;
  178. }
  179. TEST_F(MindDataImageProcess, TestRGBA2GRAY) {
  180. std::string filename = "data/dataset/apple.jpg";
  181. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  182. cv::Mat gray_image;
  183. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  184. cv::Mat rgba_mat;
  185. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  186. bool ret = false;
  187. LiteMat lite_mat_gray;
  188. ret =
  189. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  190. ASSERT_TRUE(ret == true);
  191. double distance = 0.f;
  192. int total_size = gray_image.cols * gray_image.rows * gray_image.channels();
  193. for (int i = 0; i < total_size; i++) {
  194. distance += pow((uint8_t)gray_image.data[i] - ((uint8_t *)lite_mat_gray)[i], 2);
  195. }
  196. distance = sqrt(distance / total_size);
  197. EXPECT_EQ(distance, 0.0f);
  198. }
  199. TEST_F(MindDataImageProcess, testNV21ToBGR) {
  200. // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv21 ./data/dataset/yuv/test_nv21.yuv
  201. const char *filename = "data/dataset/yuv/test_nv21.yuv";
  202. int w = 1024;
  203. int h = 800;
  204. uint8_t *yuv_data = nullptr;
  205. bool ret = ReadYUV(filename, w, h, &yuv_data);
  206. ASSERT_TRUE(ret == true);
  207. cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1);
  208. memcpy(yuvimg.data, yuv_data, w * h * 3 / 2);
  209. cv::Mat rgbimage;
  210. cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV21);
  211. LiteMat lite_mat_bgr;
  212. ret = InitFromPixel(yuv_data, LPixelType::NV212BGR, LDataType::UINT8, w, h, lite_mat_bgr);
  213. ASSERT_TRUE(ret == true);
  214. cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
  215. }
  216. TEST_F(MindDataImageProcess, testNV12ToBGR) {
  217. // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv12 ./data/dataset/yuv/test_nv12.yuv
  218. const char *filename = "data/dataset/yuv/test_nv12.yuv";
  219. int w = 1024;
  220. int h = 800;
  221. uint8_t *yuv_data = nullptr;
  222. bool ret = ReadYUV(filename, w, h, &yuv_data);
  223. ASSERT_TRUE(ret == true);
  224. cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1);
  225. memcpy(yuvimg.data, yuv_data, w * h * 3 / 2);
  226. cv::Mat rgbimage;
  227. cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV12);
  228. LiteMat lite_mat_bgr;
  229. ret = InitFromPixel(yuv_data, LPixelType::NV122BGR, LDataType::UINT8, w, h, lite_mat_bgr);
  230. ASSERT_TRUE(ret == true);
  231. cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
  232. }
  233. TEST_F(MindDataImageProcess, testExtractChannel) {
  234. std::string filename = "data/dataset/apple.jpg";
  235. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  236. cv::Mat dst_image;
  237. cv::extractChannel(src_image, dst_image, 2);
  238. // convert to RGBA for Android bitmap(rgba)
  239. cv::Mat rgba_mat;
  240. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  241. bool ret = false;
  242. LiteMat lite_mat_bgr;
  243. ret =
  244. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  245. ASSERT_TRUE(ret == true);
  246. LiteMat lite_B;
  247. ret = ExtractChannel(lite_mat_bgr, lite_B, 0);
  248. ASSERT_TRUE(ret == true);
  249. LiteMat lite_R;
  250. ret = ExtractChannel(lite_mat_bgr, lite_R, 2);
  251. ASSERT_TRUE(ret == true);
  252. cv::Mat dst_imageR(lite_R.height_, lite_R.width_, CV_8UC1, lite_R.data_ptr_);
  253. // cv::imwrite("./test_lite_r.jpg", dst_imageR);
  254. }
  255. TEST_F(MindDataImageProcess, testSplit) {
  256. std::string filename = "data/dataset/apple.jpg";
  257. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  258. std::vector<cv::Mat> dst_images;
  259. cv::split(src_image, dst_images);
  260. // convert to RGBA for Android bitmap(rgba)
  261. cv::Mat rgba_mat;
  262. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  263. bool ret = false;
  264. LiteMat lite_mat_bgr;
  265. ret =
  266. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  267. ASSERT_TRUE(ret == true);
  268. std::vector<LiteMat> lite_all;
  269. ret = Split(lite_mat_bgr, lite_all);
  270. ASSERT_TRUE(ret == true);
  271. ASSERT_TRUE(lite_all.size() == 3);
  272. LiteMat lite_r = lite_all[2];
  273. cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
  274. }
  275. TEST_F(MindDataImageProcess, testMerge) {
  276. std::string filename = "data/dataset/apple.jpg";
  277. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  278. std::vector<cv::Mat> dst_images;
  279. cv::split(src_image, dst_images);
  280. // convert to RGBA for Android bitmap(rgba)
  281. cv::Mat rgba_mat;
  282. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  283. bool ret = false;
  284. LiteMat lite_mat_bgr;
  285. ret =
  286. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  287. ASSERT_TRUE(ret == true);
  288. std::vector<LiteMat> lite_all;
  289. ret = Split(lite_mat_bgr, lite_all);
  290. ASSERT_TRUE(ret == true);
  291. ASSERT_TRUE(lite_all.size() == 3);
  292. LiteMat lite_r = lite_all[2];
  293. cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
  294. LiteMat merge_mat;
  295. EXPECT_TRUE(Merge(lite_all, merge_mat));
  296. EXPECT_EQ(merge_mat.height_, lite_mat_bgr.height_);
  297. EXPECT_EQ(merge_mat.width_, lite_mat_bgr.width_);
  298. EXPECT_EQ(merge_mat.channel_, lite_mat_bgr.channel_);
  299. }
  300. void Lite1CImageProcess(LiteMat &lite_mat_bgr, LiteMat &lite_norm_mat_cut) {
  301. LiteMat lite_mat_resize;
  302. int ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  303. ASSERT_TRUE(ret == true);
  304. LiteMat lite_mat_convert_float;
  305. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float);
  306. ASSERT_TRUE(ret == true);
  307. LiteMat lite_mat_cut;
  308. ret = Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224);
  309. ASSERT_TRUE(ret == true);
  310. std::vector<float> means = {0.485};
  311. std::vector<float> stds = {0.229};
  312. ret = SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, stds);
  313. ASSERT_TRUE(ret == true);
  314. return;
  315. }
  316. cv::Mat cv1CImageProcess(cv::Mat &image) {
  317. cv::Mat gray_image;
  318. cv::cvtColor(image, gray_image, CV_BGR2GRAY);
  319. cv::Mat resize_256_image;
  320. cv::resize(gray_image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  321. cv::Mat float_256_image;
  322. resize_256_image.convertTo(float_256_image, CV_32FC3);
  323. cv::Mat roi_224_image;
  324. cv::Rect roi;
  325. roi.x = 16;
  326. roi.y = 16;
  327. roi.width = 224;
  328. roi.height = 224;
  329. float_256_image(roi).copyTo(roi_224_image);
  330. float meanR = 0.485;
  331. float varR = 0.229;
  332. cv::Scalar mean = cv::Scalar(meanR);
  333. cv::Scalar var = cv::Scalar(varR);
  334. cv::Mat imgMean(roi_224_image.size(), CV_32FC1, mean);
  335. cv::Mat imgVar(roi_224_image.size(), CV_32FC1, var);
  336. cv::Mat imgR1 = roi_224_image - imgMean;
  337. cv::Mat imgR2 = imgR1 / imgVar;
  338. return imgR2;
  339. }
  340. TEST_F(MindDataImageProcess, test1C) {
  341. std::string filename = "data/dataset/apple.jpg";
  342. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  343. cv::Mat cv_image = cv1CImageProcess(image);
  344. // convert to RGBA for Android bitmap(rgba)
  345. cv::Mat rgba_mat;
  346. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  347. LiteMat lite_mat_bgr;
  348. bool ret =
  349. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  350. ASSERT_TRUE(ret == true);
  351. LiteMat lite_norm_mat_cut;
  352. Lite1CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
  353. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC1, lite_norm_mat_cut.data_ptr_);
  354. CompareMat(cv_image, lite_norm_mat_cut);
  355. }
  356. TEST_F(MindDataImageProcess, TestPadd) {
  357. std::string filename = "data/dataset/apple.jpg";
  358. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  359. int left = 10;
  360. int right = 20;
  361. int top = 30;
  362. int bottom = 40;
  363. cv::Mat b_image;
  364. cv::Scalar color = cv::Scalar(255, 255, 255);
  365. cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  366. cv::Mat rgba_mat;
  367. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  368. LiteMat lite_mat_bgr;
  369. bool ret =
  370. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  371. ASSERT_TRUE(ret == true);
  372. LiteMat makeborder;
  373. ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  374. ASSERT_TRUE(ret == true);
  375. size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
  376. double distance = 0.0f;
  377. for (size_t i = 0; i < total_size; i++) {
  378. distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
  379. }
  380. distance = sqrt(distance / total_size);
  381. EXPECT_EQ(distance, 0.0f);
  382. }
  383. TEST_F(MindDataImageProcess, TestPadZero) {
  384. std::string filename = "data/dataset/apple.jpg";
  385. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  386. int left = 0;
  387. int right = 0;
  388. int top = 0;
  389. int bottom = 0;
  390. cv::Mat b_image;
  391. cv::Scalar color = cv::Scalar(255, 255, 255);
  392. cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  393. cv::Mat rgba_mat;
  394. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  395. LiteMat lite_mat_bgr;
  396. bool ret =
  397. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  398. ASSERT_TRUE(ret == true);
  399. LiteMat makeborder;
  400. ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  401. ASSERT_TRUE(ret == true);
  402. size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
  403. double distance = 0.0f;
  404. for (size_t i = 0; i < total_size; i++) {
  405. distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
  406. }
  407. distance = sqrt(distance / total_size);
  408. EXPECT_EQ(distance, 0.0f);
  409. }
  410. TEST_F(MindDataImageProcess, TestPadReplicate) {
  411. std::string filename = "data/dataset/apple.jpg";
  412. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  413. int left = 20;
  414. int right = 20;
  415. int top = 20;
  416. int bottom = 20;
  417. cv::Mat b_image;
  418. cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_REPLICATE);
  419. cv::Mat rgba_mat;
  420. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  421. LiteMat lite_mat_bgr;
  422. bool ret =
  423. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  424. ASSERT_TRUE(ret == true);
  425. LiteMat makeborder;
  426. ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_REPLICATE);
  427. ASSERT_TRUE(ret == true);
  428. size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
  429. double distance = 0.0f;
  430. for (size_t i = 0; i < total_size; i++) {
  431. distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
  432. }
  433. distance = sqrt(distance / total_size);
  434. EXPECT_EQ(distance, 0.0f);
  435. }
  436. TEST_F(MindDataImageProcess, TestPadReflect101) {
  437. std::string filename = "data/dataset/apple.jpg";
  438. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  439. int left = 20;
  440. int right = 20;
  441. int top = 20;
  442. int bottom = 20;
  443. cv::Mat b_image;
  444. cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_REFLECT_101);
  445. cv::Mat rgba_mat;
  446. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  447. LiteMat lite_mat_bgr;
  448. bool ret =
  449. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  450. ASSERT_TRUE(ret == true);
  451. LiteMat makeborder;
  452. ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_REFLECT_101);
  453. ASSERT_TRUE(ret == true);
  454. size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
  455. double distance = 0.0f;
  456. for (size_t i = 0; i < total_size; i++) {
  457. distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
  458. }
  459. distance = sqrt(distance / total_size);
  460. EXPECT_EQ(distance, 0.0f);
  461. }
  462. TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {
  463. std::string benchmark = "data/dataset/testLite/default_boxes.bin";
  464. BoxesConfig config;
  465. config.img_shape = {300, 300};
  466. config.num_default = {3, 6, 6, 6, 6, 6};
  467. config.feature_size = {19, 10, 5, 3, 2, 1};
  468. config.min_scale = 0.2;
  469. config.max_scale = 0.95;
  470. config.aspect_rations = {{2}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}};
  471. config.steps = {16, 32, 64, 100, 150, 300};
  472. config.prior_scaling = {0.1, 0.2};
  473. int rows = 1917;
  474. int cols = 4;
  475. std::vector<double> benchmark_boxes(rows * cols);
  476. std::ifstream in(benchmark, std::ios::in | std::ios::binary);
  477. in.read(reinterpret_cast<char *>(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double));
  478. in.close();
  479. std::vector<std::vector<float>> default_boxes = GetDefaultBoxes(config);
  480. EXPECT_EQ(default_boxes.size(), rows);
  481. EXPECT_EQ(default_boxes[0].size(), cols);
  482. double distance = 0.0f;
  483. for (int i = 0; i < rows; i++) {
  484. for (int j = 0; j < cols; j++) {
  485. distance += pow(default_boxes[i][j] - benchmark_boxes[i * cols + j], 2);
  486. }
  487. }
  488. distance = sqrt(distance);
  489. EXPECT_LT(distance, 1e-5);
  490. }
  491. TEST_F(MindDataImageProcess, TestApplyNms) {
  492. std::vector<std::vector<float>> all_boxes = {{1, 1, 2, 2}, {3, 3, 4, 4}, {5, 5, 6, 6}, {5, 5, 6, 6}};
  493. std::vector<float> all_scores = {0.6, 0.5, 0.4, 0.9};
  494. std::vector<int> keep = ApplyNms(all_boxes, all_scores, 0.5, 10);
  495. ASSERT_TRUE(keep[0] == 3);
  496. ASSERT_TRUE(keep[1] == 0);
  497. ASSERT_TRUE(keep[2] == 1);
  498. }
  499. TEST_F(MindDataImageProcess, TestAffineInput) {
  500. LiteMat src(3, 3);
  501. LiteMat dst;
  502. double M[6] = {1};
  503. EXPECT_FALSE(Affine(src, dst, M, {}, UINT8_C1(0)));
  504. EXPECT_FALSE(Affine(src, dst, M, {3}, UINT8_C1(0)));
  505. EXPECT_FALSE(Affine(src, dst, M, {0, 0}, UINT8_C1(0)));
  506. }
  507. TEST_F(MindDataImageProcess, TestAffine) {
  508. // The input matrix
  509. // 0 0 1 0 0
  510. // 0 0 1 0 0
  511. // 2 2 3 2 2
  512. // 0 0 1 0 0
  513. // 0 0 1 0 0
  514. size_t rows = 5;
  515. size_t cols = 5;
  516. LiteMat src(rows, cols);
  517. for (size_t i = 0; i < rows; i++) {
  518. for (size_t j = 0; j < cols; j++) {
  519. if (i == 2 && j == 2) {
  520. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 3;
  521. } else if (i == 2) {
  522. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 2;
  523. } else if (j == 2) {
  524. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 1;
  525. } else {
  526. static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 0;
  527. }
  528. }
  529. }
  530. // Expect output matrix
  531. // 0 0 2 0 0
  532. // 0 0 2 0 0
  533. // 1 1 3 1 1
  534. // 0 0 2 0 0
  535. // 0 0 2 0 0
  536. LiteMat expect(rows, cols);
  537. for (size_t i = 0; i < rows; i++) {
  538. for (size_t j = 0; j < cols; j++) {
  539. if (i == 2 && j == 2) {
  540. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 3;
  541. } else if (i == 2) {
  542. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 1;
  543. } else if (j == 2) {
  544. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 2;
  545. } else {
  546. static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 0;
  547. }
  548. }
  549. }
  550. double angle = 90.0f;
  551. cv::Point2f center(rows / 2, cols / 2);
  552. cv::Mat rotate_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
  553. double M[6];
  554. for (size_t i = 0; i < 6; i++) {
  555. M[i] = rotate_matrix.at<double>(i);
  556. }
  557. LiteMat dst;
  558. EXPECT_TRUE(Affine(src, dst, M, {rows, cols}, UINT8_C1(0)));
  559. for (size_t i = 0; i < rows; i++) {
  560. for (size_t j = 0; j < cols; j++) {
  561. EXPECT_EQ(static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j].c1,
  562. static_cast<UINT8_C1 *>(dst.data_ptr_)[i * cols + j].c1);
  563. }
  564. }
  565. }
  566. TEST_F(MindDataImageProcess, TestSubtractUint8) {
  567. const size_t cols = 4;
  568. // Test uint8
  569. LiteMat src1_uint8(1, cols);
  570. LiteMat src2_uint8(1, cols);
  571. LiteMat expect_uint8(1, cols);
  572. for (size_t i = 0; i < cols; i++) {
  573. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 3;
  574. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 2;
  575. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 1;
  576. }
  577. LiteMat dst_uint8;
  578. EXPECT_TRUE(Subtract(src1_uint8, src2_uint8, &dst_uint8));
  579. for (size_t i = 0; i < cols; i++) {
  580. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  581. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  582. }
  583. }
  584. TEST_F(MindDataImageProcess, TestSubtractInt8) {
  585. const size_t cols = 4;
  586. // Test int8
  587. LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
  588. LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
  589. LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
  590. for (size_t i = 0; i < cols; i++) {
  591. static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 2;
  592. static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = 3;
  593. static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -1;
  594. }
  595. LiteMat dst_int8;
  596. EXPECT_TRUE(Subtract(src1_int8, src2_int8, &dst_int8));
  597. for (size_t i = 0; i < cols; i++) {
  598. EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1, static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
  599. }
  600. }
  601. TEST_F(MindDataImageProcess, TestSubtractUInt16) {
  602. const size_t cols = 4;
  603. // Test uint16
  604. LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
  605. LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
  606. LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
  607. for (size_t i = 0; i < cols; i++) {
  608. static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 2;
  609. static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 3;
  610. static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 0;
  611. }
  612. LiteMat dst_uint16;
  613. EXPECT_TRUE(Subtract(src1_uint16, src2_uint16, &dst_uint16));
  614. for (size_t i = 0; i < cols; i++) {
  615. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
  616. static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
  617. }
  618. }
  619. TEST_F(MindDataImageProcess, TestSubtractInt16) {
  620. const size_t cols = 4;
  621. // Test int16
  622. LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
  623. LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
  624. LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
  625. for (size_t i = 0; i < cols; i++) {
  626. static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 2;
  627. static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = 3;
  628. static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -1;
  629. }
  630. LiteMat dst_int16;
  631. EXPECT_TRUE(Subtract(src1_int16, src2_int16, &dst_int16));
  632. for (size_t i = 0; i < cols; i++) {
  633. EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  634. static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  635. }
  636. }
  637. TEST_F(MindDataImageProcess, TestSubtractUInt32) {
  638. const size_t cols = 4;
  639. // Test uint16
  640. LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
  641. LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
  642. LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
  643. for (size_t i = 0; i < cols; i++) {
  644. static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 2;
  645. static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 3;
  646. static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 0;
  647. }
  648. LiteMat dst_uint32;
  649. EXPECT_TRUE(Subtract(src1_uint32, src2_uint32, &dst_uint32));
  650. for (size_t i = 0; i < cols; i++) {
  651. EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
  652. static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
  653. }
  654. }
  655. TEST_F(MindDataImageProcess, TestSubtractInt32) {
  656. const size_t cols = 4;
  657. // Test int32
  658. LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
  659. LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
  660. LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
  661. for (size_t i = 0; i < cols; i++) {
  662. static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2;
  663. static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = 4;
  664. static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -2;
  665. }
  666. LiteMat dst_int32;
  667. EXPECT_TRUE(Subtract(src1_int32, src2_int32, &dst_int32));
  668. for (size_t i = 0; i < cols; i++) {
  669. EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
  670. static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
  671. }
  672. }
  673. TEST_F(MindDataImageProcess, TestSubtractFloat) {
  674. const size_t cols = 4;
  675. // Test float
  676. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  677. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  678. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  679. for (size_t i = 0; i < cols; i++) {
  680. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 3.4;
  681. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = 5.7;
  682. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -2.3;
  683. }
  684. LiteMat dst_float;
  685. EXPECT_TRUE(Subtract(src1_float, src2_float, &dst_float));
  686. for (size_t i = 0; i < cols; i++) {
  687. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  688. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  689. }
  690. }
  691. TEST_F(MindDataImageProcess, TestDivideUint8) {
  692. const size_t cols = 4;
  693. // Test uint8
  694. LiteMat src1_uint8(1, cols);
  695. LiteMat src2_uint8(1, cols);
  696. LiteMat expect_uint8(1, cols);
  697. for (size_t i = 0; i < cols; i++) {
  698. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 8;
  699. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 4;
  700. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 2;
  701. }
  702. LiteMat dst_uint8;
  703. EXPECT_TRUE(Divide(src1_uint8, src2_uint8, &dst_uint8));
  704. for (size_t i = 0; i < cols; i++) {
  705. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  706. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  707. }
  708. }
  709. TEST_F(MindDataImageProcess, TestDivideInt8) {
  710. const size_t cols = 4;
  711. // Test int8
  712. LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
  713. LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
  714. LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
  715. for (size_t i = 0; i < cols; i++) {
  716. static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 8;
  717. static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = -4;
  718. static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -2;
  719. }
  720. LiteMat dst_int8;
  721. EXPECT_TRUE(Divide(src1_int8, src2_int8, &dst_int8));
  722. for (size_t i = 0; i < cols; i++) {
  723. EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1, static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
  724. }
  725. }
  726. TEST_F(MindDataImageProcess, TestDivideUInt16) {
  727. const size_t cols = 4;
  728. // Test uint16
  729. LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
  730. LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
  731. LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
  732. for (size_t i = 0; i < cols; i++) {
  733. static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 40000;
  734. static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 20000;
  735. static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 2;
  736. }
  737. LiteMat dst_uint16;
  738. EXPECT_TRUE(Divide(src1_uint16, src2_uint16, &dst_uint16));
  739. for (size_t i = 0; i < cols; i++) {
  740. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
  741. static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
  742. }
  743. }
  744. TEST_F(MindDataImageProcess, TestDivideInt16) {
  745. const size_t cols = 4;
  746. // Test int16
  747. LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
  748. LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
  749. LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
  750. for (size_t i = 0; i < cols; i++) {
  751. static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 30000;
  752. static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = -3;
  753. static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -10000;
  754. }
  755. LiteMat dst_int16;
  756. EXPECT_TRUE(Divide(src1_int16, src2_int16, &dst_int16));
  757. for (size_t i = 0; i < cols; i++) {
  758. EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  759. static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  760. }
  761. }
  762. TEST_F(MindDataImageProcess, TestDivideUInt32) {
  763. const size_t cols = 4;
  764. // Test uint16
  765. LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
  766. LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
  767. LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
  768. for (size_t i = 0; i < cols; i++) {
  769. static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 4000000000;
  770. static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 4;
  771. static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 1000000000;
  772. }
  773. LiteMat dst_uint32;
  774. EXPECT_TRUE(Divide(src1_uint32, src2_uint32, &dst_uint32));
  775. for (size_t i = 0; i < cols; i++) {
  776. EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
  777. static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
  778. }
  779. }
  780. TEST_F(MindDataImageProcess, TestDivideInt32) {
  781. const size_t cols = 4;
  782. // Test int32
  783. LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
  784. LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
  785. LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
  786. for (size_t i = 0; i < cols; i++) {
  787. static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2000000000;
  788. static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = -2;
  789. static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -1000000000;
  790. }
  791. LiteMat dst_int32;
  792. EXPECT_TRUE(Divide(src1_int32, src2_int32, &dst_int32));
  793. for (size_t i = 0; i < cols; i++) {
  794. EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
  795. static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
  796. }
  797. }
  798. TEST_F(MindDataImageProcess, TestDivideFloat) {
  799. const size_t cols = 4;
  800. // Test float
  801. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  802. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  803. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  804. for (size_t i = 0; i < cols; i++) {
  805. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 12.34f;
  806. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = -2.0f;
  807. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -6.17f;
  808. }
  809. LiteMat dst_float;
  810. EXPECT_TRUE(Divide(src1_float, src2_float, &dst_float));
  811. for (size_t i = 0; i < cols; i++) {
  812. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  813. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  814. }
  815. }
  816. TEST_F(MindDataImageProcess, TestMultiplyUint8) {
  817. const size_t cols = 4;
  818. // Test uint8
  819. LiteMat src1_uint8(1, cols);
  820. LiteMat src2_uint8(1, cols);
  821. LiteMat expect_uint8(1, cols);
  822. for (size_t i = 0; i < cols; i++) {
  823. static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 8;
  824. static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 4;
  825. static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 32;
  826. }
  827. LiteMat dst_uint8;
  828. EXPECT_TRUE(Multiply(src1_uint8, src2_uint8, &dst_uint8));
  829. for (size_t i = 0; i < cols; i++) {
  830. EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
  831. static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
  832. }
  833. }
  834. TEST_F(MindDataImageProcess, TestMultiplyUInt16) {
  835. const size_t cols = 4;
  836. // Test int16
  837. LiteMat src1_int16(1, cols, LDataType(LDataType::UINT16));
  838. LiteMat src2_int16(1, cols, LDataType(LDataType::UINT16));
  839. LiteMat expect_int16(1, cols, LDataType(LDataType::UINT16));
  840. for (size_t i = 0; i < cols; i++) {
  841. static_cast<UINT16_C1 *>(src1_int16.data_ptr_)[i] = 60000;
  842. static_cast<UINT16_C1 *>(src2_int16.data_ptr_)[i] = 2;
  843. static_cast<UINT16_C1 *>(expect_int16.data_ptr_)[i] = 65535;
  844. }
  845. LiteMat dst_int16;
  846. EXPECT_TRUE(Multiply(src1_int16, src2_int16, &dst_int16));
  847. for (size_t i = 0; i < cols; i++) {
  848. EXPECT_EQ(static_cast<UINT16_C1 *>(expect_int16.data_ptr_)[i].c1,
  849. static_cast<UINT16_C1 *>(dst_int16.data_ptr_)[i].c1);
  850. }
  851. }
  852. TEST_F(MindDataImageProcess, TestMultiplyFloat) {
  853. const size_t cols = 4;
  854. // Test float
  855. LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
  856. LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
  857. LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
  858. for (size_t i = 0; i < cols; i++) {
  859. static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 30.0f;
  860. static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = -2.0f;
  861. static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -60.0f;
  862. }
  863. LiteMat dst_float;
  864. EXPECT_TRUE(Multiply(src1_float, src2_float, &dst_float));
  865. for (size_t i = 0; i < cols; i++) {
  866. EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
  867. static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
  868. }
  869. }
  870. TEST_F(MindDataImageProcess, TestExtractChannel) {
  871. LiteMat lite_single;
  872. LiteMat lite_mat = LiteMat(1, 4, 3, LDataType::UINT16);
  873. EXPECT_FALSE(ExtractChannel(lite_mat, lite_single, 0));
  874. EXPECT_TRUE(lite_single.IsEmpty());
  875. }
  876. TEST_F(MindDataImageProcess, testROI3C) {
  877. std::string filename = "data/dataset/apple.jpg";
  878. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  879. cv::Mat cv_roi = cv::Mat(src_image, cv::Rect(500, 500, 3000, 1500));
  880. cv::imwrite("./cv_roi.jpg", cv_roi);
  881. bool ret = false;
  882. LiteMat lite_mat_bgr;
  883. ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
  884. EXPECT_TRUE(ret);
  885. LiteMat lite_roi;
  886. ret = lite_mat_bgr.GetROI(500, 500, 3000, 1500, lite_roi);
  887. EXPECT_TRUE(ret);
  888. LiteMat lite_roi_save(3000, 1500, lite_roi.channel_, LDataType::UINT8);
  889. for (size_t i = 0; i < lite_roi.height_; i++) {
  890. const unsigned char *ptr = lite_roi.ptr<unsigned char>(i);
  891. size_t image_size = lite_roi.width_ * lite_roi.channel_ * sizeof(unsigned char);
  892. unsigned char *dst_ptr = (unsigned char *)lite_roi_save.data_ptr_ + image_size * i;
  893. (void)memcpy(dst_ptr, ptr, image_size);
  894. }
  895. cv::Mat dst_imageR(lite_roi_save.height_, lite_roi_save.width_, CV_8UC3, lite_roi_save.data_ptr_);
  896. cv::imwrite("./lite_roi.jpg", dst_imageR);
  897. }
  898. TEST_F(MindDataImageProcess, testROI3CFalse) {
  899. std::string filename = "data/dataset/apple.jpg";
  900. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  901. cv::Mat cv_roi = cv::Mat(src_image, cv::Rect(500, 500, 3000, 1500));
  902. cv::imwrite("./cv_roi.jpg", cv_roi);
  903. bool ret = false;
  904. LiteMat lite_mat_bgr;
  905. ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
  906. EXPECT_TRUE(ret);
  907. LiteMat lite_roi;
  908. ret = lite_mat_bgr.GetROI(500, 500, 1200, -100, lite_roi);
  909. EXPECT_FALSE(ret);
  910. }
  911. TEST_F(MindDataImageProcess, testROI1C) {
  912. std::string filename = "data/dataset/apple.jpg";
  913. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  914. cv::Mat gray_image;
  915. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  916. cv::Mat cv_roi_gray = cv::Mat(gray_image, cv::Rect(500, 500, 3000, 1500));
  917. cv::imwrite("./cv_roi_gray.jpg", cv_roi_gray);
  918. cv::Mat rgba_mat;
  919. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  920. bool ret = false;
  921. LiteMat lite_mat_gray;
  922. ret =
  923. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  924. EXPECT_TRUE(ret);
  925. LiteMat lite_roi_gray;
  926. ret = lite_mat_gray.GetROI(500, 500, 3000, 1500, lite_roi_gray);
  927. EXPECT_TRUE(ret);
  928. LiteMat lite_roi_gray_save(3000, 1500, lite_roi_gray.channel_, LDataType::UINT8);
  929. for (size_t i = 0; i < lite_roi_gray.height_; i++) {
  930. const unsigned char *ptr = lite_roi_gray.ptr<unsigned char>(i);
  931. size_t image_size = lite_roi_gray.width_ * lite_roi_gray.channel_ * sizeof(unsigned char);
  932. unsigned char *dst_ptr = (unsigned char *)lite_roi_gray_save.data_ptr_ + image_size * i;
  933. (void)memcpy(dst_ptr, ptr, image_size);
  934. }
  935. cv::Mat dst_imageR(lite_roi_gray_save.height_, lite_roi_gray_save.width_, CV_8UC1, lite_roi_gray_save.data_ptr_);
  936. cv::imwrite("./lite_roi.jpg", dst_imageR);
  937. }
  938. // warp
  939. TEST_F(MindDataImageProcess, testWarpAffineBGR) {
  940. std::string filename = "data/dataset/apple.jpg";
  941. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  942. cv::Point2f srcTri[3];
  943. cv::Point2f dstTri[3];
  944. srcTri[0] = cv::Point2f(0, 0);
  945. srcTri[1] = cv::Point2f(src_image.cols - 1, 0);
  946. srcTri[2] = cv::Point2f(0, src_image.rows - 1);
  947. dstTri[0] = cv::Point2f(src_image.cols * 0.0, src_image.rows * 0.33);
  948. dstTri[1] = cv::Point2f(src_image.cols * 0.85, src_image.rows * 0.25);
  949. dstTri[2] = cv::Point2f(src_image.cols * 0.15, src_image.rows * 0.7);
  950. cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
  951. ;
  952. cv::Mat warp_dstImage;
  953. cv::warpAffine(src_image, warp_dstImage, warp_mat, warp_dstImage.size());
  954. cv::imwrite("./warpAffine_cv_bgr.png", warp_dstImage);
  955. bool ret = false;
  956. LiteMat lite_mat_bgr;
  957. ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
  958. EXPECT_TRUE(ret);
  959. double *mat_ptr = warp_mat.ptr<double>(0);
  960. LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
  961. LiteMat lite_warp;
  962. std::vector<uint8_t> borderValues;
  963. borderValues.push_back(0);
  964. borderValues.push_back(0);
  965. borderValues.push_back(0);
  966. ret = WarpAffineBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_, lite_mat_bgr.height_,
  967. PADD_BORDER_CONSTANT, borderValues);
  968. EXPECT_TRUE(ret);
  969. cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
  970. cv::imwrite("./warpAffine_lite_bgr.png", dst_imageR);
  971. }
  972. TEST_F(MindDataImageProcess, testWarpAffineBGRScale) {
  973. std::string filename = "data/dataset/apple.jpg";
  974. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  975. cv::Point2f srcTri[3];
  976. cv::Point2f dstTri[3];
  977. srcTri[0] = cv::Point2f(10, 20);
  978. srcTri[1] = cv::Point2f(src_image.cols - 1 - 100, 0);
  979. srcTri[2] = cv::Point2f(0, src_image.rows - 1 - 300);
  980. dstTri[0] = cv::Point2f(src_image.cols * 0.22, src_image.rows * 0.33);
  981. dstTri[1] = cv::Point2f(src_image.cols * 0.87, src_image.rows * 0.75);
  982. dstTri[2] = cv::Point2f(src_image.cols * 0.35, src_image.rows * 0.37);
  983. cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
  984. ;
  985. cv::Mat warp_dstImage;
  986. cv::warpAffine(src_image, warp_dstImage, warp_mat, warp_dstImage.size());
  987. cv::imwrite("./warpAffine_cv_bgr_scale.png", warp_dstImage);
  988. bool ret = false;
  989. LiteMat lite_mat_bgr;
  990. ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
  991. EXPECT_TRUE(ret);
  992. double *mat_ptr = warp_mat.ptr<double>(0);
  993. LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
  994. LiteMat lite_warp;
  995. std::vector<uint8_t> borderValues;
  996. borderValues.push_back(0);
  997. borderValues.push_back(0);
  998. borderValues.push_back(0);
  999. ret = WarpAffineBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_, lite_mat_bgr.height_,
  1000. PADD_BORDER_CONSTANT, borderValues);
  1001. EXPECT_TRUE(ret);
  1002. cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
  1003. cv::imwrite("./warpAffine_lite_bgr_scale.png", dst_imageR);
  1004. }
  1005. TEST_F(MindDataImageProcess, testWarpAffineBGRResize) {
  1006. std::string filename = "data/dataset/apple.jpg";
  1007. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1008. cv::Point2f srcTri[3];
  1009. cv::Point2f dstTri[3];
  1010. srcTri[0] = cv::Point2f(10, 20);
  1011. srcTri[1] = cv::Point2f(src_image.cols - 1 - 100, 0);
  1012. srcTri[2] = cv::Point2f(0, src_image.rows - 1 - 300);
  1013. dstTri[0] = cv::Point2f(src_image.cols * 0.22, src_image.rows * 0.33);
  1014. dstTri[1] = cv::Point2f(src_image.cols * 0.87, src_image.rows * 0.75);
  1015. dstTri[2] = cv::Point2f(src_image.cols * 0.35, src_image.rows * 0.37);
  1016. cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
  1017. ;
  1018. cv::Mat warp_dstImage;
  1019. cv::warpAffine(src_image, warp_dstImage, warp_mat, cv::Size(src_image.cols + 200, src_image.rows - 300));
  1020. cv::imwrite("./warpAffine_cv_bgr_resize.png", warp_dstImage);
  1021. bool ret = false;
  1022. LiteMat lite_mat_bgr;
  1023. ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
  1024. EXPECT_TRUE(ret);
  1025. double *mat_ptr = warp_mat.ptr<double>(0);
  1026. LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
  1027. LiteMat lite_warp;
  1028. std::vector<uint8_t> borderValues;
  1029. borderValues.push_back(0);
  1030. borderValues.push_back(0);
  1031. borderValues.push_back(0);
  1032. ret = WarpAffineBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_ + 200, lite_mat_bgr.height_ - 300,
  1033. PADD_BORDER_CONSTANT, borderValues);
  1034. EXPECT_TRUE(ret);
  1035. cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
  1036. cv::imwrite("./warpAffine_lite_bgr_resize.png", dst_imageR);
  1037. }
  1038. TEST_F(MindDataImageProcess, testWarpAffineGray) {
  1039. std::string filename = "data/dataset/apple.jpg";
  1040. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1041. cv::Mat gray_image;
  1042. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  1043. cv::Point2f srcTri[3];
  1044. cv::Point2f dstTri[3];
  1045. srcTri[0] = cv::Point2f(0, 0);
  1046. srcTri[1] = cv::Point2f(src_image.cols - 1, 0);
  1047. srcTri[2] = cv::Point2f(0, src_image.rows - 1);
  1048. dstTri[0] = cv::Point2f(src_image.cols * 0.0, src_image.rows * 0.33);
  1049. dstTri[1] = cv::Point2f(src_image.cols * 0.85, src_image.rows * 0.25);
  1050. dstTri[2] = cv::Point2f(src_image.cols * 0.15, src_image.rows * 0.7);
  1051. cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
  1052. ;
  1053. cv::Mat warp_gray_dstImage;
  1054. cv::warpAffine(gray_image, warp_gray_dstImage, warp_mat, cv::Size(src_image.cols + 200, src_image.rows - 300));
  1055. cv::imwrite("./warpAffine_cv_gray.png", warp_gray_dstImage);
  1056. cv::Mat rgba_mat;
  1057. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1058. bool ret = false;
  1059. LiteMat lite_mat_gray;
  1060. ret =
  1061. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1062. EXPECT_TRUE(ret);
  1063. double *mat_ptr = warp_mat.ptr<double>(0);
  1064. LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
  1065. LiteMat lite_warp;
  1066. std::vector<uint8_t> borderValues;
  1067. borderValues.push_back(0);
  1068. ret = WarpAffineBilinear(lite_mat_gray, lite_warp, lite_M, lite_mat_gray.width_ + 200, lite_mat_gray.height_ - 300,
  1069. PADD_BORDER_CONSTANT, borderValues);
  1070. EXPECT_TRUE(ret);
  1071. cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC1, lite_warp.data_ptr_);
  1072. cv::imwrite("./warpAffine_lite_gray.png", dst_imageR);
  1073. }
  1074. TEST_F(MindDataImageProcess, testWarpPerspectiveBGRResize) {
  1075. std::string filename = "data/dataset/apple.jpg";
  1076. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1077. cv::Point2f srcQuad[4], dstQuad[4];
  1078. srcQuad[0].x = 0;
  1079. srcQuad[0].y = 0;
  1080. srcQuad[1].x = src_image.cols - 1.;
  1081. srcQuad[1].y = 0;
  1082. srcQuad[2].x = 0;
  1083. srcQuad[2].y = src_image.rows - 1;
  1084. srcQuad[3].x = src_image.cols - 1;
  1085. srcQuad[3].y = src_image.rows - 1;
  1086. dstQuad[0].x = src_image.cols * 0.05;
  1087. dstQuad[0].y = src_image.rows * 0.33;
  1088. dstQuad[1].x = src_image.cols * 0.9;
  1089. dstQuad[1].y = src_image.rows * 0.25;
  1090. dstQuad[2].x = src_image.cols * 0.2;
  1091. dstQuad[2].y = src_image.rows * 0.7;
  1092. dstQuad[3].x = src_image.cols * 0.8;
  1093. dstQuad[3].y = src_image.rows * 0.9;
  1094. cv::Mat ptran = cv::getPerspectiveTransform(srcQuad, dstQuad, cv::DECOMP_SVD);
  1095. cv::Mat warp_dstImage;
  1096. cv::warpPerspective(src_image, warp_dstImage, ptran, cv::Size(src_image.cols + 200, src_image.rows - 300));
  1097. cv::imwrite("./warpPerspective_cv_bgr.png", warp_dstImage);
  1098. bool ret = false;
  1099. LiteMat lite_mat_bgr;
  1100. ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
  1101. EXPECT_TRUE(ret);
  1102. double *mat_ptr = ptran.ptr<double>(0);
  1103. LiteMat lite_M(3, 3, 1, mat_ptr, LDataType::DOUBLE);
  1104. LiteMat lite_warp;
  1105. std::vector<uint8_t> borderValues;
  1106. borderValues.push_back(0);
  1107. borderValues.push_back(0);
  1108. borderValues.push_back(0);
  1109. ret = WarpPerspectiveBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_ + 200, lite_mat_bgr.height_ - 300,
  1110. PADD_BORDER_CONSTANT, borderValues);
  1111. EXPECT_TRUE(ret);
  1112. cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
  1113. cv::imwrite("./warpPerspective_lite_bgr.png", dst_imageR);
  1114. }
  1115. TEST_F(MindDataImageProcess, testWarpPerspectiveGrayResize) {
  1116. std::string filename = "data/dataset/apple.jpg";
  1117. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1118. cv::Mat gray_image;
  1119. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  1120. cv::Point2f srcQuad[4], dstQuad[4];
  1121. srcQuad[0].x = 0;
  1122. srcQuad[0].y = 0;
  1123. srcQuad[1].x = src_image.cols - 1.;
  1124. srcQuad[1].y = 0;
  1125. srcQuad[2].x = 0;
  1126. srcQuad[2].y = src_image.rows - 1;
  1127. srcQuad[3].x = src_image.cols - 1;
  1128. srcQuad[3].y = src_image.rows - 1;
  1129. dstQuad[0].x = src_image.cols * 0.05;
  1130. dstQuad[0].y = src_image.rows * 0.33;
  1131. dstQuad[1].x = src_image.cols * 0.9;
  1132. dstQuad[1].y = src_image.rows * 0.25;
  1133. dstQuad[2].x = src_image.cols * 0.2;
  1134. dstQuad[2].y = src_image.rows * 0.7;
  1135. dstQuad[3].x = src_image.cols * 0.8;
  1136. dstQuad[3].y = src_image.rows * 0.9;
  1137. cv::Mat ptran = cv::getPerspectiveTransform(srcQuad, dstQuad, cv::DECOMP_SVD);
  1138. cv::Mat warp_dstImage;
  1139. cv::warpPerspective(gray_image, warp_dstImage, ptran, cv::Size(gray_image.cols + 200, gray_image.rows - 300));
  1140. cv::imwrite("./warpPerspective_cv_gray.png", warp_dstImage);
  1141. cv::Mat rgba_mat;
  1142. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1143. bool ret = false;
  1144. LiteMat lite_mat_gray;
  1145. ret =
  1146. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1147. EXPECT_TRUE(ret);
  1148. double *mat_ptr = ptran.ptr<double>(0);
  1149. LiteMat lite_M(3, 3, 1, mat_ptr, LDataType::DOUBLE);
  1150. LiteMat lite_warp;
  1151. std::vector<uint8_t> borderValues;
  1152. borderValues.push_back(0);
  1153. ret = WarpPerspectiveBilinear(lite_mat_gray, lite_warp, lite_M, lite_mat_gray.width_ + 200,
  1154. lite_mat_gray.height_ - 300, PADD_BORDER_CONSTANT, borderValues);
  1155. EXPECT_TRUE(ret);
  1156. cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC1, lite_warp.data_ptr_);
  1157. cv::imwrite("./warpPerspective_lite_gray.png", dst_imageR);
  1158. }
  1159. TEST_F(MindDataImageProcess, testGetRotationMatrix2D) {
  1160. std::vector<std::vector<double>> expect_matrix = {{0.250000, 0.433013, -0.116025}, {-0.433013, 0.250000, 1.933013}};
  1161. double angle = 60.0;
  1162. double scale = 0.5;
  1163. LiteMat M;
  1164. bool ret = false;
  1165. ret = GetRotationMatrix2D(1.0f, 2.0f, angle, scale, M);
  1166. EXPECT_TRUE(ret);
  1167. AccuracyComparison(expect_matrix, M);
  1168. }
  1169. TEST_F(MindDataImageProcess, testGetPerspectiveTransform) {
  1170. std::vector<std::vector<double>> expect_matrix = {
  1171. {1.272113, 3.665216, -788.484287}, {-0.394146, 3.228247, -134.009780}, {-0.001460, 0.006414, 1}};
  1172. std::vector<Point> src = {Point(165, 270), Point(835, 270), Point(360, 125), Point(615, 125)};
  1173. std::vector<Point> dst = {Point(165, 270), Point(835, 270), Point(100, 100), Point(500, 30)};
  1174. LiteMat M;
  1175. bool ret = false;
  1176. ret = GetPerspectiveTransform(src, dst, M);
  1177. EXPECT_TRUE(ret);
  1178. AccuracyComparison(expect_matrix, M);
  1179. }
  1180. TEST_F(MindDataImageProcess, testGetAffineTransform) {
  1181. std::vector<std::vector<double>> expect_matrix = {{0.400000, 0.066667, 16.666667}, {0.000000, 0.333333, 23.333333}};
  1182. std::vector<Point> src = {Point(50, 50), Point(200, 50), Point(50, 200)};
  1183. std::vector<Point> dst = {Point(40, 40), Point(100, 40), Point(50, 90)};
  1184. LiteMat M;
  1185. bool ret = false;
  1186. ret = GetAffineTransform(src, dst, M);
  1187. EXPECT_TRUE(ret);
  1188. AccuracyComparison(expect_matrix, M);
  1189. }
  1190. TEST_F(MindDataImageProcess, TestConv2D8U) {
  1191. LiteMat lite_mat_src;
  1192. lite_mat_src.Init(3, 3, 1, LDataType::UINT8);
  1193. uint8_t *src_ptr = lite_mat_src;
  1194. for (int i = 0; i < 9; i++) {
  1195. src_ptr[i] = i % 3;
  1196. }
  1197. LiteMat kernel;
  1198. kernel.Init(3, 3, 1, LDataType::FLOAT32);
  1199. float *kernel_ptr = kernel;
  1200. for (int i = 0; i < 9; i++) {
  1201. kernel_ptr[i] = i % 2;
  1202. }
  1203. LiteMat lite_mat_dst;
  1204. bool ret = Conv2D(lite_mat_src, kernel, lite_mat_dst, LDataType::UINT8);
  1205. ASSERT_TRUE(ret == true);
  1206. std::vector<uint8_t> expected_result = {2, 4, 6, 2, 4, 6, 2, 4, 6};
  1207. size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
  1208. float distance = 0.0f;
  1209. for (size_t i = 0; i < total_size; i++) {
  1210. distance += pow(((uint8_t *)lite_mat_dst)[i] - expected_result[i], 2);
  1211. }
  1212. distance = sqrt(distance / total_size);
  1213. EXPECT_EQ(distance, 0.0f);
  1214. }
  1215. TEST_F(MindDataImageProcess, TestConv2D32F) {
  1216. LiteMat lite_mat_src;
  1217. lite_mat_src.Init(2, 2, 1, LDataType::FLOAT32);
  1218. float *src_ptr = lite_mat_src;
  1219. for (int i = 0; i < 4; i++) {
  1220. src_ptr[i] = static_cast<float>(i) / 2;
  1221. }
  1222. LiteMat kernel;
  1223. kernel.Init(2, 2, 1, LDataType::FLOAT32);
  1224. float *kernel_ptr = kernel;
  1225. for (int i = 0; i < 4; i++) {
  1226. kernel_ptr[i] = static_cast<float>(i);
  1227. }
  1228. LiteMat lite_mat_dst;
  1229. bool ret = Conv2D(lite_mat_src, kernel, lite_mat_dst, LDataType::FLOAT32);
  1230. ASSERT_TRUE(ret == true);
  1231. std::vector<float> expected_result = {2.f, 3.f, 6.f, 7.f};
  1232. size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
  1233. float distance = 0.0f;
  1234. for (size_t i = 0; i < total_size; i++) {
  1235. distance += pow(((float *)lite_mat_dst)[i] - expected_result[i], 2);
  1236. }
  1237. distance = sqrt(distance / total_size);
  1238. EXPECT_EQ(distance, 0.0f);
  1239. }
  1240. TEST_F(MindDataImageProcess, TestGaussianBlurSize35) {
  1241. std::string filename = "data/dataset/apple.jpg";
  1242. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1243. cv::Mat dst_image;
  1244. cv::GaussianBlur(src_image, dst_image, cv::Size(3, 5), 3, 3);
  1245. cv::Mat rgba_mat;
  1246. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1247. LiteMat lite_mat_bgr;
  1248. bool ret =
  1249. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  1250. ASSERT_TRUE(ret == true);
  1251. LiteMat lite_mat_dst;
  1252. ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 5}, 3, 3);
  1253. ASSERT_TRUE(ret == true);
  1254. size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
  1255. double distance = 0.0f;
  1256. for (size_t i = 0; i < total_size; i++) {
  1257. distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
  1258. }
  1259. distance = sqrt(distance / total_size);
  1260. EXPECT_LE(distance, 1.0f);
  1261. }
  1262. TEST_F(MindDataImageProcess, TestGaussianBlurSize13) {
  1263. std::string filename = "data/dataset/apple.jpg";
  1264. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1265. cv::Mat dst_image;
  1266. cv::GaussianBlur(src_image, dst_image, cv::Size(1, 3), 3);
  1267. cv::Mat rgba_mat;
  1268. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1269. LiteMat lite_mat_bgr;
  1270. bool ret =
  1271. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  1272. ASSERT_TRUE(ret == true);
  1273. LiteMat lite_mat_dst;
  1274. ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {1, 3}, 3);
  1275. ASSERT_TRUE(ret == true);
  1276. size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
  1277. double distance = 0.0f;
  1278. for (size_t i = 0; i < total_size; i++) {
  1279. distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
  1280. }
  1281. distance = sqrt(distance / total_size);
  1282. EXPECT_LE(distance, 1.0f);
  1283. }
  1284. TEST_F(MindDataImageProcess, TestGaussianBlurInvalidParams) {
  1285. std::string filename = "data/dataset/apple.jpg";
  1286. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1287. cv::Mat rgba_mat;
  1288. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1289. LiteMat lite_mat_bgr;
  1290. bool ret =
  1291. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  1292. ASSERT_TRUE(ret == true);
  1293. LiteMat lite_mat_dst;
  1294. // even size
  1295. ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 4}, 3);
  1296. ASSERT_TRUE(ret == false);
  1297. // ksize.size() != 2
  1298. ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 4, 5}, 3);
  1299. ASSERT_TRUE(ret == false);
  1300. // size less or equal to 0
  1301. ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {0, 3}, 3);
  1302. ASSERT_TRUE(ret == false);
  1303. // sigmaX less or equal to 0
  1304. ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 3}, 0);
  1305. ASSERT_TRUE(ret == false);
  1306. }
  1307. TEST_F(MindDataImageProcess, TestCannySize3) {
  1308. std::string filename = "data/dataset/apple.jpg";
  1309. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1310. cv::Mat gray_image;
  1311. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  1312. cv::Mat dst_image;
  1313. cv::Canny(gray_image, dst_image, 100, 200, 3);
  1314. cv::Mat rgba_mat;
  1315. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1316. bool ret = false;
  1317. LiteMat lite_mat_gray;
  1318. ret =
  1319. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1320. ASSERT_TRUE(ret == true);
  1321. LiteMat lite_mat_dst;
  1322. ret = Canny(lite_mat_gray, lite_mat_dst, 100, 200, 3);
  1323. ASSERT_TRUE(ret == true);
  1324. int total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
  1325. double distance = 0.0f;
  1326. for (int i = 0; i < total_size; i++) {
  1327. distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
  1328. }
  1329. distance = sqrt(distance / total_size);
  1330. EXPECT_EQ(distance, 0.0f);
  1331. }
  1332. TEST_F(MindDataImageProcess, TestCannySize5) {
  1333. std::string filename = "data/dataset/apple.jpg";
  1334. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1335. cv::Mat gray_image;
  1336. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  1337. cv::Mat dst_image;
  1338. cv::Canny(gray_image, dst_image, 200, 300, 5);
  1339. cv::Mat rgba_mat;
  1340. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1341. bool ret = false;
  1342. LiteMat lite_mat_gray;
  1343. ret =
  1344. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1345. ASSERT_TRUE(ret == true);
  1346. LiteMat lite_mat_dst;
  1347. ret = Canny(lite_mat_gray, lite_mat_dst, 200, 300, 5);
  1348. ASSERT_TRUE(ret == true);
  1349. int total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
  1350. double distance = 0.0f;
  1351. for (int i = 0; i < total_size; i++) {
  1352. distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
  1353. }
  1354. distance = sqrt(distance / total_size);
  1355. EXPECT_EQ(distance, 0.0f);
  1356. }
  1357. TEST_F(MindDataImageProcess, TestCannyL2) {
  1358. std::string filename = "data/dataset/apple.jpg";
  1359. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1360. cv::Mat gray_image;
  1361. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  1362. cv::Mat dst_image;
  1363. cv::Canny(gray_image, dst_image, 50, 150, 3, true);
  1364. cv::Mat rgba_mat;
  1365. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1366. bool ret = false;
  1367. LiteMat lite_mat_gray;
  1368. ret =
  1369. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1370. ASSERT_TRUE(ret == true);
  1371. LiteMat lite_mat_dst;
  1372. ret = Canny(lite_mat_gray, lite_mat_dst, 50, 150, 3, true);
  1373. ASSERT_TRUE(ret == true);
  1374. int total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
  1375. double distance = 0.0f;
  1376. for (int i = 0; i < total_size; i++) {
  1377. distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
  1378. }
  1379. distance = sqrt(distance / total_size);
  1380. EXPECT_EQ(distance, 0.0f);
  1381. }
  1382. TEST_F(MindDataImageProcess, TestCannyInvalidParams) {
  1383. std::string filename = "data/dataset/apple.jpg";
  1384. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1385. cv::Mat rgba_mat;
  1386. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1387. bool ret = false;
  1388. LiteMat lite_mat_bgr;
  1389. ret =
  1390. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  1391. ASSERT_TRUE(ret == true);
  1392. // channel is not 1
  1393. LiteMat lite_mat_dst;
  1394. ret = Canny(lite_mat_bgr, lite_mat_dst, 70, 210, 3);
  1395. ASSERT_TRUE(ret == false);
  1396. LiteMat lite_mat_gray;
  1397. ret =
  1398. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1399. ASSERT_TRUE(ret == true);
  1400. // low_thresh less than 0
  1401. ret = Canny(lite_mat_gray, lite_mat_dst, -5, 230, 3);
  1402. ASSERT_TRUE(ret == false);
  1403. // high_thresh less than low_thresh
  1404. ret = Canny(lite_mat_gray, lite_mat_dst, 250, 130, 3);
  1405. ASSERT_TRUE(ret == false);
  1406. // even size
  1407. ret = Canny(lite_mat_gray, lite_mat_dst, 60, 180, 4);
  1408. ASSERT_TRUE(ret == false);
  1409. // size less than 3 or large than 7
  1410. ret = Canny(lite_mat_gray, lite_mat_dst, 10, 190, 9);
  1411. ASSERT_TRUE(ret == false);
  1412. }
  1413. TEST_F(MindDataImageProcess, TestSobel) {
  1414. std::string filename = "data/dataset/apple.jpg";
  1415. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1416. cv::Mat gray_image;
  1417. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  1418. cv::Mat sobel_image_x;
  1419. cv::Mat sobel_image_y;
  1420. cv::Sobel(gray_image, sobel_image_x, CV_32F, 1, 0, 3, 1, 0, cv::BORDER_REPLICATE);
  1421. cv::Sobel(gray_image, sobel_image_y, CV_32F, 0, 1, 3, 1, 0, cv::BORDER_REPLICATE);
  1422. cv::Mat sobel_cv_x, sobel_cv_y;
  1423. sobel_image_x.convertTo(sobel_cv_x, CV_8UC1);
  1424. sobel_image_y.convertTo(sobel_cv_y, CV_8UC1);
  1425. cv::Mat rgba_mat;
  1426. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1427. bool ret = false;
  1428. LiteMat lite_mat_gray;
  1429. ret =
  1430. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1431. ASSERT_TRUE(ret == true);
  1432. LiteMat lite_mat_x;
  1433. LiteMat lite_mat_y;
  1434. Sobel(lite_mat_gray, lite_mat_x, 1, 0, 3, PaddBorderType::PADD_BORDER_REPLICATE);
  1435. Sobel(lite_mat_gray, lite_mat_y, 0, 1, 3, PaddBorderType::PADD_BORDER_REPLICATE);
  1436. ASSERT_TRUE(ret == true);
  1437. cv::Mat dst_imageX(lite_mat_x.height_, lite_mat_x.width_, CV_32FC1, lite_mat_x.data_ptr_);
  1438. cv::Mat dst_imageY(lite_mat_y.height_, lite_mat_y.width_, CV_32FC1, lite_mat_y.data_ptr_);
  1439. cv::Mat sobel_ms_x, sobel_ms_y;
  1440. dst_imageX.convertTo(sobel_ms_x, CV_8UC1);
  1441. dst_imageY.convertTo(sobel_ms_y, CV_8UC1);
  1442. size_t total_size = lite_mat_x.height_ * lite_mat_x.width_ * lite_mat_x.channel_;
  1443. float distance_x = 0.0f, distance_y = 0.0f;
  1444. for (int i = 0; i < total_size; i++) {
  1445. distance_x += pow((uint8_t)sobel_cv_x.data[i] - (uint8_t)sobel_ms_x.data[i], 2);
  1446. distance_y += pow((uint8_t)sobel_cv_y.data[i] - (uint8_t)sobel_ms_y.data[i], 2);
  1447. }
  1448. distance_x = sqrt(distance_x / total_size);
  1449. distance_y = sqrt(distance_y / total_size);
  1450. EXPECT_EQ(distance_x, 0.0f);
  1451. EXPECT_EQ(distance_y, 0.0f);
  1452. }
  1453. TEST_F(MindDataImageProcess, TestSobelFlag) {
  1454. std::string filename = "data/dataset/apple.jpg";
  1455. cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1456. cv::Mat gray_image;
  1457. cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
  1458. cv::Mat sobel_image_x;
  1459. cv::Sobel(gray_image, sobel_image_x, CV_32F, 3, 1, 5, 1, 0, cv::BORDER_REPLICATE);
  1460. cv::Mat sobel_cv_x;
  1461. sobel_image_x.convertTo(sobel_cv_x, CV_8UC1);
  1462. cv::Mat rgba_mat;
  1463. cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
  1464. bool ret = false;
  1465. LiteMat lite_mat_gray;
  1466. ret =
  1467. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
  1468. ASSERT_TRUE(ret == true);
  1469. LiteMat lite_mat_x;
  1470. Sobel(lite_mat_gray, lite_mat_x, 3, 1, 5, PaddBorderType::PADD_BORDER_REPLICATE);
  1471. ASSERT_TRUE(ret == true);
  1472. cv::Mat dst_imageX(lite_mat_x.height_, lite_mat_x.width_, CV_32FC1, lite_mat_x.data_ptr_);
  1473. cv::Mat sobel_ms_x;
  1474. dst_imageX.convertTo(sobel_ms_x, CV_8UC1);
  1475. size_t total_size = lite_mat_x.height_ * lite_mat_x.width_ * lite_mat_x.channel_;
  1476. float distance_x = 0.0f;
  1477. for (int i = 0; i < total_size; i++) {
  1478. distance_x += pow((uint8_t)sobel_cv_x.data[i] - (uint8_t)sobel_ms_x.data[i], 2);
  1479. }
  1480. distance_x = sqrt(distance_x / total_size);
  1481. EXPECT_EQ(distance_x, 0.0f);
  1482. }
  1483. TEST_F(MindDataImageProcess, testConvertRgbToGray) {
  1484. std::string filename = "data/dataset/apple.jpg";
  1485. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1486. cv::Mat rgb_mat;
  1487. cv::Mat rgb_mat1;
  1488. cv::cvtColor(image, rgb_mat, CV_BGR2GRAY);
  1489. cv::imwrite("./opencv_image.jpg", rgb_mat);
  1490. cv::cvtColor(image, rgb_mat1, CV_BGR2RGB);
  1491. LiteMat lite_mat_rgb;
  1492. lite_mat_rgb.Init(rgb_mat1.cols, rgb_mat1.rows, rgb_mat1.channels(), rgb_mat1.data, LDataType::UINT8);
  1493. LiteMat lite_mat_gray;
  1494. bool ret = ConvertRgbToGray(lite_mat_rgb, LDataType::UINT8, image.cols, image.rows, lite_mat_gray);
  1495. ASSERT_TRUE(ret == true);
  1496. cv::Mat dst_image(lite_mat_gray.height_, lite_mat_gray.width_, CV_8UC1, lite_mat_gray.data_ptr_);
  1497. cv::imwrite("./mindspore_image.jpg", dst_image);
  1498. CompareMat(rgb_mat, lite_mat_gray);
  1499. }
  1500. TEST_F(MindDataImageProcess, testResizePreserveARWithFillerv) {
  1501. std::string filename = "data/dataset/apple.jpg";
  1502. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  1503. LiteMat lite_mat_rgb;
  1504. lite_mat_rgb.Init(image.cols, image.rows, image.channels(), image.data, LDataType::UINT8);
  1505. LiteMat lite_mat_resize;
  1506. float ratioShiftWShiftH[3] = {0};
  1507. float invM[2][3] = {{0, 0, 0}, {0, 0, 0}};
  1508. int h = 1000;
  1509. int w = 1000;
  1510. bool ret = ResizePreserveARWithFiller(lite_mat_rgb, lite_mat_resize, h, w, &ratioShiftWShiftH, &invM, 0);
  1511. ASSERT_TRUE(ret == true);
  1512. cv::Mat dst_image(lite_mat_resize.height_, lite_mat_resize.width_, CV_32FC3, lite_mat_resize.data_ptr_);
  1513. cv::imwrite("./mindspore_image.jpg", dst_image);
  1514. }