From f7747c17df87987bb795b39b6750dda5170a4b08 Mon Sep 17 00:00:00 2001 From: taiqzheng <2013898008@qq.com> Date: Sun, 21 May 2023 20:26:01 +0800 Subject: [PATCH 1/5] [DIP] Add benchmark for Buddy rotation operation. --- .../BuddyRotate2DBenchmark.cpp | 141 ++++++++++++++++++ benchmarks/ImageProcessing/CMakeLists.txt | 26 ++++ benchmarks/ImageProcessing/MainRotate.cpp | 54 +++++++ 3 files changed, 221 insertions(+) create mode 100644 benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp create mode 100644 benchmarks/ImageProcessing/MainRotate.cpp diff --git a/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp new file mode 100644 index 00000000..7e2c7fc2 --- /dev/null +++ b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp @@ -0,0 +1,141 @@ +//===- BuddyRotate2DBenchmark.cpp -------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// This file implements the benchmark for Rotate2D operation. +// +//===----------------------------------------------------------------------===// + +#include +#include +#include +#include +#include + +using namespace cv; +using namespace std; + +// Declare input image. +Mat inputImageBuddyRotate2D; + +// Define the angle. +float BuddyRotate2DAngle; + +// Define sizes of input. +intptr_t sizesInputBuddyRotate2D[2]; + +// Declare Angle option supported. +enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; + +// Define Angle option selected. +AngleOption AngleType; + +void initializeBuddyRotate2D(char **argv) { + inputImageBuddyRotate2D = imread(argv[1], IMREAD_GRAYSCALE); + + sizesInputBuddyRotate2D[0] = inputImageBuddyRotate2D.rows; + sizesInputBuddyRotate2D[1] = inputImageBuddyRotate2D.cols; + + if (static_cast(argv[2]) == "DEGREE") { + AngleType = ANGLE_DEGREE; + } else { + AngleType = ANGLE_RADIAN; + } + + std::string argAngle = argv[3]; + try { + BuddyRotate2DAngle = std::stof(argAngle); + } catch (const std::exception& e) { + cout << "Exception converting rotation angle to float." << endl; + } +} + +static void Buddy_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { + // Define the MemRef descriptor for input. + Img inputBuddyRotate2D(inputImageBuddyRotate2D); + + for (auto _ : state) { + for (int i = 0; i < state.range(0); ++i) { + // Call the MLIR Rotate2D function. + MemRef output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, + dip::ANGLE_TYPE::DEGREE); + } + } +} + +static void Buddy_Rotate2D_ANGLE_RADIAN(benchmark::State &state) { + // Define the MemRef descriptor for input. + Img inputBuddyRotate2D(inputImageBuddyRotate2D); + + for (auto _ : state) { + for (int i = 0; i < state.range(0); ++i) { + // Call the MLIR Rotate2D function. + MemRef output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, + dip::ANGLE_TYPE::RADIAN); + } + } +} + +// Register benchmarking function. +void registerBenchmarkBuddyRotate2D() { + if (AngleType == ANGLE_DEGREE) { + BENCHMARK(Buddy_Rotate2D_ANGLE_DEGREE) + ->Arg(1) + ->Unit(benchmark::kMillisecond); + } else { + BENCHMARK(Buddy_Rotate2D_ANGLE_RADIAN) + ->Arg(1) + ->Unit(benchmark::kMillisecond); + } +} + +// Generate result image. +void generateResultBuddyRotate2D() { + // Define the MemRef descriptor for input. + Img input(inputImageBuddyRotate2D); + MemRef output(sizesInputBuddyRotate2D); + // Run the resize 2D operation. + if (AngleType == ANGLE_DEGREE) { + // Call the MLIR Rotate2D function. + output = dip::Rotate2D(&input, BuddyRotate2DAngle, + dip::ANGLE_TYPE::DEGREE); + } else { + // Call the MLIR Rotate2D function. + output = dip::Rotate2D(&input, BuddyRotate2DAngle, + dip::ANGLE_TYPE::RADIAN); + } + + // Define a cv::Mat with the output of the resize operation. + Mat outputImage(output.getSizes()[0], output.getSizes()[1], CV_32FC1, + output.getData()); + + // Choose a PNG compression level + vector compressionParams; + compressionParams.push_back(IMWRITE_PNG_COMPRESSION); + compressionParams.push_back(9); + + // Write output to PNG. + bool result = true; + try { + result = imwrite("ResultBuddyRotate2D.png", outputImage, compressionParams); + } catch (const cv::Exception &ex) { + fprintf(stderr, "Exception converting image to PNG format: %s\n", + ex.what()); + } + if (result) + cout << "Saved PNG file." << endl; + else + cout << "ERROR: Can't save PNG file." << endl; +} diff --git a/benchmarks/ImageProcessing/CMakeLists.txt b/benchmarks/ImageProcessing/CMakeLists.txt index b1113e4a..dae2e64f 100644 --- a/benchmarks/ImageProcessing/CMakeLists.txt +++ b/benchmarks/ImageProcessing/CMakeLists.txt @@ -117,3 +117,29 @@ target_link_libraries(image-processing-benchmark # Link Buddy MLIR DIP Library. BuddyLibDIP ) + +#------------------------------------------------------------------------------- +# Image Processing Rotate Benchmark Target +#------------------------------------------------------------------------------- + +add_executable(image-processing-rotate-benchmark + MainRotate.cpp + BuddyRotate2DBenchmark.cpp + ) + +target_include_directories(image-processing-rotate-benchmark + PRIVATE + ${BUDDY_SOURCE_DIR}/benchmarks/ImageProcessing/include/ + ) + +target_link_directories(image-processing-rotate-benchmark + PRIVATE + ${BUDDY_MLIR_LIB_DIR} + ) + +target_link_libraries(image-processing-rotate-benchmark + GoogleBenchmark + ${OpenCV_LIBS} + # Link Buddy MLIR DIP Library. + BuddyLibDIP + ) diff --git a/benchmarks/ImageProcessing/MainRotate.cpp b/benchmarks/ImageProcessing/MainRotate.cpp new file mode 100644 index 00000000..ac7ceab9 --- /dev/null +++ b/benchmarks/ImageProcessing/MainRotate.cpp @@ -0,0 +1,54 @@ +//===- MainRotate.cpp -----------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// This is the main file of the image processing rotate benchmark. +// +//===----------------------------------------------------------------------===// + +#include +#include + +void initializeBuddyRotate2D(char **); + +void generateResultBuddyRotate2D(); + +void registerBenchmarkBuddyRotate2D(); + +// Run benchmarks. +int main(int argc, char **argv) { + if (argc != 4) { + throw std::invalid_argument( + "Wrong format of command line arguments.\n" + "Correct format is ./image-processing-rotate-benchmark \n where " + "image path provides path of the image to be processed, Rotate option " + "available are DEGREE, RADIAN. " + "RotateAngle accepts a " + "float number for Rotate option.\n"); + } + + initializeBuddyRotate2D(argv); + + registerBenchmarkBuddyRotate2D(); + + ::benchmark::Initialize(&argc, argv); + ::benchmark::RunSpecifiedBenchmarks(); + + // Generate result image. + generateResultBuddyRotate2D(); + + return 0; +} From 20523410273d4c38990b78bbbc0fbf3bd239c5a6 Mon Sep 17 00:00:00 2001 From: taiqzheng <2013898008@qq.com> Date: Mon, 22 May 2023 00:27:32 +0800 Subject: [PATCH 2/5] [DIP] Add benchmark for OpenCV rotation operation. --- benchmarks/ImageProcessing/CMakeLists.txt | 1 + benchmarks/ImageProcessing/MainRotate.cpp | 9 +- .../OpenCVRotate2DBenchmark.cpp | 122 ++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp diff --git a/benchmarks/ImageProcessing/CMakeLists.txt b/benchmarks/ImageProcessing/CMakeLists.txt index dae2e64f..f5594d6e 100644 --- a/benchmarks/ImageProcessing/CMakeLists.txt +++ b/benchmarks/ImageProcessing/CMakeLists.txt @@ -125,6 +125,7 @@ target_link_libraries(image-processing-benchmark add_executable(image-processing-rotate-benchmark MainRotate.cpp BuddyRotate2DBenchmark.cpp + OpenCVRotate2DBenchmark.cpp ) target_include_directories(image-processing-rotate-benchmark diff --git a/benchmarks/ImageProcessing/MainRotate.cpp b/benchmarks/ImageProcessing/MainRotate.cpp index ac7ceab9..73c5aef0 100644 --- a/benchmarks/ImageProcessing/MainRotate.cpp +++ b/benchmarks/ImageProcessing/MainRotate.cpp @@ -22,10 +22,13 @@ #include void initializeBuddyRotate2D(char **); +void initializeOpenCVRotate2D(char **); void generateResultBuddyRotate2D(); +void generateResultOpenCVRotate2D(); void registerBenchmarkBuddyRotate2D(); +void registerBenchmarkOpenCVRotate2D(); // Run benchmarks. int main(int argc, char **argv) { @@ -37,18 +40,22 @@ int main(int argc, char **argv) { "image path provides path of the image to be processed, Rotate option " "available are DEGREE, RADIAN. " "RotateAngle accepts a " - "float number for Rotate option.\n"); + "float number for Rotate option." + "OpenCV rotate() only supports 90, 180 and 270 degree.\n"); } initializeBuddyRotate2D(argv); + initializeOpenCVRotate2D(argv); registerBenchmarkBuddyRotate2D(); + registerBenchmarkOpenCVRotate2D(); ::benchmark::Initialize(&argc, argv); ::benchmark::RunSpecifiedBenchmarks(); // Generate result image. generateResultBuddyRotate2D(); + generateResultOpenCVRotate2D(); return 0; } diff --git a/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp new file mode 100644 index 00000000..e04f9038 --- /dev/null +++ b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp @@ -0,0 +1,122 @@ +//===- OpenCVRotate2DBenchmark.cpp ----------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// This file implements the benchmark for OpenCV's Rotate Operations. +// +//===----------------------------------------------------------------------===// + +#include +#include + +using namespace cv; +using namespace std; + +// Declare input and output image. +Mat inputImageOpenCVRotate2D, outputImageOpenCVRotate2D; + +// Define the angle. +int OpenCVRotate2DAngle; + +// Define sizes of input. +intptr_t sizesInputOpenCVRotate2D[2]; +cv::Size sizesOutputOpenCVRotate2D; + +// Declare Angle option supported. +enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; + +// Define Angle option selected. +AngleOption OpenCVAngleType; + +// Define OpenCV Rotate option. +cv::RotateFlags RotateFlag = cv::ROTATE_90_CLOCKWISE; + +// Define the OpenCV Rotate benchmark option. +bool OpenCVRunRotate = true; + +void initializeOpenCVRotate2D(char **argv) { + inputImageOpenCVRotate2D = imread(argv[1], IMREAD_GRAYSCALE); + + sizesInputOpenCVRotate2D[0] = inputImageOpenCVRotate2D.rows; + sizesInputOpenCVRotate2D[1] = inputImageOpenCVRotate2D.cols; + + if (static_cast(argv[2]) == "DEGREE") { + OpenCVAngleType = ANGLE_DEGREE; + } else { + OpenCVAngleType = ANGLE_RADIAN; + } + + std::string argAngle = argv[3]; + try { + OpenCVRotate2DAngle = std::stoi(argAngle); + OpenCVRotate2DAngle = OpenCVRotate2DAngle % 360; + } catch (const std::exception& e) { + cout << "OpenCV rotate() support three ways: 90 degrees clockwise, 180 degrees clockwise, 270 degrees clockwise." << endl; + } + if (OpenCVRotate2DAngle == 90) { + RotateFlag = cv::ROTATE_90_CLOCKWISE; + } else if (OpenCVRotate2DAngle == 180) { + RotateFlag = cv::ROTATE_180; + } else if (OpenCVRotate2DAngle == 270) { + RotateFlag = cv::ROTATE_90_COUNTERCLOCKWISE; + } else { + OpenCVRunRotate = false; + } +} + +// Benchmarking function. +static void OpenCV_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { + for (auto _ : state) { + for (int i = 0; i < state.range(0); ++i) { + cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, RotateFlag); + } + } +} + +// Register benchmarking function. +void registerBenchmarkOpenCVRotate2D() { + if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { + BENCHMARK(OpenCV_Rotate2D_ANGLE_DEGREE) + ->Arg(1) + ->Unit(benchmark::kMillisecond); + } +} + +// Generate result image. +void generateResultOpenCVRotate2D() { + // Run the resize 2D operation. + if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { + cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, OpenCVRotate2DAngle); + + // Choose a PNG compression level + vector compressionParams; + compressionParams.push_back(IMWRITE_PNG_COMPRESSION); + compressionParams.push_back(9); + + // Write output to PNG. + bool result = false; + try { + result = + imwrite("ResultOpenCVRotate2D.png", outputImageOpenCVRotate2D, compressionParams); + } catch (const cv::Exception &ex) { + fprintf(stderr, "Exception converting image to PNG format: %s\n", + ex.what()); + } + if (result) + cout << "Saved PNG file." << endl; + else + cout << "ERROR: Can't save PNG file." << endl; + } +} From fb143272212f9e1b86101f65ee0a13c476872192 Mon Sep 17 00:00:00 2001 From: taiqzheng <2013898008@qq.com> Date: Wed, 31 May 2023 17:09:58 +0800 Subject: [PATCH 3/5] Remove duplicate enum class for angle type. --- .../BuddyRotate2DBenchmark.cpp | 25 ++++++++----------- .../OpenCVRotate2DBenchmark.cpp | 20 +++++++-------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp index 7e2c7fc2..d29006a5 100644 --- a/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp +++ b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp @@ -36,11 +36,8 @@ float BuddyRotate2DAngle; // Define sizes of input. intptr_t sizesInputBuddyRotate2D[2]; -// Declare Angle option supported. -enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; - // Define Angle option selected. -AngleOption AngleType; +dip::ANGLE_TYPE AngleType; void initializeBuddyRotate2D(char **argv) { inputImageBuddyRotate2D = imread(argv[1], IMREAD_GRAYSCALE); @@ -49,9 +46,9 @@ void initializeBuddyRotate2D(char **argv) { sizesInputBuddyRotate2D[1] = inputImageBuddyRotate2D.cols; if (static_cast(argv[2]) == "DEGREE") { - AngleType = ANGLE_DEGREE; + AngleType = dip::ANGLE_TYPE::DEGREE; } else { - AngleType = ANGLE_RADIAN; + AngleType = dip::ANGLE_TYPE::RADIAN; } std::string argAngle = argv[3]; @@ -62,7 +59,7 @@ void initializeBuddyRotate2D(char **argv) { } } -static void Buddy_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { +static void Buddy_Rotate2D_DEGREE(benchmark::State &state) { // Define the MemRef descriptor for input. Img inputBuddyRotate2D(inputImageBuddyRotate2D); @@ -75,7 +72,7 @@ static void Buddy_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { } } -static void Buddy_Rotate2D_ANGLE_RADIAN(benchmark::State &state) { +static void Buddy_Rotate2D_RADIAN(benchmark::State &state) { // Define the MemRef descriptor for input. Img inputBuddyRotate2D(inputImageBuddyRotate2D); @@ -90,12 +87,12 @@ static void Buddy_Rotate2D_ANGLE_RADIAN(benchmark::State &state) { // Register benchmarking function. void registerBenchmarkBuddyRotate2D() { - if (AngleType == ANGLE_DEGREE) { - BENCHMARK(Buddy_Rotate2D_ANGLE_DEGREE) + if (AngleType == dip::ANGLE_TYPE::DEGREE) { + BENCHMARK(Buddy_Rotate2D_DEGREE) ->Arg(1) ->Unit(benchmark::kMillisecond); } else { - BENCHMARK(Buddy_Rotate2D_ANGLE_RADIAN) + BENCHMARK(Buddy_Rotate2D_RADIAN) ->Arg(1) ->Unit(benchmark::kMillisecond); } @@ -106,8 +103,8 @@ void generateResultBuddyRotate2D() { // Define the MemRef descriptor for input. Img input(inputImageBuddyRotate2D); MemRef output(sizesInputBuddyRotate2D); - // Run the resize 2D operation. - if (AngleType == ANGLE_DEGREE) { + // Run the rotate 2D operation. + if (AngleType == dip::ANGLE_TYPE::DEGREE) { // Call the MLIR Rotate2D function. output = dip::Rotate2D(&input, BuddyRotate2DAngle, dip::ANGLE_TYPE::DEGREE); @@ -117,7 +114,7 @@ void generateResultBuddyRotate2D() { dip::ANGLE_TYPE::RADIAN); } - // Define a cv::Mat with the output of the resize operation. + // Define a cv::Mat with the output of the rotate operation. Mat outputImage(output.getSizes()[0], output.getSizes()[1], CV_32FC1, output.getData()); diff --git a/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp index e04f9038..6fe4ae9f 100644 --- a/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp +++ b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp @@ -20,6 +20,7 @@ #include #include +#include using namespace cv; using namespace std; @@ -34,11 +35,8 @@ int OpenCVRotate2DAngle; intptr_t sizesInputOpenCVRotate2D[2]; cv::Size sizesOutputOpenCVRotate2D; -// Declare Angle option supported. -enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; - // Define Angle option selected. -AngleOption OpenCVAngleType; +dip::ANGLE_TYPE OpenCVAngleType; // Define OpenCV Rotate option. cv::RotateFlags RotateFlag = cv::ROTATE_90_CLOCKWISE; @@ -53,9 +51,9 @@ void initializeOpenCVRotate2D(char **argv) { sizesInputOpenCVRotate2D[1] = inputImageOpenCVRotate2D.cols; if (static_cast(argv[2]) == "DEGREE") { - OpenCVAngleType = ANGLE_DEGREE; + OpenCVAngleType = dip::ANGLE_TYPE::DEGREE; } else { - OpenCVAngleType = ANGLE_RADIAN; + OpenCVAngleType = dip::ANGLE_TYPE::RADIAN; } std::string argAngle = argv[3]; @@ -77,7 +75,7 @@ void initializeOpenCVRotate2D(char **argv) { } // Benchmarking function. -static void OpenCV_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { +static void OpenCV_Rotate2D_DEGREE(benchmark::State &state) { for (auto _ : state) { for (int i = 0; i < state.range(0); ++i) { cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, RotateFlag); @@ -87,8 +85,8 @@ static void OpenCV_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { // Register benchmarking function. void registerBenchmarkOpenCVRotate2D() { - if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { - BENCHMARK(OpenCV_Rotate2D_ANGLE_DEGREE) + if (OpenCVAngleType == dip::ANGLE_TYPE::DEGREE && OpenCVRunRotate == true) { + BENCHMARK(OpenCV_Rotate2D_DEGREE) ->Arg(1) ->Unit(benchmark::kMillisecond); } @@ -96,8 +94,8 @@ void registerBenchmarkOpenCVRotate2D() { // Generate result image. void generateResultOpenCVRotate2D() { - // Run the resize 2D operation. - if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { + // Run the rotate 2D operation. + if (OpenCVAngleType == dip::ANGLE_TYPE::DEGREE && OpenCVRunRotate == true) { cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, OpenCVRotate2DAngle); // Choose a PNG compression level From 8adb202c37cd010123c74b43fe0695dc50859e33 Mon Sep 17 00:00:00 2001 From: taiqzheng <2013898008@qq.com> Date: Wed, 7 Jun 2023 10:31:03 +0800 Subject: [PATCH 4/5] Reformat with clang-format. And add the angle option for OpenCV. --- .../BuddyRotate2DBenchmark.cpp | 32 ++++++------- benchmarks/ImageProcessing/MainRotate.cpp | 12 ++--- .../OpenCVRotate2DBenchmark.cpp | 46 ++++++++++--------- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp index d29006a5..aa0136da 100644 --- a/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp +++ b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp @@ -20,11 +20,11 @@ #include #include -#include #include +#include #include -using namespace cv; +using namespace cv; using namespace std; // Declare input image. @@ -54,7 +54,7 @@ void initializeBuddyRotate2D(char **argv) { std::string argAngle = argv[3]; try { BuddyRotate2DAngle = std::stof(argAngle); - } catch (const std::exception& e) { + } catch (const std::exception &e) { cout << "Exception converting rotation angle to float." << endl; } } @@ -66,8 +66,8 @@ static void Buddy_Rotate2D_DEGREE(benchmark::State &state) { for (auto _ : state) { for (int i = 0; i < state.range(0); ++i) { // Call the MLIR Rotate2D function. - MemRef output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, - dip::ANGLE_TYPE::DEGREE); + MemRef output = dip::Rotate2D( + &inputBuddyRotate2D, BuddyRotate2DAngle, dip::ANGLE_TYPE::DEGREE); } } } @@ -79,8 +79,8 @@ static void Buddy_Rotate2D_RADIAN(benchmark::State &state) { for (auto _ : state) { for (int i = 0; i < state.range(0); ++i) { // Call the MLIR Rotate2D function. - MemRef output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, - dip::ANGLE_TYPE::RADIAN); + MemRef output = dip::Rotate2D( + &inputBuddyRotate2D, BuddyRotate2DAngle, dip::ANGLE_TYPE::RADIAN); } } } @@ -88,14 +88,10 @@ static void Buddy_Rotate2D_RADIAN(benchmark::State &state) { // Register benchmarking function. void registerBenchmarkBuddyRotate2D() { if (AngleType == dip::ANGLE_TYPE::DEGREE) { - BENCHMARK(Buddy_Rotate2D_DEGREE) - ->Arg(1) - ->Unit(benchmark::kMillisecond); + BENCHMARK(Buddy_Rotate2D_DEGREE)->Arg(1)->Unit(benchmark::kMillisecond); } else { - BENCHMARK(Buddy_Rotate2D_RADIAN) - ->Arg(1) - ->Unit(benchmark::kMillisecond); - } + BENCHMARK(Buddy_Rotate2D_RADIAN)->Arg(1)->Unit(benchmark::kMillisecond); + } } // Generate result image. @@ -106,13 +102,11 @@ void generateResultBuddyRotate2D() { // Run the rotate 2D operation. if (AngleType == dip::ANGLE_TYPE::DEGREE) { // Call the MLIR Rotate2D function. - output = dip::Rotate2D(&input, BuddyRotate2DAngle, - dip::ANGLE_TYPE::DEGREE); + output = dip::Rotate2D(&input, BuddyRotate2DAngle, dip::ANGLE_TYPE::DEGREE); } else { // Call the MLIR Rotate2D function. - output = dip::Rotate2D(&input, BuddyRotate2DAngle, - dip::ANGLE_TYPE::RADIAN); - } + output = dip::Rotate2D(&input, BuddyRotate2DAngle, dip::ANGLE_TYPE::RADIAN); + } // Define a cv::Mat with the output of the rotate operation. Mat outputImage(output.getSizes()[0], output.getSizes()[1], CV_32FC1, diff --git a/benchmarks/ImageProcessing/MainRotate.cpp b/benchmarks/ImageProcessing/MainRotate.cpp index 73c5aef0..3e2b86b4 100644 --- a/benchmarks/ImageProcessing/MainRotate.cpp +++ b/benchmarks/ImageProcessing/MainRotate.cpp @@ -1,4 +1,5 @@ -//===- MainRotate.cpp -----------------------------------------------------------===// +//===- MainRotate.cpp +//-----------------------------------------------------------===// // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -24,7 +25,7 @@ void initializeBuddyRotate2D(char **); void initializeOpenCVRotate2D(char **); -void generateResultBuddyRotate2D(); +void generateResultBuddyRotate2D(); void generateResultOpenCVRotate2D(); void registerBenchmarkBuddyRotate2D(); @@ -35,12 +36,11 @@ int main(int argc, char **argv) { if (argc != 4) { throw std::invalid_argument( "Wrong format of command line arguments.\n" - "Correct format is ./image-processing-rotate-benchmark \n where " + "Correct format is ./image-processing-rotate-benchmark " + " \n where " "image path provides path of the image to be processed, Rotate option " "available are DEGREE, RADIAN. " - "RotateAngle accepts a " - "float number for Rotate option." + "RotateAngle accepts a float number for Rotate option." "OpenCV rotate() only supports 90, 180 and 270 degree.\n"); } diff --git a/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp index 6fe4ae9f..9222b0dd 100644 --- a/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp +++ b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp @@ -20,7 +20,6 @@ #include #include -#include using namespace cv; using namespace std; @@ -35,8 +34,11 @@ int OpenCVRotate2DAngle; intptr_t sizesInputOpenCVRotate2D[2]; cv::Size sizesOutputOpenCVRotate2D; +// Declare Angle option supported. +enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; + // Define Angle option selected. -dip::ANGLE_TYPE OpenCVAngleType; +AngleOption OpenCVAngleType; // Define OpenCV Rotate option. cv::RotateFlags RotateFlag = cv::ROTATE_90_CLOCKWISE; @@ -51,17 +53,19 @@ void initializeOpenCVRotate2D(char **argv) { sizesInputOpenCVRotate2D[1] = inputImageOpenCVRotate2D.cols; if (static_cast(argv[2]) == "DEGREE") { - OpenCVAngleType = dip::ANGLE_TYPE::DEGREE; + OpenCVAngleType = ANGLE_DEGREE; } else { - OpenCVAngleType = dip::ANGLE_TYPE::RADIAN; + OpenCVAngleType = ANGLE_RADIAN; } std::string argAngle = argv[3]; try { OpenCVRotate2DAngle = std::stoi(argAngle); OpenCVRotate2DAngle = OpenCVRotate2DAngle % 360; - } catch (const std::exception& e) { - cout << "OpenCV rotate() support three ways: 90 degrees clockwise, 180 degrees clockwise, 270 degrees clockwise." << endl; + } catch (const std::exception &e) { + cout << "OpenCV rotate() support three ways: 90 degrees clockwise, 180 " + "degrees clockwise, 270 degrees clockwise." + << endl; } if (OpenCVRotate2DAngle == 90) { RotateFlag = cv::ROTATE_90_CLOCKWISE; @@ -78,25 +82,25 @@ void initializeOpenCVRotate2D(char **argv) { static void OpenCV_Rotate2D_DEGREE(benchmark::State &state) { for (auto _ : state) { for (int i = 0; i < state.range(0); ++i) { - cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, RotateFlag); + cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, + RotateFlag); } } } // Register benchmarking function. void registerBenchmarkOpenCVRotate2D() { - if (OpenCVAngleType == dip::ANGLE_TYPE::DEGREE && OpenCVRunRotate == true) { - BENCHMARK(OpenCV_Rotate2D_DEGREE) - ->Arg(1) - ->Unit(benchmark::kMillisecond); - } + if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { + BENCHMARK(OpenCV_Rotate2D_DEGREE)->Arg(1)->Unit(benchmark::kMillisecond); + } } // Generate result image. void generateResultOpenCVRotate2D() { // Run the rotate 2D operation. - if (OpenCVAngleType == dip::ANGLE_TYPE::DEGREE && OpenCVRunRotate == true) { - cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, OpenCVRotate2DAngle); + if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { + cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, + OpenCVRotate2DAngle); // Choose a PNG compression level vector compressionParams; @@ -106,15 +110,15 @@ void generateResultOpenCVRotate2D() { // Write output to PNG. bool result = false; try { - result = - imwrite("ResultOpenCVRotate2D.png", outputImageOpenCVRotate2D, compressionParams); + result = imwrite("ResultOpenCVRotate2D.png", outputImageOpenCVRotate2D, + compressionParams); } catch (const cv::Exception &ex) { - fprintf(stderr, "Exception converting image to PNG format: %s\n", - ex.what()); + fprintf(stderr, "Exception converting image to PNG format: %s\n", + ex.what()); } if (result) - cout << "Saved PNG file." << endl; + cout << "Saved PNG file." << endl; else - cout << "ERROR: Can't save PNG file." << endl; - } + cout << "ERROR: Can't save PNG file." << endl; + } } From 275fa564cabba58a9e73dd755f068061406758c4 Mon Sep 17 00:00:00 2001 From: taiqzheng <2013898008@qq.com> Date: Mon, 12 Jun 2023 16:37:38 +0800 Subject: [PATCH 5/5] Remove duplicate enum class for OpenCVRotate2DBenchmark.cpp - Use clang-format for rotate benchmark files. --- .../ImageProcessing/BuddyRotate2DBenchmark.cpp | 3 ++- benchmarks/ImageProcessing/MainRotate.cpp | 3 +-- .../ImageProcessing/OpenCVRotate2DBenchmark.cpp | 14 ++++++-------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp index aa0136da..2b828866 100644 --- a/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp +++ b/benchmarks/ImageProcessing/BuddyRotate2DBenchmark.cpp @@ -1,4 +1,4 @@ -//===- BuddyRotate2DBenchmark.cpp -------------------------------------------===// +//===- BuddyRotate2DBenchmark.cpp -----------------------------------------===// // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // +//===- BuddyRotate2DBenchmark.cpp -----------------------------------------===// //===----------------------------------------------------------------------===// // // This file implements the benchmark for Rotate2D operation. diff --git a/benchmarks/ImageProcessing/MainRotate.cpp b/benchmarks/ImageProcessing/MainRotate.cpp index 3e2b86b4..438ee808 100644 --- a/benchmarks/ImageProcessing/MainRotate.cpp +++ b/benchmarks/ImageProcessing/MainRotate.cpp @@ -1,5 +1,4 @@ -//===- MainRotate.cpp -//-----------------------------------------------------------===// +//===- MainRotate.cpp -----------------------------------------------------===// // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp index 9222b0dd..e751887a 100644 --- a/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp +++ b/benchmarks/ImageProcessing/OpenCVRotate2DBenchmark.cpp @@ -19,6 +19,7 @@ //===----------------------------------------------------------------------===// #include +#include #include using namespace cv; @@ -34,11 +35,8 @@ int OpenCVRotate2DAngle; intptr_t sizesInputOpenCVRotate2D[2]; cv::Size sizesOutputOpenCVRotate2D; -// Declare Angle option supported. -enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; - // Define Angle option selected. -AngleOption OpenCVAngleType; +dip::ANGLE_TYPE OpenCVAngleType; // Define OpenCV Rotate option. cv::RotateFlags RotateFlag = cv::ROTATE_90_CLOCKWISE; @@ -53,9 +51,9 @@ void initializeOpenCVRotate2D(char **argv) { sizesInputOpenCVRotate2D[1] = inputImageOpenCVRotate2D.cols; if (static_cast(argv[2]) == "DEGREE") { - OpenCVAngleType = ANGLE_DEGREE; + OpenCVAngleType = dip::ANGLE_TYPE::DEGREE; } else { - OpenCVAngleType = ANGLE_RADIAN; + OpenCVAngleType = dip::ANGLE_TYPE::RADIAN; } std::string argAngle = argv[3]; @@ -90,7 +88,7 @@ static void OpenCV_Rotate2D_DEGREE(benchmark::State &state) { // Register benchmarking function. void registerBenchmarkOpenCVRotate2D() { - if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { + if (OpenCVAngleType == dip::ANGLE_TYPE::DEGREE && OpenCVRunRotate == true) { BENCHMARK(OpenCV_Rotate2D_DEGREE)->Arg(1)->Unit(benchmark::kMillisecond); } } @@ -98,7 +96,7 @@ void registerBenchmarkOpenCVRotate2D() { // Generate result image. void generateResultOpenCVRotate2D() { // Run the rotate 2D operation. - if (OpenCVAngleType == ANGLE_DEGREE && OpenCVRunRotate == true) { + if (OpenCVAngleType == dip::ANGLE_TYPE::DEGREE && OpenCVRunRotate == true) { cv::rotate(inputImageOpenCVRotate2D, outputImageOpenCVRotate2D, OpenCVRotate2DAngle);