test_img_prepross.cc 34.9 KB
Newer Older
H
HappyAngel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <iostream>
#include <vector>
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "paddle_api.h"               // NOLINT
#include "paddle_image_preprocess.h"  // NOLINT
#include "time.h"                     // NOLINT
typedef paddle::lite_api::Tensor Tensor;
typedef paddle::lite::utils::cv::ImageFormat ImageFormat;
typedef paddle::lite::utils::cv::FlipParam FlipParam;
typedef paddle::lite::utils::cv::TransParam TransParam;
typedef paddle::lite::utils::cv::ImagePreprocess ImagePreprocess;
typedef paddle::lite_api::DataLayoutType LayoutType;
using namespace paddle::lite_api;  // NOLINT

H
HappyAngel 已提交
31 32 33
// crop point
int flag_left_x = 50;
int flag_left_y = 50;
H
HappyAngel 已提交
34
void fill_with_mat(cv::Mat& mat, uint8_t* src, int num) {  // NOLINT
H
HappyAngel 已提交
35 36
  for (int i = 0; i < mat.rows; i++) {
    for (int j = 0; j < mat.cols; j++) {
H
HappyAngel 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      if (num == 1) {
        int tmp = (i * mat.cols + j);
      } else if (num == 2) {
        int tmp = (i * mat.cols + j) * 2;
        cv::Vec2b& rgb = mat.at<cv::Vec2b>(i, j);
        rgb[0] = src[tmp];
        rgb[1] = src[tmp + 1];
        rgb[2] = src[tmp + 2];
      } else if (num == 3) {
        int tmp = (i * mat.cols + j) * 3;
        cv::Vec3b& rgb = mat.at<cv::Vec3b>(i, j);
        rgb[0] = src[tmp];
        rgb[1] = src[tmp + 1];
        rgb[2] = src[tmp + 2];
      } else if (num == 4) {
        int tmp = (i * mat.cols + j) * 4;
        cv::Vec4b& rgb = mat.at<cv::Vec4b>(i, j);
        rgb[0] = src[tmp];
        rgb[1] = src[tmp + 1];
        rgb[2] = src[tmp + 2];
        rgb[3] = src[tmp + 3];
      } else {
        std::cout << "it is not support" << std::endl;
        return;
      }
H
HappyAngel 已提交
62 63 64 65
    }
  }
}

66
double compare_diff(uint8_t* data1, uint8_t* data2, int size, uint8_t* diff_v) {
H
HappyAngel 已提交
67 68 69
  double diff = 0.0;
  for (int i = 0; i < size; i++) {
    double val = abs(data1[i] - data2[i]);
70
    diff_v[i] = val;
H
HappyAngel 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    diff = val > diff ? val : diff;
  }
  return diff;
}
void print_data(const uint8_t* data, int size) {
  for (int i = 0; i < size; i++) {
    if ((i + 1) % 10 == 0) {
      std::cout << std::endl;
    }
  }
  std::cout << std::endl;
}
bool test_convert(bool cv_run,
                  const uint8_t* src,
                  cv::Mat img,
                  ImagePreprocess image_preprocess,
                  int in_size,
                  int out_size,
                  ImageFormat srcFormat,
                  ImageFormat dstFormat,
                  int dsth,
                  int dstw,
                  std::string dst_path,
                  int test_iter = 1) {
  // out
  uint8_t* resize_cv = new uint8_t[out_size];
  uint8_t* resize_lite = new uint8_t[out_size];
  cv::Mat im_resize;
H
HappyAngel 已提交
99

H
HappyAngel 已提交
100 101 102 103 104 105 106 107
  double to_cv = 0.0;
  double to_lite = 0.0;
  std::cout << "opencv compute:" << std::endl;
  if (cv_run) {
    for (int i = 0; i < test_iter; i++) {
      clock_t begin = clock();
      // convert bgr-gray
      if (dstFormat == srcFormat) {
H
HappyAngel 已提交
108 109
        cv::Rect rect(0, 0, dstw, dsth);
        im_resize = img(rect);
H
HappyAngel 已提交
110 111 112 113 114 115 116 117 118 119 120
      } else if ((dstFormat == ImageFormat::BGR ||
                  dstFormat == ImageFormat::RGB) &&
                 srcFormat == ImageFormat::GRAY) {
        cv::cvtColor(img, im_resize, cv::COLOR_GRAY2BGR);
      } else if ((srcFormat == ImageFormat::BGR ||
                  dstFormat == ImageFormat::RGBA) &&
                 dstFormat == ImageFormat::GRAY) {
        cv::cvtColor(img, im_resize, cv::COLOR_BGR2GRAY);
      } else if (dstFormat == srcFormat) {
        printf("convert format error \n");
        return false;
H
HappyAngel 已提交
121
      }
H
HappyAngel 已提交
122 123 124 125
      clock_t end = clock();
      to_cv += (end - begin);
    }
  }
H
HappyAngel 已提交
126

H
HappyAngel 已提交
127 128 129 130 131 132 133 134 135 136
  std::cout << "lite compute:" << std::endl;
  for (int i = 0; i < test_iter; i++) {
    clock_t begin = clock();
    // resize default linear
    image_preprocess.imageConvert(src, resize_lite);
    clock_t end = clock();
    to_lite += (end - begin);
  }
  to_cv = 1000 * to_cv / CLOCKS_PER_SEC;
  to_lite = 1000 * to_lite / CLOCKS_PER_SEC;
H
HappyAngel 已提交
137

H
HappyAngel 已提交
138 139 140 141 142 143 144 145
  std::cout << "---opencv convert run time: " << to_cv
            << "ms, avg: " << to_cv / test_iter << std::endl;
  std::cout << "---lite convert run time: " << to_lite
            << "ms, avg: " << to_lite / test_iter << std::endl;
  std::cout << "compare diff: " << std::endl;

  if (cv_run) {
    resize_cv = im_resize.data;
146 147
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
H
HappyAngel 已提交
148 149 150 151 152 153 154
    if (diff > 1) {
      std::cout << "din: " << std::endl;
      print_data(src, in_size);
      std::cout << "cv out: " << std::endl;
      print_data(resize_cv, out_size);
      std::cout << "lite out: " << std::endl;
      print_data(resize_lite, out_size);
155 156
      std::cout << "lite out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
157 158 159
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
      return false;
    } else {
      // save_img
      std::cout << "write image: " << std::endl;
      std::string resize_name = dst_path + "/convert.jpg";
      cv::Mat resize_mat;
      int num = 1;
      if (dstFormat == ImageFormat::BGR || dstFormat == ImageFormat::RGB) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC3);
        num = 3;
      } else if (dstFormat == ImageFormat::BGRA ||
                 dstFormat == ImageFormat::RGBA) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC4);
        num = 4;
      } else if (dstFormat == ImageFormat::GRAY) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC1);
        num = 1;
      } else if (dstFormat == ImageFormat::NV12) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC2);
        num = 2;
