Skip to content

Commit 2052341

Browse files
committed
[DIP] Add benchmark for OpenCV rotation operation.
1 parent f7747c1 commit 2052341

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

benchmarks/ImageProcessing/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ target_link_libraries(image-processing-benchmark
125125
add_executable(image-processing-rotate-benchmark
126126
MainRotate.cpp
127127
BuddyRotate2DBenchmark.cpp
128+
OpenCVRotate2DBenchmark.cpp
128129
)
129130

130131
target_include_directories(image-processing-rotate-benchmark

benchmarks/ImageProcessing/MainRotate.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
#include <stdexcept>
2323

2424
void initializeBuddyRotate2D(char **);
25+
void initializeOpenCVRotate2D(char **);
2526

2627
void generateResultBuddyRotate2D();
28+
void generateResultOpenCVRotate2D();
2729

2830
void registerBenchmarkBuddyRotate2D();
31+
void registerBenchmarkOpenCVRotate2D();
2932

3033
// Run benchmarks.
3134
int main(int argc, char **argv) {
@@ -37,18 +40,22 @@ int main(int argc, char **argv) {
3740
"image path provides path of the image to be processed, Rotate option "
3841
"available are DEGREE, RADIAN. "
3942
"RotateAngle accepts a "
40-
"float number for Rotate option.\n");
43+
"float number for Rotate option."
44+
"OpenCV rotate() only supports 90, 180 and 270 degree.\n");
4145
}
4246

4347
initializeBuddyRotate2D(argv);
48+
initializeOpenCVRotate2D(argv);
4449

4550
registerBenchmarkBuddyRotate2D();
51+
registerBenchmarkOpenCVRotate2D();
4652

4753
::benchmark::Initialize(&argc, argv);
4854
::benchmark::RunSpecifiedBenchmarks();
4955

5056
// Generate result image.
5157
generateResultBuddyRotate2D();
58+
generateResultOpenCVRotate2D();
5259

5360
return 0;
5461
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)