-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbine.cpp
316 lines (283 loc) · 6.09 KB
/
bine.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <typeinfo>
#include "Mymat.h"
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace std;
double t = 0;
void tik()
{
t = (double)getTickCount();
}
void tok(String s)
{
t = ((double)getTickCount() - t) / getTickFrequency();
cout<< s << " , " << "Times passed in seconds: " << t << endl;
t = (double)getTickCount();
}
int getIntPixelValue(Mat mat, int row, int col)
{
//row col start from 0
return mat.data[row*mat.cols+col];
}
void setPixelValue(Mat& mat, int row, int col, int value)
{
mat.data[row*mat.cols + col] = value;
//cout << value << " " << int(mat->data[row*mat->cols + col]) << endl;
}
// require mat1 mat2 result have same rows and cols
//only keep the pixel that both exist
int myCombine(Mat &mat1, Mat &mat2,Mat& result)
{
for (int i = 0; i < mat1.rows; i++)
{
for (int j = 0; j < mat1.cols; j++)
{
if (getIntPixelValue(mat1, i, j) && getIntPixelValue(mat2, i, j))
{
setPixelValue(result, i, j, 255);
}
else
{
setPixelValue(result, i, j, 0);
}
}
}
return 0;
}
int myCombineOR(Mat &mat1, Mat &mat2, Mat& result)
{
for (int i = 0; i < mat1.rows; i++)
{
for (int j = 0; j < mat1.cols; j++)
{
if (getIntPixelValue(mat1, i, j) || getIntPixelValue(mat2, i, j))
{
setPixelValue(result, i, j, 255);
}
else
{
setPixelValue(result, i, j, 0);
}
}
}
return 0;
}
void reserve(Mat &mat)
{
for (int i = 0; i < mat.rows - 1; i++)
{
for (int j = 0; j < mat.cols - 1; j++)
{
if (getIntPixelValue(mat, i, j) > 0)
{
setPixelValue(mat, i, j, 0);
}
else
{
setPixelValue(mat, i, j, 255);
}
}
}
}
void removeSinglePixel(Mat &mat)
{
for (int i = 1; i < mat.rows - 1; i++)
{
for (int j = 1; j < mat.cols - 1; j++)
{
if (getIntPixelValue(mat, i - 1, j) == 0 &&
getIntPixelValue(mat, i, j - 1) == 0 &&
getIntPixelValue(mat, i + 1, j) == 0 &&
getIntPixelValue(mat, i,j + 1) == 0)
{
setPixelValue(mat, i, j, 0);
}
}
}
}
void initMat(Mat &mat)
{
for (int i = 0; i < mat.rows; i++)
{
for (int j = 0; j < mat.cols; j++)
{
setPixelValue(mat, i, j, 0);
}
}
}
void connect(Mat &mat,Mat &res)
{
for (int i = 1; i < mat.rows - 1; i++)
{
for (int j = 1; j < mat.cols - 1; j++)
{
if (getIntPixelValue(mat, i - 1, j) && getIntPixelValue(mat, i + 1, j))
{
setPixelValue(res, i, j, 250);
}
if (getIntPixelValue(mat, i, j - 1) && getIntPixelValue(mat, i, j + 1))
{
setPixelValue(res, i, j, 250);
}
}
}
myCombineOR(mat, res, res);
}
int getFontWidth(Mat mat) {
int* width = new int[mat.cols+1]();
for (int i = 0; i < mat.rows; i++)
{
int cur = 0;
for (int j = 0; j < mat.cols; j++)
{
cur++;
if (getIntPixelValue(mat, i, j) != 0)
{
width[cur] += 1;
//cout << "!!!++== : " << cur << " " << *(width+cur)<< endl;
cur = 0;
}
}
}
int target = 0;
int maxNum = 0;
for (int i = 0; i < mat.cols + 1; i++)
{
if (*(width + i) > maxNum)
{
maxNum = *(width + i);
target = i;
}
}
delete [] width;
return target;
}
int main()
{
//Mat src = imread("HW4.bmp");
Mat src = imread("test2.jpg");
if (src.empty())
{
cout << "get image error" << endl;
return -1;
}
tik();
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
//Canny
Mat canny;
Canny(gray, canny, 150, 60, 3);
Mymat grayM(gray);
//gradient and contrast
Mymat fig2aM(gray);
//get the windwos size by font width
grayM.window = getFontWidth(canny);
Mymat fig2bM(gray.rows, gray.cols);
//for each pixel
for (int row = grayM.window; row < gray.rows - grayM.window; row++)
{
for (int col = 1 + grayM.window; col < gray.cols - grayM.window; col++)
{
//do
//for pixel sourround it within size of windows
int min = 255, max = 0;
for (int i = row - grayM.window; i < row + grayM.window; i++)
{
for (int j = col - grayM.window; j < col + grayM.window; j++)
{
if (grayM.getPixel(i, j) < min)
{
min = grayM.getPixel(i, j);
}
if (grayM.getPixel(i, j) > max)
{
max = grayM.getPixel(i, j);
}
}
}
fig2aM.setPixel(row, col,
max- min);
fig2bM.setPixel(row, col,
(255*(max - min))
/ (max + min + 0.01));
}
//cout << "LINE" << endl;
}
Mat fig2aMshow = gray.clone();
fig2aM.getMat(fig2aMshow);
Mat fig2bMshow = gray.clone();
fig2bM.getMat(fig2bMshow);
//C alpha
Mymat Calpha(gray.rows, gray.cols);
int alpha = 0.95;
for (int i = grayM.window; i < gray.rows - grayM.window; i++)
{
for (int j = 1 + grayM.window; j < gray.cols - grayM.window; j++)
{
Calpha.setPixel(i, j, alpha*fig2aM.getPixel(i, j) + (1-alpha)*fig2bM.getPixel(i, j));
}
}
Mat Calphashow = gray.clone();
Calpha.getMat(Calphashow);
tok("c alpah ");
//Otsu's
Mat otsu;
threshold(Calphashow, otsu, 0, 255, THRESH_OTSU);
tok("otsu");
//Combine
Mat comb = otsu.clone();
//myCombine(canny, otsu, comb);
removeSinglePixel(comb);
Mat connectRes = gray.clone();
tik();
initMat(connectRes);
tok("initmap");
connect(comb, connectRes);
imshow("1", otsu);
imshow("2", comb);
imshow("3", connectRes);
//Ger result
Mymat res(connectRes);
grayM.window = 1;
tok("connect");
Mat resshow = gray.clone();
res.getMat(resshow);
for (int i = grayM.window; i < grayM.getrows()- grayM.window; i++)
{
for (int j = grayM.window; j < grayM.getcols()- grayM.window; j++)
{
if (grayM.getPixel(i, j) <= (res.getMeanAverage(i, j) + (res.getStdAverage(i, j) / 2)))
{
setPixelValue(resshow, i, j, 255);
}
else
{
setPixelValue(resshow, i, j, 0);
}
}
}
tok("remove by algrothm");
//removeSinglePixel(resshow);
Mat resshow2 = resshow.clone();
//the only different between res1 and res2,resshow2 is after connect
connect(resshow, resshow2);
tok("connect once");
removeSinglePixel(resshow2);
///
reserve(resshow);
reserve(resshow2);
tok("reserve twice");
imshow("res1show",resshow);
imshow("resshow2", resshow2);
//Mat otsu2;
//threshold(gray, otsu2, 0, 255, THRESH_OTSU);
//imshow("byOTSU", otsu2);
imwrite("res1.jpg", resshow);
imwrite("res2.jpg", resshow2);
waitKey();
return 0;
}