H
HappyAngel 已提交
180
      }
H
HappyAngel 已提交
181 182 183 184
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);

      std::cout << "convert successed!" << std::endl;
H
HappyAngel 已提交
185 186 187
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
188 189 190
      return true;
    }
  }
H
HappyAngel 已提交
191 192 193
  delete[] resize_cv;
  delete[] resize_lite;
  return false;
H
HappyAngel 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

bool test_flip(bool cv_run,
               const uint8_t* src,
               cv::Mat img,
               ImagePreprocess image_preprocess,
               int in_size,
               int out_size,
               FlipParam flip,
               ImageFormat dstFormat,
               int dsth,
               int dstw,
               std::string dst_path,
               int test_iter = 1) {
  // out
  uint8_t* resize_cv = new uint8_t[out_size];
  uint8_t* resize_lite = new uint8_t[out_size];
  cv::Mat im_resize;
H
HappyAngel 已提交
212

H
HappyAngel 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  double to_cv = 0.0;
  double to_lite = 0.0;
  std::cout << "opencv compute:" << std::endl;
  if (cv_run) {
    for (int i = 0; i < test_iter; i++) {
      clock_t begin = clock();
      // resize default linear
      cv::flip(img, im_resize, flip);
      clock_t end = clock();
      to_cv += (end - begin);
    }
  }
  std::cout << "lite compute:" << std::endl;
  for (int i = 0; i < test_iter; i++) {
    clock_t begin = clock();
    // resize default linear
    image_preprocess.imageFlip(src, resize_lite);
    clock_t end = clock();
    to_lite += (end - begin);
  }
  to_cv = 1000 * to_cv / CLOCKS_PER_SEC;
  to_lite = 1000 * to_lite / CLOCKS_PER_SEC;

  std::cout << "---opencv flip run time: " << to_cv
            << "ms, avg: " << to_cv / test_iter << std::endl;
  std::cout << "---lite flip run time: " << to_lite
            << "ms, avg: " << to_lite / test_iter << std::endl;
  std::cout << "compare diff: " << std::endl;

  if (cv_run) {
    resize_cv = im_resize.data;
244 245
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
H
HappyAngel 已提交
246 247 248 249 250 251 252
    if (diff > 1) {
      std::cout << "din: " << std::endl;
      print_data(src, in_size);
      std::cout << "cv out: " << std::endl;
      print_data(resize_cv, out_size);
      std::cout << "lite out: " << std::endl;
      print_data(resize_lite, out_size);
253 254
      std::cout << "diff out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
255 256 257
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
      return false;
    } else {
      // save_img
      std::cout << "write image: " << std::endl;
      std::string resize_name = dst_path + "/flip.jpg";
      cv::Mat resize_mat;
      int num = 1;
      if (dstFormat == ImageFormat::BGR || dstFormat == ImageFormat::RGB) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC3);
        num = 3;
      } else if (dstFormat == ImageFormat::BGRA ||
                 dstFormat == ImageFormat::RGBA) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC4);
        num = 4;
      } else if (dstFormat == ImageFormat::GRAY) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC1);
        num = 1;
      } else if (dstFormat == ImageFormat::NV12) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC2);
        num = 2;
