-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
72 lines (61 loc) · 1.58 KB
/
Copy pathtest.cpp
File metadata and controls
72 lines (61 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <gtest/gtest.h>
#include <opencv2/core/core.hpp>
static int clamp(int value, int min, int max)
{
return std::min(std::max(value, min), max);
}
static void sobelFilter(const cv::Mat& src, cv::Mat& dst)
{
// Create vertical Sobel 3x3 kernel
static const float kernelData[] =
{
+1, 0, -1,
+2, 0, -2,
+1, 0, -1,
};
static const cv::Mat kernel(3, 3, CV_32FC1, const_cast<float*>(kernelData));
// Allocate destination matrix
if ((dst.rows != src.rows) ||
(dst.cols != src.cols) ||
(dst.type() != CV_32FC1))
{
dst.create(src.rows, src.cols, CV_32FC1);
}
// Perform 2D convolution
for (int y = 0; y < src.rows; ++y)
for (int x = 0; x < src.cols; ++x)
{
float value = 0;
for (int yy = 0; yy < kernel.rows; ++yy)
for (int xx = 0; xx < kernel.cols; ++xx)
{
const int xsrc = clamp(x + xx, 0, src.cols - 1);
const int ysrc = clamp(y + yy, 0, src.rows - 1);
const float element = src.at<float>(ysrc, xsrc);
value += element * kernel.at<float>(yy, xx);
}
dst.at<float>(y, x) = value;
}
}
TEST(Sobel, Solid)
{
// Create uniform 3x3 image
static const float srcData[] =
{
1, 1, 1,
1, 1, 1,
1, 1, 1,
};
static const cv::Mat src(3, 3, CV_32FC1, const_cast<float*>(srcData));
// Run Sobel filter
cv::Mat edges;
sobelFilter(src, edges);
// Check results
ASSERT_EQ(edges.cols, src.cols);
ASSERT_EQ(edges.rows, src.rows);
for (int y = 0; y < src.rows; ++y)
for (int x = 0; x < src.cols; ++x)
{
EXPECT_EQ(edges.at<float>(y, x), 0) << "[" << y << ", " << x << "]";
}
}