|
| 1 | +//===- OpenCVRotate2DBenchmark.cpp ----------------------------------------===// |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// This file implements the benchmark for OpenCV's Rotate Operations. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +#include <benchmark/benchmark.h> |
| 22 | +#include <opencv2/opencv.hpp> |
| 23 | + |
| 24 | +using namespace cv; |
| 25 | +using namespace std; |
| 26 | + |
| 27 | +// Declare input and output image. |
| 28 | +Mat inputImageOpenCVRotate2D, outputImageOpenCVRotate2D; |
| 29 | + |
| 30 | +// Define the angle. |
| 31 | +int OpenCVRotate2DAngle; |
| 32 | + |
| 33 | +// Define sizes of input. |
| 34 | +intptr_t sizesInputOpenCVRotate2D[2]; |
| 35 | +cv::Size sizesOutputOpenCVRotate2D; |
| 36 | + |
| 37 | +// Declare Angle option supported. |
| 38 | +enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; |
| 39 | + |
| 40 | +// Define Angle option selected. |
| 41 | +AngleOption OpenCVAngleType; |
| 42 | + |
| 43 | +// Define OpenCV Rotate option. |
| 44 | +cv::RotateFlags RotateFlag = cv::ROTATE_90_CLOCKWISE; |
| 45 | + |
| 46 | +// Define the OpenCV Rotate benchmark option. |
| 47 | +bool OpenCVRunRotate = true; |
| 48 | + |
| 49 | +void initializeOpenCVRotate2D(char **argv) { |
| 50 | + inputImageOpenCVRotate2D = imread(argv[1], IMREAD_GRAYSCALE); |
| 51 | + |
| 52 | + sizesInputOpenCVRotate2D[0] = inputImageOpenCVRotate2D.rows; |
| 53 | + sizesInputOpenCVRotate2D[1] = inputImageOpenCVRotate2D.cols; |
| 54 | + |
| 55 | + if (static_cast<string>(argv[2]) == "DEGREE") { |
| 56 | + OpenCVAngleType = ANGLE_DEGREE; |
| 57 | + } else { |
| 58 | + OpenCVAngleType = ANGLE_RADIAN; |
| 59 | + } |
| 60 | + |
| 61 | + std::string argAngle = argv[3]; |
| 62 | + try { |
| 63 | + OpenCVRotate2DAngle = std::stoi(argAngle); |
| 64 | + OpenCVRotate2DAngle = OpenCVRotate2DAngle % 360; |
| 65 | + } catch (const std::exception& e) { |
| 66 | + cout << "OpenCV rotate() support three ways: 90 degrees clockwise, 180 degrees clockwise, 270 degrees clockwise." << endl; |
| 67 | + } |
| 68 | + if (OpenCVRotate2DAngle == 90) { |
| 69 | + RotateFlag = cv::ROTATE_90_CLOCKWISE; |
| 70 | + } else if (OpenCVRotate2DAngle == 180) { |
| 71 | + RotateFlag = cv::ROTATE_180; |
| 72 | + } else if (OpenCVRotate2DAngle == 270) { |
| 73 | + RotateFlag = cv::ROTATE_90_COUNTERCLOCKWISE; |
| 74 | + } else { |
| 75 | + OpenCVRunRotate = false; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Benchmarking function. |
| 80 | +static void OpenCV_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { |
| 81 | + for (auto _ : state) { |
| 82 | + for (int i = 0; i < state.range(0); ++i) { |
| 83 | + cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, RotateFlag); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Register benchmarking function. |
| 89 | +void registerBenchmarkOpenCVRotate2D() { |
| 90 | + if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { |
| 91 | + BENCHMARK(OpenCV_Rotate2D_ANGLE_DEGREE) |
| 92 | + ->Arg(1) |
| 93 | + ->Unit(benchmark::kMillisecond); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Generate result image. |
| 98 | +void generateResultOpenCVRotate2D() { |
| 99 | + // Run the resize 2D operation. |
| 100 | + if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { |
| 101 | + cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, OpenCVRotate2DAngle); |
| 102 | + |
| 103 | + // Choose a PNG compression level |
| 104 | + vector<int> compressionParams; |
| 105 | + compressionParams.push_back(IMWRITE_PNG_COMPRESSION); |
| 106 | + compressionParams.push_back(9); |
| 107 | + |
| 108 | + // Write output to PNG. |
| 109 | + bool result = false; |
| 110 | + try { |
| 111 | + result = |
| 112 | + imwrite("ResultOpenCVRotate2D.png", outputImageOpenCVRotate2D, compressionParams); |
| 113 | + } catch (const cv::Exception &ex) { |
| 114 | + fprintf(stderr, "Exception converting image to PNG format: %s\n", |
| 115 | + ex.what()); |
| 116 | + } |
| 117 | + if (result) |
| 118 | + cout << "Saved PNG file." << endl; |
| 119 | + else |
| 120 | + cout << "ERROR: Can't save PNG file." << endl; |
| 121 | + } |
| 122 | +} |
0 commit comments