H
HappyAngel 已提交
278
      }
H
HappyAngel 已提交
279 280 281
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);
      std::cout << "flip successed!" << std::endl;
H
HappyAngel 已提交
282 283 284
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
285 286 287
      return true;
    }
  }
H
HappyAngel 已提交
288 289 290
  delete[] resize_cv;
  delete[] resize_lite;
  return false;
H
HappyAngel 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
}

bool test_rotate(bool cv_run,
                 const uint8_t* src,
                 cv::Mat img,
                 ImagePreprocess image_preprocess,
                 int in_size,
                 int out_size,
                 float rotate,
                 ImageFormat dstFormat,
                 int dsth,
                 int dstw,
                 std::string dst_path,
                 int test_iter = 1) {
  // out
  uint8_t* resize_cv = new uint8_t[out_size];
  uint8_t* resize_lite = new uint8_t[out_size];
  cv::Mat im_resize;

  double to_cv = 0.0;
  double to_lite = 0.0;
  std::cout << "opencv compute:" << std::endl;
  if (cv_run) {
    for (int i = 0; i < test_iter; i++) {
      clock_t begin = clock();
      // rotate 90
      if (rotate == 90) {
        cv::flip(img.t(), im_resize, 1);
      } else if (rotate == 180) {
        cv::flip(img, im_resize, -1);
      } else if (rotate == 270) {
        cv::flip(img.t(), im_resize, 0);
H
HappyAngel 已提交
323
      }
H
HappyAngel 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
      clock_t end = clock();
      to_cv += (end - begin);
    }
  }
  // lite
  std::cout << "lite compute:" << std::endl;
  for (int i = 0; i < test_iter; i++) {
    clock_t begin = clock();
    // resize default linear
    image_preprocess.imageRotate(src, resize_lite);
    clock_t end = clock();
    to_lite += (end - begin);
  }
  to_cv = 1000 * to_cv / CLOCKS_PER_SEC;
  to_lite = 1000 * to_lite / CLOCKS_PER_SEC;
H
HappyAngel 已提交
339

H
HappyAngel 已提交
340 341 342 343 344 345 346
  std::cout << "---opencv rotate run time: " << to_cv
            << "ms, avg: " << to_cv / test_iter << std::endl;
  std::cout << "---lite rotate run time: " << to_lite
            << "ms, avg: " << to_lite / test_iter << std::endl;
  std::cout << "compare diff: " << std::endl;
  if (cv_run) {
    resize_cv = im_resize.data;
347 348
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
H
HappyAngel 已提交
349 350 351 352 353 354 355
    if (diff > 1) {
      std::cout << "din: " << std::endl;
      print_data(src, in_size);
      std::cout << "cv out: " << std::endl;
      print_data(resize_cv, out_size);
      std::cout << "lite out: " << std::endl;
      print_data(resize_lite, out_size);
356 357
      std::cout << "diff out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
358 359 360
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
361 362 363 364 365 366 367
      return false;
    } else {
      // save_img
      std::cout << "write image: " << std::endl;
      std::string resize_name = dst_path + "/rotate.jpg";
      cv::Mat resize_mat;
      int num = 1;
H
HappyAngel 已提交
368
      if (dstFormat == ImageFormat::BGR || dstFormat == ImageFormat::RGB) {
H
HappyAngel 已提交
369 370 371 372 373 374
        resize_mat = cv::Mat(dsth, dstw, CV_8UC3);
        num = 3;
      } else if (dstFormat == ImageFormat::BGRA ||
                 dstFormat == ImageFormat::RGBA) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC4);
        num = 4;
H
HappyAngel 已提交
375
      } else if (dstFormat == ImageFormat::GRAY) {
H
HappyAngel 已提交
376 377 378 379 380
        resize_mat = cv::Mat(dsth, dstw, CV_8UC1);
        num = 1;
      } else if (dstFormat == ImageFormat::NV12) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC2);
        num = 2;
H
HappyAngel 已提交
381
      }
H
HappyAngel 已提交
382 383 384
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);
      std::cout << "rotate successed!" << std::endl;
H
HappyAngel 已提交
385 386 387
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
388 389 390
      return true;
    }
  }
H
HappyAngel 已提交
391 392 393
  delete[] resize_cv;
  delete[] resize_lite;
  return false;
