-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
174 lines (141 loc) · 3.51 KB
/
main.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
bool headless = false;
void thresholdIntegral(cv::Mat &inputMat, cv::Mat &outputMat)
{
// accept only char type matrices
CV_Assert(!inputMat.empty());
CV_Assert(inputMat.depth() == CV_8U);
CV_Assert(inputMat.channels() == 1);
CV_Assert(!outputMat.empty());
CV_Assert(outputMat.depth() == CV_8U);
CV_Assert(outputMat.channels() == 1);
// rows -> height -> y
int nRows = inputMat.rows;
// cols -> width -> x
int nCols = inputMat.cols;
// create the integral image
cv::Mat sumMat;
cv::integral(inputMat, sumMat);
CV_Assert(sumMat.depth() == CV_32S);
CV_Assert(sizeof(int) == 4);
int S = MAX(nRows, nCols) / 8;
double T = 0.15;
// perform thresholding
int s2 = S / 2;
int x1, y1, x2, y2, count, sum;
// CV_Assert(sizeof(int) == 4);
int *p_y1, *p_y2;
uchar *p_inputMat, *p_outputMat;
for (int i = 0; i < nRows; ++i)
{
y1 = i - s2;
y2 = i + s2;
if (y1 < 0)
{
y1 = 0;
}
if (y2 >= nRows)
{
y2 = nRows - 1;
}
y1++;
y2++;
p_y1 = sumMat.ptr<int>(y1);
p_y2 = sumMat.ptr<int>(y2);
p_inputMat = inputMat.ptr<uchar>(i);
p_outputMat = outputMat.ptr<uchar>(i);
for (int j = 0; j < nCols; ++j)
{
// set the SxS region
x1 = j - s2;
x2 = j + s2;
if (x1 < 0)
{
x1 = 0;
}
if (x2 >= nCols)
{
x2 = nCols - 1;
}
x1++;
x2++;
count = (x2 - x1) * (y2 - y1);
// I(x,y)=s(x2,y2)-s(x1,y2)-s(x2,y1)+s(x1,x1)
sum = p_y2[x2] - p_y1[x2] - p_y2[x1] + p_y1[x1];
if ((int)(p_inputMat[j] * count) < (int)(sum * (1.0 - T)))
p_outputMat[j] = 255;
else
p_outputMat[j] = 0;
}
}
}
int main(int argc, char *argv[])
{
//! [load_image]
// Load the image
cv::Mat src = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
// Check if image is loaded fine
if (src.empty())
{
cerr << "Problem loading image!!!" << endl;
return -1;
}
// Show source image
try
{
cv::imshow("src", src);
}
catch (const cv::Exception &e)
{
cout << "Are we headless?" << endl;
headless = true;
}
//! [load_image]
//! [gray]
// Transform source image to gray
cv::Mat gray;
if (src.channels() == 3)
{
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
// Show gray image
cv::imshow("gray", gray);
}
else
{
gray = src;
}
cout << "TEST" << endl;
//! [gray]
//! [bin_1]
cv::Mat bw1;
cv::adaptiveThreshold(gray, bw1, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 15, -2);
// Show binary image
if (headless)
{
cv::imwrite("threshold_opencv.png", bw1);
}
else
{
cv::imshow("threshold_opencv", bw1);
}
//! [bin_1]
//! [bin_2]
cv::Mat bw2 = cv::Mat::zeros(gray.size(), CV_8UC1);
thresholdIntegral(gray, bw2);
// Show binary image
if (headless)
{
cv::imwrite("threshold_integral.png", bw2);
}
else
{
cv::imshow("threshold_integral", bw2);
}
//! [bin_2]
cv::waitKey(0);
return 0;
}