-
Notifications
You must be signed in to change notification settings - Fork 5
/
niblack.h
192 lines (152 loc) · 4.43 KB
/
niblack.h
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
175
176
177
178
179
180
181
182
183
#pragma once
#include <tuple>
using namespace std;
Mat Niblack(Mat& InputImg, float k = -2, int dW = 30)
{
int Ix = InputImg.cols;
int Iy = InputImg.rows;
unsigned char *inputIm = NULL;
inputIm = (unsigned char*)malloc(Ix*Iy * sizeof(unsigned char));
for (int y = 0; y < Iy; y++)
{
auto v = InputImg.ptr<uchar>(y);
for (int x = 0; x < Ix; x++)
*(inputIm + y*Ix + x) = v[x];
}
unsigned char *outputImB = NULL;
outputImB = (unsigned char*)calloc(Ix*Iy, sizeof(unsigned char));
long *IX_pix, *IX_gray, *IX_graygray, *IX_pix1, *IX_gray1, *IX_graygray1;
IX_pix = NULL;
IX_gray = NULL;
IX_graygray = NULL;
IX_pix1 = NULL;
IX_gray1 = NULL;
IX_graygray1 = NULL;
IX_pix = (long *)calloc((Ix + 2), sizeof(long));
IX_gray = (long *)calloc((Ix + 2), sizeof(long));
IX_graygray = (long *)calloc((Ix + 2), sizeof(long));
IX_gray1 = (long *)calloc((Ix + 2), sizeof(long));
IX_graygray1 = (long *)calloc((Ix + 2), sizeof(long));
IX_pix1 = (long *)calloc((Ix + 2), sizeof(long));
for (int y = 0; y < Iy; y++)
{
if (y == 0)
{
for (int x = 0; x < Ix; x++)
{
long pix = 0; long gray = 0; long gray2 = 0;
for (int iy = 0; iy <= dW; iy++)
{
pix++;
{
gray += *(inputIm + iy*Ix + x);
gray2 += (*(inputIm + iy*Ix + x))*(*(inputIm + iy*Ix + x));
}
}
IX_pix[x] = pix; IX_gray[x] = gray; IX_graygray[x] = gray2;
IX_pix1[x] = IX_pix[x]; IX_gray1[x] = IX_gray[x]; IX_graygray1[x] = IX_graygray[x];
}
}
else
if (y <= dW)
{
for (int x = 0; x < Ix; x++)
{
IX_pix[x] = IX_pix1[x] + 1;
{
IX_gray[x] = IX_gray1[x] + *(inputIm + (y + dW)*Ix + x);
IX_graygray[x] = IX_graygray1[x] + (*(inputIm + (y + dW)*Ix + x))*(*(inputIm + (y + dW)*Ix + x));
}
IX_pix1[x] = IX_pix[x]; IX_gray1[x] = IX_gray[x]; IX_graygray1[x] = IX_graygray[x];
}
}
else
if (y <= Iy - 1 - dW)
{
for (int x = 0; x < Ix; x++)
{
IX_pix[x] = IX_pix1[x];
{
IX_gray[x] = IX_gray1[x] + *(inputIm + (y + dW)*Ix + x) - *(inputIm + (y - dW - 1)*Ix + x);
IX_graygray[x] = IX_graygray1[x] + (*(inputIm + (y + dW)*Ix + x))*(*(inputIm + (y + dW)*Ix + x)) - (*(inputIm + (y - dW - 1)*Ix + x))*(*(inputIm + (y - dW - 1)*Ix + x));
}
IX_pix1[x] = IX_pix[x]; IX_gray1[x] = IX_gray[x]; IX_graygray1[x] = IX_graygray[x];
}
}
else
{
for (int x = 0; x < Ix; x++)
{
IX_pix[x] = IX_pix1[x] - 1;
{
IX_gray[x] = IX_gray1[x] - (*(inputIm + (y - dW - 1)*Ix + x));
IX_graygray[x] = IX_graygray1[x] - (*(inputIm + (y - dW - 1)*Ix + x))*(*(inputIm + (y - dW - 1)*Ix + x));
}
IX_pix1[x] = IX_pix[x]; IX_gray1[x] = IX_gray[x]; IX_graygray1[x] = IX_graygray[x];
}
}
long pix1, gray1, graygray1;
for (int x = 0; x < Ix; x++)
{
float m, s;
long TH, pix, gray, graygray;
if (x == 0)
{
pix = 0; gray = 0; graygray = 0;
for (int ix = 0; ix <= dW; ix++)
{
pix += IX_pix[ix]; gray += IX_gray[ix]; graygray += IX_graygray[ix];
}
}
else
if (x <= dW)
{
pix = pix1 + IX_pix[x + dW];
gray = gray1 + IX_gray[x + dW];
graygray = graygray1 + IX_graygray[x + dW];
}
else
if (x <= Ix - 1 - dW)
{
pix = pix1 + IX_pix[x + dW] - IX_pix[x - dW - 1];
gray = gray1 + IX_gray[x + dW] - IX_gray[x - dW - 1];
graygray = graygray1 + IX_graygray[x + dW] - IX_graygray[x - dW - 1];
}
else
{
pix = pix1 - IX_pix[x - dW - 1];
gray = gray1 - IX_gray[x - dW - 1];
graygray = graygray1 - IX_graygray[x - dW - 1];
}
pix1 = pix; gray1 = gray; graygray1 = graygray;
m = gray /(float) pix;
s = sqrt(fabs((float)(pix*graygray - gray*gray) / (pix*(pix - 1))));
TH = (long)( m + (float)(k*s) / 10);
{
if ((*(inputIm + y*Ix + x) >= TH))
*(outputImB + y*Ix + x) = 0;
else
*(outputImB + y*Ix + x) = 1;
}
}
}
//Release Mem
if (IX_pix != NULL) free(IX_pix);
if (IX_gray != NULL) free(IX_gray);
if (IX_graygray != NULL) free(IX_graygray);
if (IX_gray1 != NULL) free(IX_gray1);
if (IX_graygray1 != NULL) free(IX_graygray1);
if (IX_pix1 != NULL)free(IX_pix1);
Mat outImg = Mat::zeros(InputImg.size(), CV_8UC1);
for (int y = 0; y < Iy; y++)
{
uchar* v = outImg.ptr<uchar>(y);
for (int x = 0; x < Ix; x++)
{
v[x] = *(outputImB + y*Ix + x);
}
}
if (outputImB != nullptr) free(outputImB);
if (inputIm != nullptr) free(inputIm);
return outImg;
}