H
HappyAngel 已提交
394
}
H
HappyAngel 已提交
395

H
HappyAngel 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
bool test_resize(bool cv_run,
                 const uint8_t* src,
                 cv::Mat img,
                 ImagePreprocess image_preprocess,
                 int in_size,
                 int out_size,
                 ImageFormat dstFormat,
                 int dsth,
                 int dstw,
                 std::string dst_path,
                 int test_iter = 1) {
  // out
  uint8_t* resize_cv = new uint8_t[out_size];
  uint8_t* resize_lite = new uint8_t[out_size];
  cv::Mat im_resize;
H
HappyAngel 已提交
411

H
HappyAngel 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  double to_cv = 0.0;
  double to_lite = 0.0;
  std::cout << "opencv compute:" << std::endl;
  if (cv_run) {
    for (int i = 0; i < test_iter; i++) {
      clock_t begin = clock();
      // resize default linear
      cv::resize(img, im_resize, cv::Size(dstw, dsth), 0.f, 0.f);
      clock_t end = clock();
      to_cv += (end - begin);
    }
  }
  // param
  std::cout << "lite compute:" << std::endl;
  for (int i = 0; i < test_iter; i++) {
    clock_t begin = clock();
    // resize default linear
    image_preprocess.imageResize(src, resize_lite);
    clock_t end = clock();
    to_lite += (end - begin);
  }
  to_cv = 1000 * to_cv / CLOCKS_PER_SEC;
  to_lite = 1000 * to_lite / CLOCKS_PER_SEC;
H
HappyAngel 已提交
435

H
HappyAngel 已提交
436 437 438 439 440
  std::cout << "---opencv resize run time: " << to_cv
            << "ms, avg: " << to_cv / test_iter << std::endl;
  std::cout << "---lite resize run time: " << to_lite
            << "ms, avg: " << to_lite / test_iter << std::endl;
  std::cout << "compare diff: " << std::endl;
H
HappyAngel 已提交
441

H
HappyAngel 已提交
442 443
  if (cv_run) {
    resize_cv = im_resize.data;
444 445 446
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
    if (diff > 10) {
H
HappyAngel 已提交
447 448 449 450 451 452
      std::cout << "din: " << std::endl;
      print_data(src, in_size);
      std::cout << "cv out: " << std::endl;
      print_data(resize_cv, out_size);
      std::cout << "lite out: " << std::endl;
      print_data(resize_lite, out_size);
453 454
      std::cout << "diff out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
455 456 457
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
      return false;
    } else {
      // save_img
      std::cout << "write image: " << std::endl;
      std::string resize_name = dst_path + "/resize.jpg";
      cv::Mat resize_mat;
      int num = 1;
      if (dstFormat == ImageFormat::BGR || dstFormat == ImageFormat::RGB) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC3);
        num = 3;
      } else if (dstFormat == ImageFormat::BGRA ||
                 dstFormat == ImageFormat::RGBA) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC4);
        num = 4;
      } else if (dstFormat == ImageFormat::GRAY) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC1);
        num = 1;
      } else if (dstFormat == ImageFormat::NV12) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC2);
        num = 2;
H
HappyAngel 已提交
478
      }
H
HappyAngel 已提交
479 480 481
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);
      std::cout << "resize successed!" << std::endl;
H
HappyAngel 已提交
482 483 484
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
H
HappyAngel 已提交
485 486 487
      return true;
    }
  }
H
HappyAngel 已提交
488 489 490
  delete[] resize_cv;
  delete[] resize_lite;
  return false;
H
HappyAngel 已提交
491 492
}

