Skip to content

Commit aa6dc2b

Browse files
committed
Add support for testing DIP dialect with OpenCV
1 parent 12c0544 commit aa6dc2b

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,21 @@ $ cmake --build . --
110110
$ ninja test
111111
```
112112

113+
For tests related to DIP dialect :
114+
115+
```
116+
$ cd buddy-benchmark
117+
$ mkdir build && cd build
118+
$ cmake -G Ninja .. \
119+
-DIMAGE_PROCESSING_BENCHMARKS=ON \
120+
-DDEEP_LEARNING_BENCHMARKS=ON \
121+
-DBUILD_TESTS=ON \
122+
-DOpenCV_DIR=/path/to/opencv/build/ \
123+
-DBUDDY_OPT_BUILD_DIR=/path/to/buddy-mlir/build/ \
124+
-DBUDDY_OPT_STRIP_MINING=<strip mining size, default: 256> \
125+
-DBUDDY_OPT_ATTR=<ISA vector extension, default: avx512f>
126+
$ ninja DIPCorr2DTest
127+
$ cd bin && ./DIPCorr2DTest <image path>
128+
```
129+
130+
where `<image path>` is the path of image used as first operand during 2D Correlation.

tests/UnitTests/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ add_dependencies(test-container Container)
1111

1212
include(GoogleTest)
1313
gtest_discover_tests(test-container)
14+
15+
add_subdirectory(ImageProcessing)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
find_package(OpenCV REQUIRED CONFIG)
2+
include_directories(${OpenCV_INCLUDE_DIRS})
3+
4+
add_executable(DIPCorr2DTest DIPCorr2DTest.cpp)
5+
target_link_libraries(DIPCorr2DTest ${OpenCV_LIBS} Corr2D Container PNGImage)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)