test_img_prepross.cc 30.2 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
void fill_with_mat(cv::Mat& mat, uint8_t* src, int num) {  // NOLINT
H
HappyAngel 已提交
32 33
  for (int i = 0; i < mat.rows; i++) {
    for (int j = 0; j < mat.cols; j++) {
H
HappyAngel 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      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 已提交
59 60 61 62
    }
  }
}

63
double compare_diff(uint8_t* data1, uint8_t* data2, int size, uint8_t* diff_v) {
H
HappyAngel 已提交
64 65 66
  double diff = 0.0;
  for (int i = 0; i < size; i++) {
    double val = abs(data1[i] - data2[i]);
67
    diff_v[i] = val;
H
HappyAngel 已提交
68 69 70 71 72 73
    diff = val > diff ? val : diff;
  }
  return diff;
}
void print_data(const uint8_t* data, int size) {
  for (int i = 0; i < size; i++) {
74
    printf("%d ", data[i]);
H
HappyAngel 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    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 已提交
97

H
HappyAngel 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  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) {
        im_resize = img;
      } 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 已提交
118
      }
H
HappyAngel 已提交
119 120 121 122
      clock_t end = clock();
      to_cv += (end - begin);
    }
  }
H
HappyAngel 已提交
123

H
HappyAngel 已提交
124 125 126 127 128 129 130 131 132 133
  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 已提交
134

H
HappyAngel 已提交
135 136 137 138 139 140 141 142
  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;
143 144
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
H
HappyAngel 已提交
145 146 147 148 149 150 151
    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);
152 153
      std::cout << "lite out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      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 已提交
174
      }
H
HappyAngel 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);

      std::cout << "convert successed!" << std::endl;
      return true;
    }
  }
}

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 已提交
200

H
HappyAngel 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  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;
232 233
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
H
HappyAngel 已提交
234 235 236 237 238 239 240
    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);
241 242
      std::cout << "diff out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
      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 已提交
263
      }
H
HappyAngel 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);
      std::cout << "flip successed!" << std::endl;
      return true;
    }
  }
}

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 已提交
302
      }
H
HappyAngel 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      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 已提交
318

H
HappyAngel 已提交
319 320 321 322 323 324 325
  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;
326 327
    uint8_t* diff_v = new uint8_t[out_size];
    double diff = compare_diff(resize_cv, resize_lite, out_size, diff_v);
H
HappyAngel 已提交
328 329 330 331 332 333 334
    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);
335 336
      std::cout << "diff out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
337 338 339 340 341 342 343
      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 已提交
344
      if (dstFormat == ImageFormat::BGR || dstFormat == ImageFormat::RGB) {
H
HappyAngel 已提交
345 346 347 348 349 350
        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 已提交
351
      } else if (dstFormat == ImageFormat::GRAY) {
H
HappyAngel 已提交
352 353 354 355 356
        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 已提交
357
      }
H
HappyAngel 已提交
358 359 360 361 362 363 364
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);
      std::cout << "rotate successed!" << std::endl;
      return true;
    }
  }
}
H
HappyAngel 已提交
365

H
HappyAngel 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
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 已提交
381

H
HappyAngel 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  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 已提交
405

H
HappyAngel 已提交
406 407 408 409 410
  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 已提交
411

H
HappyAngel 已提交
412 413
  if (cv_run) {
    resize_cv = im_resize.data;
414 415 416
    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 已提交
417 418 419 420 421 422
      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);
423 424
      std::cout << "diff out: " << std::endl;
      print_data(diff_v, out_size);
H
HappyAngel 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
      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 已提交
445
      }
H
HappyAngel 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
      fill_with_mat(resize_mat, resize_lite, num);
      cv::imwrite(resize_name, resize_mat);
      std::cout << "resize successed!" << std::endl;
      return true;
    }
  }
}

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 已提交
514

H
HappyAngel 已提交
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
  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 已提交
543

H
HappyAngel 已提交
544 545 546 547 548 549 550
  TransParam tparam;
  tparam.ih = srch;
  tparam.iw = srcw;
  tparam.oh = srch;
  tparam.ow = srcw;
  tparam.flip_param = flip;
  tparam.rotate_param = rotate;
H
HappyAngel 已提交
551

H
HappyAngel 已提交
552 553 554 555 556 557 558
  TransParam tparam1;
  tparam1.ih = srch;
  tparam1.iw = srcw;
  tparam1.oh = dsth;
  tparam1.ow = dstw;
  tparam1.flip_param = flip;
  tparam1.rotate_param = rotate;
H
HappyAngel 已提交
559

H
HappyAngel 已提交
560
  ImagePreprocess image_preprocess(srcFormat, dstFormat, tparam);
561
  std::cout << "image convert testing" << std::endl;
H
HappyAngel 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  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;
  }
577
  std::cout << "image resize testing" << std::endl;
H
HappyAngel 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  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 已提交
595

596
  std::cout << "image rotate testing" << std::endl;
H
HappyAngel 已提交
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
  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);
627
  std::cout << "image flip testing" << std::endl;
H
HappyAngel 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
  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 已提交
644

H
HappyAngel 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
#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 已提交
666

H
HappyAngel 已提交
667 668 669 670 671 672 673
                  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 已提交
674

H
HappyAngel 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
                  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 已提交
777

H
HappyAngel 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
                  // 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 已提交
796

H
HappyAngel 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
                  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 已提交
834 835 836 837
      }
    }
  }
}
H
HappyAngel 已提交
838
#endif
H
HappyAngel 已提交
839 840 841 842

int main(int argc, char** argv) {
  if (argc < 7) {
    std::cerr << "[ERROR] usage: " << argv[0]
H
HappyAngel 已提交
843 844 845
              << " has_img image_path/txt_path dst_apth srcFormat dstFormat "
                 "dstw dsth "
              << "[options] srcw srch flip rotate test_iter\n ";
H
HappyAngel 已提交
846 847
    exit(1);
  }
H
HappyAngel 已提交
848 849 850 851 852 853 854 855 856
  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 已提交
857 858
  int flip = -1;
  float rotate = 90;
H
HappyAngel 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
  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) {
      test_iter = atoi(argv[10]);
    }
H
HappyAngel 已提交
883
  }
H
HappyAngel 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
  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 已提交
900 901
  return 0;
}