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