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