H
HappyAngel 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
bool test_crop(bool cv_run,
               const uint8_t* src,
               cv::Mat img,
               ImagePreprocess image_preprocess,
               int in_size,
               int out_size,
               ImageFormat dstFormat,
               int left_x,
               int left_y,
               int dstw,
               int dsth,
               std::string dst_path,
               int test_iter = 1) {
  uint8_t* resize_cv = new uint8_t[out_size];
  uint8_t* resize_lite = new uint8_t[out_size];

  cv::Mat im_resize;

  double to_cv = 0.0;
  double to_lite = 0.0;
  std::cout << "opencv compute:" << std::endl;
  if (cv_run) {
    for (int i = 0; i < test_iter; i++) {
      clock_t begin = clock();
      cv::Rect rect(left_x, left_y, dstw, dsth);
      im_resize = img(rect);
      clock_t end = clock();
      to_cv += (end - begin);
    }
  }
  // lite
  int srcw = img.cols;
  int srch = img.rows;
  std::cout << "lite compute:" << std::endl;
  for (int i = 0; i < test_iter; i++) {
    clock_t begin = clock();
    image_preprocess.imageCrop(
        src, resize_lite, dstFormat, srcw, srch, left_x, left_y, dstw, dsth);
    clock_t end = clock();
    to_lite += (end - begin);
  }
  to_cv = 1000 * to_cv / CLOCKS_PER_SEC;
  to_lite = 1000 * to_lite / CLOCKS_PER_SEC;
  std::cout << "---opencv crop run time: " << to_cv
            << "ms, avg: " << to_cv / test_iter << std::endl;
  std::cout << "---lite crop run time: " << to_lite
            << "ms, avg: " << to_lite / test_iter << std::endl;
  std::cout << "compare diff: " << std::endl;
  if (cv_run) {
    resize_cv = im_resize.data;
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
    diff = 0;
    if (diff > 1) {
      std::cout << "din: " << std::endl;
      print_data(src, in_size);
      std::cout << "cv out: " << std::endl;
      print_data(resize_cv, out_size);
      std::cout << "lite out: " << std::endl;
      print_data(resize_lite, out_size);
      std::cout << "diff out: " << std::endl;
      print_data(diff_v, out_size);
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
      return false;
    } else {
      // save_img
      std::cout << "write image: " << std::endl;
      std::string resize_name = dst_path + "/crop.jpg";
      cv::Mat resize_mat;
      int num = 1;
      if (dstFormat == ImageFormat::BGR || dstFormat == ImageFormat::RGB) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC3);
        num = 3;
      } else if (dstFormat == ImageFormat::BGRA ||
                 dstFormat == ImageFormat::RGBA) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC4);
        num = 4;
      } else if (dstFormat == ImageFormat::GRAY) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC1);
        num = 1;
      } else if (dstFormat == ImageFormat::NV12) {
        resize_mat = cv::Mat(dsth, dstw, CV_8UC2);
        num = 2;
      }
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);
      std::cout << "crop successed!" << std::endl;
      delete[] diff_v;
      delete[] resize_cv;
      delete[] resize_lite;
      return true;
    }
  }
  delete[] resize_cv;
  delete[] resize_lite;
  return false;
}
H
HappyAngel 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
void test_custom(bool has_img,  // input is image
                 std::string img_path,
                 std::string in_txt,
                 std::string dst_path,
                 ImageFormat srcFormat,
                 ImageFormat dstFormat,
                 int srcw,
                 int srch,
                 int dstw,
                 int dsth,
                 float rotate,
                 FlipParam flip,
                 int test_iter = 1) {
  // RGBA = 0, BGRA, RGB, BGR, GRAY, NV21 = 11, NV12,
  cv::Mat img;
  uint8_t* src = nullptr;
  int in_size = 0;
  if (has_img) {
    if (srcFormat == ImageFormat::BGR || srcFormat == ImageFormat::RGB) {
      img = imread(img_path, cv::IMREAD_COLOR);
    } else if (srcFormat == ImageFormat::GRAY) {
      img = imread(img_path, cv::IMREAD_GRAYSCALE);
    } else {
      printf("this format %d does not support \n", srcFormat);
      return;
    }
    srcw = img.cols;
    srch = img.rows;
    src = img.data;
  }
  bool cv_run = true;
  if (srcFormat == ImageFormat::GRAY) {
    std::cout << "srcFormat: GRAY" << std::endl;
    cv_run = false;
  } else if (srcFormat == ImageFormat::BGR || srcFormat == ImageFormat::RGB) {
    in_size = 3 * srch * srcw;
    std::cout << "srcFormat: BGR/RGB" << std::endl;
  } else if (srcFormat == ImageFormat::RGBA || srcFormat == ImageFormat::BGRA) {
    in_size = 4 * srch * srcw;
    std::cout << "srcFormat: BGRA/RGBA" << std::endl;
  } else if (srcFormat == ImageFormat::NV12 || srcFormat == ImageFormat::NV21) {
    in_size = (3 * srch * srcw) / 2;
    cv_run = false;
    std::cout << "srcFormat: NV12/NV12" << std::endl;
  }
  int out_size = dstw * dsth;
  // out
  if (dstFormat == ImageFormat::GRAY) {
    std::cout << "dstFormat: GRAY" << std::endl;
  } else if (dstFormat == ImageFormat::BGR || dstFormat == ImageFormat::RGB) {
    out_size = 3 * dsth * dstw;
    std::cout << "dstFormat: BGR/RGB" << std::endl;
  } else if (dstFormat == ImageFormat::RGBA || dstFormat == ImageFormat::BGRA) {
    out_size = 4 * dsth * dstw;
    std::cout << "dstFormat: BGRA/RGBA" << std::endl;
  } else if (dstFormat == ImageFormat::NV12 || dstFormat == ImageFormat::NV21) {
    out_size = (3 * dsth * dstw) / 2;
    cv_run = false;
    std::cout << "dstFormat: NV12/NV12" << std::endl;
  }
H
HappyAngel 已提交
652

H
HappyAngel 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  if (!has_img) {
    src = new uint8_t[in_size];
    // read txt
    FILE* fp = fopen(in_txt.c_str(), "r");
    for (int i = 0; i < in_size; i++) {
      fscanf(fp, "%d\n", &src[i]);
    }
    fclose(fp);
    int num = 1;
    if (srcFormat == ImageFormat::GRAY) {
      img = cv::Mat(srch, srcw, CV_8UC1);
    } else if (srcFormat == ImageFormat::BGR || srcFormat == ImageFormat::RGB) {
      img = cv::Mat(srch, srcw, CV_8UC3);
      num = 3;
    } else if (srcFormat == ImageFormat::BGRA ||
               srcFormat == ImageFormat::RGBA) {
      img = cv::Mat(srch, srcw, CV_8UC4);
      num = 4;
    } else if (srcFormat == ImageFormat::NV12 ||
               srcFormat == ImageFormat::NV21) {
      img = cv::Mat(srch, srcw, CV_8UC2);
      num = 2;
      std::cout << "CV not support NV12";
    }
    fill_with_mat(img, src, num);
    std::string name = dst_path + "input.jpg";
    cv::imwrite(name, img);  // shurutup
  }
H
HappyAngel 已提交
681

H
HappyAngel 已提交
682 683 684 685 686 687 688
  TransParam tparam;
  tparam.ih = srch;
  tparam.iw = srcw;
  tparam.oh = srch;
  tparam.ow = srcw;
  tparam.flip_param = flip;
  tparam.rotate_param = rotate;
H
HappyAngel 已提交
689

H
HappyAngel 已提交
690 691 692 693 694 695 696
  TransParam tparam1;
  tparam1.ih = srch;
  tparam1.iw = srcw;
  tparam1.oh = dsth;
  tparam1.ow = dstw;
  tparam1.flip_param = flip;
  tparam1.rotate_param = rotate;
H
HappyAngel 已提交
697

H
HappyAngel 已提交
698
  ImagePreprocess image_preprocess(srcFormat, dstFormat, tparam);
H
HappyAngel 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
  std::cout << "cv_run: " << cv_run << std::endl;
  std::cout << "image crop testing" << std::endl;
  bool res = test_crop(cv_run,
                       src,
                       img,
                       image_preprocess,
                       in_size,
                       out_size,
                       dstFormat,
                       flag_left_x,
                       flag_left_y,
                       dstw,
                       dsth,
                       dst_path,
                       test_iter);
  if (!res) {
    return;
  }
717
  std::cout << "image convert testing" << std::endl;
H
HappyAngel 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
  bool re = test_convert(cv_run,
                         src,
                         img,
                         image_preprocess,
                         in_size,
                         out_size,
                         srcFormat,
                         dstFormat,
                         srch,
                         srcw,
                         dst_path,
                         test_iter);
  if (!re) {
    return;
  }
733
  std::cout << "image resize testing" << std::endl;
H
HappyAngel 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
  tparam.oh = dsth;
  tparam.ow = dstw;
  ImagePreprocess image_preprocess1(srcFormat, srcFormat, tparam1);
  re = test_resize(cv_run,
                   src,
                   img,
                   image_preprocess1,
                   in_size,
                   out_size,
                   srcFormat,
                   dsth,
                   dstw,
                   dst_path,
                   test_iter);
  if (!re) {
    return;
  }
H
HappyAngel 已提交
751

752
  std::cout << "image rotate testing" << std::endl;
H
HappyAngel 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
  if (rotate == 90 || rotate == 270) {
    tparam.oh = srcw;
    tparam.ow = srch;
    dsth = srcw;
    dstw = srch;
  } else {
    tparam.oh = srch;
    tparam.ow = srcw;
    dsth = srch;
    dstw = srcw;
  }
  ImagePreprocess image_preprocess2(srcFormat, srcFormat, tparam);
  re = test_rotate(cv_run,
                   src,
                   img,
                   image_preprocess2,
                   in_size,
                   out_size,
                   rotate,
                   srcFormat,
                   dsth,
                   dstw,
                   dst_path,
                   test_iter);
  if (!re) {
    return;
  }
  tparam.oh = srch;
  tparam.ow = srcw;
  ImagePreprocess image_preprocess3(srcFormat, srcFormat, tparam);
783
  std::cout << "image flip testing" << std::endl;
H
HappyAngel 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  re = test_flip(cv_run,
                 src,
                 img,
                 image_preprocess3,
                 in_size,
                 out_size,
                 flip,
                 srcFormat,
                 srch,
                 srcw,
                 dst_path,
                 test_iter);
  if (!re) {
    return;
  }
}
H
HappyAngel 已提交
800

