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 7.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/common.h"
  17. #include "lite_cv/lite_mat.h"
  18. #include "lite_cv/image_process.h"
  19. #include <opencv2/opencv.hpp>
  20. #include <opencv2/imgproc/types_c.h>
  21. #include "utils/log_adapter.h"
  22. 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. LiteMat Lite3CImageProcess(LiteMat &lite_mat_bgr) {
  40. bool ret;
  41. LiteMat lite_mat_resize;
  42. ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  43. if (!ret) {
  44. MS_LOG(ERROR) << "ResizeBilinear error";
  45. }
  46. LiteMat lite_mat_convert_float;
  47. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0 );
  48. if (!ret) {
  49. MS_LOG(ERROR) << "ConvertTo error";
  50. }
  51. LiteMat lite_mat_crop;
  52. ret = Crop(lite_mat_convert_float, lite_mat_crop, 16, 16, 224, 224);
  53. if (!ret) {
  54. MS_LOG(ERROR) << "Crop error";
  55. }
  56. float means[3] = {0.485, 0.456, 0.406};
  57. float vars[3] = {1.0 / 0.229, 1.0 / 0.224, 1.0 / 0.225};
  58. LiteMat lite_norm_mat_cut;
  59. SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, vars);
  60. return lite_norm_mat_cut;
  61. }
  62. cv::Mat cv3CImageProcess(cv::Mat &image) {
  63. cv::Mat resize_256_image;
  64. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  65. cv::Mat float_256_image;
  66. resize_256_image.convertTo(float_256_image, CV_32FC3);
  67. cv::Mat roi_224_image;
  68. cv::Rect roi;
  69. roi.x = 16;
  70. roi.y = 16;
  71. roi.width = 224;
  72. roi.height = 224;
  73. float_256_image(roi).copyTo(roi_224_image);
  74. float meanR = 0.485;
  75. float meanG = 0.456;
  76. float meanB = 0.406;
  77. float varR = 0.229;
  78. float varG = 0.224;
  79. float varB = 0.225;
  80. cv::Scalar mean = cv::Scalar(meanR, meanG, meanB);
  81. cv::Scalar var = cv::Scalar(varR, varG, varB);
  82. cv::Mat imgMean(roi_224_image.size(), CV_32FC3, mean);
  83. cv::Mat imgVar(roi_224_image.size(), CV_32FC3, var);
  84. cv::Mat imgR1 = roi_224_image - imgMean;
  85. cv::Mat imgR2 = imgR1 / imgVar;
  86. return imgR2;
  87. }
  88. TEST_F(MindDataImageProcess, test3C) {
  89. std::string filename = "data/dataset/apple.jpg";
  90. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  91. cv::Mat cv_image = cv3CImageProcess(image);
  92. // cv::imwrite("/home/xlei/test_3cv.jpg", cv_image);
  93. // convert to RGBA for Android bitmap(rgba)
  94. cv::Mat rgba_mat;
  95. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  96. bool ret = false;
  97. LiteMat lite_mat_bgr;
  98. ret =
  99. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  100. if (!ret) {
  101. MS_LOG(ERROR) << "Init From RGBA error";
  102. }
  103. LiteMat lite_norm_mat_cut = Lite3CImageProcess(lite_mat_bgr);
  104. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC3, lite_norm_mat_cut.data_ptr_);
  105. // cv::imwrite("/home/xlei/test_3clite.jpg", dst_image);
  106. CompareMat(cv_image, lite_norm_mat_cut);
  107. }
  108. LiteMat Lite1CImageProcess(LiteMat &lite_mat_bgr) {
  109. LiteMat lite_mat_resize;
  110. ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  111. LiteMat lite_mat_convert_float;
  112. ConvertTo(lite_mat_resize, lite_mat_convert_float);
  113. LiteMat lite_mat_cut;
  114. Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224);
  115. float means[1] = {0.485};
  116. float vars[1] = {1.0 / 0.229};
  117. LiteMat lite_norm_mat_cut;
  118. SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, vars);
  119. return lite_norm_mat_cut;
  120. }
  121. cv::Mat cv1CImageProcess(cv::Mat &image) {
  122. cv::Mat gray_image;
  123. cv::cvtColor(image, gray_image, CV_BGR2GRAY);
  124. cv::Mat resize_256_image;
  125. cv::resize(gray_image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  126. cv::Mat float_256_image;
  127. resize_256_image.convertTo(float_256_image, CV_32FC3);
  128. cv::Mat roi_224_image;
  129. cv::Rect roi;
  130. roi.x = 16;
  131. roi.y = 16;
  132. roi.width = 224;
  133. roi.height = 224;
  134. float_256_image(roi).copyTo(roi_224_image);
  135. float meanR = 0.485;
  136. float varR = 0.229;
  137. cv::Scalar mean = cv::Scalar(meanR);
  138. cv::Scalar var = cv::Scalar(varR);
  139. cv::Mat imgMean(roi_224_image.size(), CV_32FC1, mean);
  140. cv::Mat imgVar(roi_224_image.size(), CV_32FC1, var);
  141. cv::Mat imgR1 = roi_224_image - imgMean;
  142. cv::Mat imgR2 = imgR1 / imgVar;
  143. return imgR2;
  144. }
  145. TEST_F(MindDataImageProcess, test1C) {
  146. std::string filename = "data/dataset/apple.jpg";
  147. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  148. cv::Mat cv_image = cv1CImageProcess(image);
  149. // cv::imwrite("/home/xlei/test_c1v.jpg", cv_image);
  150. // convert to RGBA for Android bitmap(rgba)
  151. cv::Mat rgba_mat;
  152. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  153. LiteMat lite_mat_bgr;
  154. InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  155. LiteMat lite_norm_mat_cut = Lite1CImageProcess(lite_mat_bgr);
  156. cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC1, lite_norm_mat_cut.data_ptr_);
  157. // cv::imwrite("/home/xlei/test_c1lite.jpg", dst_image);
  158. CompareMat(cv_image, lite_norm_mat_cut);
  159. }
  160. TEST_F(MindDataImageProcess, TestPadd) {
  161. std::string filename = "data/dataset/apple.jpg";
  162. cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  163. cv::Mat resize_256_image;
  164. cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
  165. int left = 10;
  166. int right = 10;
  167. int top = 10;
  168. int bottom = 10;
  169. cv::Mat b_image;
  170. cv::Scalar color = cv::Scalar(255, 255, 255);
  171. cv::copyMakeBorder(resize_256_image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  172. // cv::imwrite("/home/xlei/test_ccc.jpg", b_image);
  173. cv::Mat rgba_mat;
  174. cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
  175. LiteMat lite_mat_bgr;
  176. InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
  177. LiteMat lite_mat_resize;
  178. ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
  179. LiteMat makeborder;
  180. Padd(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
  181. cv::Mat dst_image(256 + top + bottom, 256 + left + right, CV_8UC3, makeborder.data_ptr_);
  182. // cv::imwrite("/home/xlei/test_liteccc.jpg", dst_image);
  183. }
  184. TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {
  185. BoxesConfig config;
  186. config.img_shape = {300, 300};
  187. config.num_default = {3, 6, 6, 6, 6, 6};
  188. config.feature_size = {19, 10, 5, 3, 2, 1};
  189. config.min_scale = 0.2;
  190. config.max_scale = 0.95;
  191. config.aspect_rations = {{2}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}};
  192. config.steps = {16, 32, 64, 100, 150, 300};
  193. config.prior_scaling = {0.1, 0.2};
  194. std::vector<std::vector<float>> default_boxes = GetDefaultBoxes(config);
  195. ASSERT_TRUE(default_boxes.size() == 1917);
  196. }
  197. TEST_F(MindDataImageProcess, TestApplyNms) {
  198. std::vector<std::vector<float>> all_boxes = {{1, 1, 2, 2}, {3, 3, 4, 4}, {5, 5, 6, 6}, {5, 5, 6, 6}};
  199. std::vector<float> all_scores = {0.6, 0.5, 0.4, 0.9};
  200. std::vector<int> keep = ApplyNms(all_boxes, all_scores, 0.5, 10);
  201. ASSERT_TRUE(keep[0] == 3);
  202. ASSERT_TRUE(keep[1] == 0);
  203. ASSERT_TRUE(keep[2] == 1);
  204. }