|
| 1 | +//====- rotation2D.cpp - Example of buddy-opt tool ===========================// |
| 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 a 2D rotation example with dip.rotate_2d operation. |
| 18 | +// The dip.rotate_2d operation will be compiled into an object file with the |
| 19 | +// buddy-opt tool. |
| 20 | +// This file will be linked with the object file to generate the executable |
| 21 | +// file. |
| 22 | +// |
| 23 | +//===----------------------------------------------------------------------===// |
| 24 | + |
| 25 | +#include <opencv2/imgcodecs.hpp> |
| 26 | +#include <opencv2/opencv.hpp> |
| 27 | + |
| 28 | +#include <Interface/buddy/dip/dip.h> |
| 29 | +#include <Interface/buddy/dip/memref.h> |
| 30 | +#include <iostream> |
| 31 | + |
| 32 | +using namespace cv; |
| 33 | +using namespace std; |
| 34 | + |
| 35 | +bool testImages(cv::Mat img1, cv::Mat img2) { |
| 36 | + if (img1.rows != img2.rows || img1.cols != img2.cols) { |
| 37 | + std::cout << "Dimensions not equal\n"; |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + for (std::ptrdiff_t i = 0; i < img1.cols; ++i) { |
| 42 | + for (std::ptrdiff_t j = 0; j < img1.rows; ++j) { |
| 43 | + if (img1.at<uchar>(i, j) != img2.at<uchar>(i, j)) { |
| 44 | + std::cout << "Pixels not equal at : (" << i << "," << j << ")\n"; |
| 45 | + std::cout << (int)img1.at<uchar>(i, j) << "\n"; |
| 46 | + std::cout << (int)img2.at<uchar>(i, j) << "\n\n"; |
| 47 | + |
| 48 | + std::cout << img1 << "\n\n"; |
| 49 | + std::cout << img2 << "\n\n"; |
| 50 | + return 0; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + return 1; |
| 55 | +} |
| 56 | + |
| 57 | +bool testImplementation(int argc, char *argv[]) { |
| 58 | + // Read as grayscale image. |
| 59 | + Mat image = imread(argv[1], IMREAD_GRAYSCALE); |
| 60 | + if (image.empty()) { |
| 61 | + cout << "Could not read the image: " << argv[1] << endl; |
| 62 | + } |
| 63 | + |
| 64 | + int inputSize = image.rows * image.cols; |
| 65 | + |
| 66 | + // Define the input with the image. |
| 67 | + float *inputAlign = (float *)malloc(inputSize * sizeof(float)); |
| 68 | + for (int i = 0; i < image.rows; i++) { |
| 69 | + for (int j = 0; j < image.cols; j++) { |
| 70 | + inputAlign[image.rows * i + j] = (float)image.at<uchar>(i, j); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Define allocated, sizes, and strides fields for the MemRef_descriptor. |
| 75 | + float *allocated = (float *)malloc(1 * sizeof(float)); |
| 76 | + intptr_t sizesInput[2] = {image.rows, image.cols}; |
| 77 | + intptr_t stridesInput[2] = {image.rows, image.cols}; |
| 78 | + |
| 79 | + // Define memref descriptors. |
| 80 | + MemRef_descriptor input = |
| 81 | + MemRef_Descriptor(allocated, inputAlign, 0, sizesInput, stridesInput); |
| 82 | + MemRef_descriptor output = dip::Rotate2D(input, 45, dip::ANGLE_TYPE::DEGREE); |
| 83 | + |
| 84 | + // Define a cv::Mat with the output of Rotate2D. |
| 85 | + Mat outputImageRotate2D(output->sizes[0], output->sizes[1], CV_32FC1, |
| 86 | + output->aligned); |
| 87 | + imwrite(argv[2], outputImageRotate2D); |
| 88 | + |
| 89 | + free(input); |
| 90 | + free(inputAlign); |
| 91 | + |
| 92 | + return 1; |
| 93 | +} |
| 94 | + |
| 95 | +int main(int argc, char *argv[]) { |
| 96 | + testImplementation(argc, argv); |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
0 commit comments