H
HappyAngel 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
#if 0
void test_all_r(std::string dst_path, int test_iter = 1) {
  // RGBA = 0, BGRA, RGB, BGR, GRAY, NV21 = 11, NV12,
  cv::Mat img;
  uint8_t* src = nullptr;
  int in_size = 0;
  for (auto& srcFormat : {1, 3, 4, 11}) {
    for (auto& dstFormat : {1, 3, 4, 11}) {
      for (auto& srcw : {10, 112, 200}) {
        for (auto& srch : {10, 224, 400}) {
          for (auto& dstw : {12, 224, 180}) {
            for (auto& dsth : {12, 224, 320}) {
              for (auto& flip : {-1, 0, 1}) {
                for (auto& rotate : {90, 180, 270}) {
                  TransParam tparam;
                  tparam.ih = srch;
                  tparam.iw = srcw;
                  tparam.oh = srch;
                  tparam.ow = srcw;
                  tparam.flip_param = (FlipParam)flip;
                  tparam.rotate_param = rotate;
H
HappyAngel 已提交
822

H
HappyAngel 已提交
823 824 825 826 827 828 829
                  TransParam tparam1;
                  tparam1.ih = srch;
                  tparam1.iw = srcw;
                  tparam1.oh = dsth;
                  tparam1.ow = dstw;
                  tparam1.flip_param = (FlipParam)flip;
                  tparam.rotate_param = rotate;
H
HappyAngel 已提交
830

H
HappyAngel 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
                  ImagePreprocess image_preprocess(
                      (ImageFormat)srcFormat, (ImageFormat)dstFormat, tparam);
                  ImagePreprocess image_preprocess1(
                      (ImageFormat)srcFormat, (ImageFormat)srcFormat, tparam1);
                  ImagePreprocess image_preprocess2(
                      (ImageFormat)srcFormat, (ImageFormat)srcFormat, tparam);
                  int h = srch;
                  int w = srcw;
                  if (rotate == 90 || rotate == 270) {
                    tparam.oh = srcw;
                    h = srcw;
                    tparam.ow = srch;
                    w = srch;
                  }
                  ImagePreprocess image_preprocess3(
                      (ImageFormat)srcFormat, (ImageFormat)srcFormat, tparam);
                  int in_size = srcw * srch;
                  int out_size = dstw * dsth;
                  if (srcFormat == ImageFormat::GRAY) {
                    std::cout << "srcFormat: GRAY" << std::endl;
                  } else if (srcFormat == ImageFormat::BGR ||
                             srcFormat == ImageFormat::RGB) {
                    in_size = 3 * srch * srcw;
                    std::cout << "srcFormat: BGR/RGB" << std::endl;
                  } else if (srcFormat == ImageFormat::RGBA ||
                             srcFormat == ImageFormat::BGRA) {
                    in_size = 4 * srch * srcw;
                    std::cout << "srcFormat: BGRA/RGBA" << std::endl;
                  } else if (srcFormat == ImageFormat::NV12 ||
                             srcFormat == ImageFormat::NV21) {
                    in_size = (3 * srch * srcw) / 2;
                    std::cout << "srcFormat: NV12/NV12" << std::endl;
                  }
                  // out
                  if (dstFormat == ImageFormat::GRAY) {
                    std::cout << "dstFormat: GRAY" << std::endl;
                  } else if (dstFormat == ImageFormat::BGR ||
                             dstFormat == ImageFormat::RGB) {
                    out_size = 3 * dsth * dstw;
                    std::cout << "dstFormat: BGR/RGB" << std::endl;
                  } else if (dstFormat == ImageFormat::RGBA ||
                             dstFormat == ImageFormat::BGRA) {
                    out_size = 4 * dsth * dstw;
                    std::cout << "dstFormat: BGRA/RGBA" << std::endl;
                  } else if (dstFormat == ImageFormat::NV12 ||
                             dstFormat == ImageFormat::NV21) {
                    out_size = (3 * dsth * dstw) / 2;
                    std::cout << "dstFormat: NV12/NV12" << std::endl;
                  }
                  // init
                  uint8_t* src = new uint8_t[in_size];
                  for (int i = 0; i < in_size; i++) {
                    src[i] = i % 255;
                  }
                  cv::Mat img;
                  int num = 1;
                  bool cv_run = true;
                  if (srcFormat == ImageFormat::GRAY) {
                    img = cv::Mat(srch, srcw, CV_8UC1);
                    cv_run = false;
                  } else if (srcFormat == ImageFormat::BGR ||
                             srcFormat == ImageFormat::RGB) {
                    img = cv::Mat(srch, srcw, CV_8UC3);
                    num = 3;
                  } else if (srcFormat == ImageFormat::BGRA ||
                             srcFormat == ImageFormat::RGBA) {
                    img = cv::Mat(srch, srcw, CV_8UC4);
                    num = 4;
                  } else if (srcFormat == ImageFormat::NV12 ||
                             srcFormat == ImageFormat::NV21) {
                    img = cv::Mat(srch, srcw, CV_8UC2);
                    num = 2;
                    cv_run = false;
                  }
                  fill_with_mat(img, src, num);
                  std::string name = dst_path + "input.jpg";
                  cv::imwrite(name, img);  // shurutup
                  // convert
                  bool convert = true;
                  if (srcFormat == 11 || dstFormat == 11) {
                    // NV12, cv not support
                    convert = false;
                    cv_run = false;
                  }
                  if (convert) {
                    std::cout << "image convert testing";
                    bool re = test_convert(cv_run,
                                           src,
                                           img,
                                           image_preprocess,
                                           in_size,
                                           out_size,
                                           (ImageFormat)srcFormat,
                                           (ImageFormat)dstFormat,
                                           srch,
                                           srcw,
                                           dst_path,
                                           test_iter);
                    if (!re) {
                      return;
                    }
                  }
H
HappyAngel 已提交
933

H
HappyAngel 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
                  // resize
                  std::cout << "image resize testing";
                  bool re = test_resize(cv_run,
                                        src,
                                        img,
                                        image_preprocess1,
                                        in_size,
                                        out_size,
                                        (ImageFormat)srcFormat,
                                        dsth,
                                        dstw,
                                        dst_path,
                                        test_iter);
                  if (convert && !re) {
                    return;
                  }
                  // rotate
                  std::cout << "image rotate testing";
H
HappyAngel 已提交
952

H
HappyAngel 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
                  re = test_rotate(cv_run,
                                   src,
                                   img,
                                   image_preprocess3,
                                   in_size,
                                   out_size,
                                   rotate,
                                   (ImageFormat)srcFormat,
                                   h,
                                   w,
                                   dst_path,
                                   test_iter);
                  if (convert && !re) {
                    return;
                  }
                  // flip
                  std::cout << "image rotate testing";
                  re = test_flip(cv_run,
                                 src,
                                 img,
                                 image_preprocess2,
                                 in_size,
                                 out_size,
                                 (FlipParam)flip,
                                 (ImageFormat)srcFormat,
                                 srch,
                                 srcw,
                                 dst_path,
                                 test_iter);
                  if (convert && !re) {
                    return;
                  }
                }
              }
            }
          }
        }
H
HappyAngel 已提交
990 991 992 993
      }
    }
  }
}
H
HappyAngel 已提交
994
#endif
H
HappyAngel 已提交
995 996 997 998

