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