|
| 1 | +#include "../../../include/ImageProcessing/Kernels.h" |
| 2 | +#include "Utils/Container.h" |
| 3 | + |
| 4 | +#include <opencv2/imgcodecs.hpp> |
| 5 | +#include <opencv2/opencv.hpp> |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | + |
| 9 | +using namespace cv; |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +// Declare the Corr2D C interface. |
| 13 | +extern "C" { |
| 14 | +void _mlir_ciface_corr_2d(MemRef<float, 2> *input, MemRef<float, 2> *kernel, |
| 15 | + MemRef<float, 2> *output, unsigned int centerX, |
| 16 | + unsigned int centerY, int boundaryOption); |
| 17 | +} |
| 18 | + |
| 19 | +bool equalImages(const Mat &img1, const Mat &img2) |
| 20 | +{ |
| 21 | + if (img1.rows != img2.rows || img1.cols != img2.cols) { |
| 22 | + std::cout << "Produced outputs by DIP and OpenCV differ. Image dimensions are not equal\n"; |
| 23 | + return 0; |
| 24 | + } |
| 25 | + |
| 26 | + for (unsigned int y = 0; y < img1.rows; ++y) |
| 27 | + { |
| 28 | + for (unsigned int x = 0; x < img1.cols; ++x) |
| 29 | + { |
| 30 | + if (abs(img1.at<float>(x, y) - img2.at<float>(x, y)) > 10e-3) |
| 31 | + { |
| 32 | + std::cout << "Produced outputs by DIP and OpenCV differ.\n"; |
| 33 | + return 0; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + return 1; |
| 38 | +} |
| 39 | + |
| 40 | +void testKernelImpl(const Mat& inputImage, unsigned int kernelRows, unsigned int kernelCols, float* kernelArray, |
| 41 | + unsigned int x, unsigned int y) |
| 42 | +{ |
| 43 | + // Define container sizes. |
| 44 | + intptr_t sizesInput[2] = {inputImage.rows, inputImage.cols}; |
| 45 | + intptr_t sizesKernel[2] = {kernelRows, kernelCols}; |
| 46 | + intptr_t sizesOutput[2] = {inputImage.rows, inputImage.cols}; |
| 47 | + |
| 48 | + // Define input, kernel, and output. |
| 49 | + MemRef<float, 2> input(inputImage, sizesInput); |
| 50 | + MemRef<float, 2> kernel(kernelArray, sizesKernel); |
| 51 | + MemRef<float, 2> output(sizesOutput); |
| 52 | + |
| 53 | + for (int i = 0; i < inputImage.rows; i++) |
| 54 | + for (int j = 0; j < inputImage.cols; j++) |
| 55 | + output[i * inputImage.rows + j] = 0; |
| 56 | + |
| 57 | + Mat kernel1 = Mat(kernelRows, kernelCols, CV_32FC1, kernelArray); |
| 58 | + Mat opencvOutput; |
| 59 | + |
| 60 | + _mlir_ciface_corr_2d(&input, &kernel, &output, x, y, 0); |
| 61 | + |
| 62 | + filter2D(inputImage, opencvOutput, CV_32FC1, kernel1, cv::Point(x, y), 1.0, |
| 63 | + cv::BORDER_REPLICATE); |
| 64 | + |
| 65 | + // Define a cv::Mat with the output of corr_2d. |
| 66 | + Mat dipOutput(inputImage.rows, inputImage.cols, CV_32FC1, output.getData()); |
| 67 | + |
| 68 | + if (!equalImages(dipOutput, opencvOutput)) |
| 69 | + { |
| 70 | + std::cout << "Different images produced by OpenCV and DIP for kernel :\n" << kernel1 << "\n" |
| 71 | + "when anchor point was : (" << x << ", " << y << ").\n"; |
| 72 | + return; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +void testKernel(const Mat& inputImage, unsigned int kernelRows, unsigned int kernelCols, float* kernelArray) |
| 77 | +{ |
| 78 | + for (unsigned int y = 0; y < kernelRows; ++y) |
| 79 | + for (unsigned int x = 0; x < kernelCols; ++x) |
| 80 | + testKernelImpl(inputImage, kernelRows, kernelCols, kernelArray, x, y); |
| 81 | +} |
| 82 | + |
| 83 | +int main(int argc, char **argv) |
| 84 | +{ |
| 85 | + // Read input image |
| 86 | + Mat inputImage = imread(argv[1], IMREAD_GRAYSCALE); |
| 87 | + |
| 88 | + for (auto kernel : kernelMap) |
| 89 | + testKernel(inputImage, get<1>(kernel.second), get<2>(kernel.second), get<0>(kernel.second)); |
| 90 | +} |
0 commit comments