int main(int argc, char** argv) {
  if (argc < 7) {
    std::cerr << "[ERROR] usage: " << argv[0]
H
HappyAngel 已提交
999 1000 1001
              << " has_img image_path/txt_path dst_apth srcFormat dstFormat "
                 "dstw dsth "
              << "[options] srcw srch flip rotate test_iter\n ";
H
HappyAngel 已提交
1002 1003
    exit(1);
  }
H
HappyAngel 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012
  bool has_img = atoi(argv[1]);
  std::string path = argv[2];
  std::string dst_path = argv[3];
  int srcFormat = atoi(argv[4]);
  int dstFormat = atoi(argv[5]);
  int dstw = atoi(argv[6]);
  int dsth = atoi(argv[7]);
  int srcw = 100;
  int srch = 100;
H
HappyAngel 已提交
1013 1014
  int flip = -1;
  float rotate = 90;
H
HappyAngel 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  int test_iter = 10;
  if (!has_img) {
    std::cout << "It needs srcw and srch";
    srcw = atoi(argv[8]);
    srch = atoi(argv[9]);
    if (argc > 10) {
      flip = atoi(argv[10]);
    }
    if (argc > 11) {
      rotate = atoi(argv[11]);
    }
    if (argc > 12) {
      test_iter = atoi(argv[12]);
    }
  } else {
    if (argc > 8) {
      flip = atoi(argv[8]);
    }
    if (argc > 9) {
      rotate = atoi(argv[9]);
    }
    if (argc > 10) {
H
HappyAngel 已提交
1037 1038 1039 1040 1041
      flag_left_x = atoi(argv[10]);
      flag_left_y = atoi(argv[11]);
    }
    if (argc > 12) {
      test_iter = atoi(argv[12]);
H
HappyAngel 已提交
1042
    }
H
HappyAngel 已提交
1043
  }
H
HappyAngel 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
  test_custom(has_img,
              path,
              path,
              dst_path,
              (ImageFormat)srcFormat,
              (ImageFormat)dstFormat,
              srcw,
              srch,
              dstw,
              dsth,
              rotate,
              (FlipParam)flip,
              test_iter);
#if 0
  test_all_r(dst_path, test_iter);
#endif
H
HappyAngel 已提交
1060 1061
  return 0;
}