-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegm_line.h
More file actions
342 lines (302 loc) · 6.11 KB
/
segm_line.h
File metadata and controls
342 lines (302 loc) · 6.11 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#pragma once
#include "opencv\cv.h"
#include "opencv\highgui.h"
#include "opencv2\opencv.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
using namespace cv;
class segm_line
{
private:
Mat image;
vector<int> column_left;
vector<int> column_right;
vector<int> row_up;
vector<int> row_down;
vector <cv::Mat> test_picture;
//return "true" then all pixels in column are white
bool Check_Bright_Column(int column)
{
bool flag = true;
for (int i = 0;i < image.rows;i++)
if (image.at<uchar>(i, column) != 255)
{
flag = false;
break;
}
return flag;
}
//return "true" then all pixels in column are white
bool Check_Bright_Row(int row_start, int tmp_column_left, int tmp_column_right)
{
bool flag = true;
int row_final = 0;
row_final += row_start;
Clamp(row_final++, 0, image.rows);
for (int i = row_start;i < row_final;i++)
for (int j = tmp_column_left;j < tmp_column_right;j++)
if (image.at<uchar>(i, j) != 255)
{
flag = false;
break;
}
return flag;
}
//èùåì âñå ãðàíèöû
void Find_Border_Symbols()
{
Find_Left_Border();
Find_Right_Border();
for (int i = 0;i < column_left.size();i++)
{
Find_Up_Border(column_left[i], column_right[i]);
Find_Down_Border(column_left[i], column_right[i]);
}
}
//èùåì ëåâûå ãðàíèöû
void Find_Left_Border()
{
for (int j = 0; j < image.cols; j++)
{
// if current column has black pixsels AND previous hasn't, we find new symbol
if ((!Check_Bright_Column(j)) && Check_Bright_Column(Clamp((j - 1), 0, image.cols)))
{
column_left.push_back(j - 1);
}
}
}
//èùåì ïðàâûå ãðàíèöû
void Find_Right_Border()
{
for (int j = 0; j < image.cols; j++)
{
// if current column has black pixsels AND next hasn't, we find new symbol
if ((!Check_Bright_Column(j)) && Check_Bright_Column(Clamp((j + 1), 0, image.cols)))
{
column_right.push_back(j);
}
}
}
void Find_Up_Border(int tmp_column_left, int tmp_column_right)
{
for (int i = image.rows - 1; i > 0; i--)
{
// if current column has black pixsels AND previous hasn't, we find new symbol
if (!Check_Bright_Row(i, tmp_column_left, tmp_column_right) && Check_Bright_Row(Clamp((i + 1), 0, image.rows), tmp_column_left, tmp_column_right))
{
row_up.push_back(i);
break;
}
}
}
void Find_Down_Border(int tmp_column_left, int tmp_column_right)
{
for (int i = 0; i < image.rows; i++)
{
// if current column has black pixsels AND previous hasn't, we find new symbol
if (!Check_Bright_Row(i, tmp_column_left, tmp_column_right) && Check_Bright_Row(Clamp((i - 1), 0, image.rows), tmp_column_left, tmp_column_right))
{
row_down.push_back(i);
break;
}
}
}
void Create_Words()
{
Find_Border_Symbols();
int delta = 5;
for (int i = 0;i < column_left.size();i++)
{
//ñîñ÷èòàëè âðåìåííóþ øèðèíó ñòîëáöà
int tmp_size_column = abs(column_right[i] - column_left[i]);
int tmp_size_row = abs(row_down[i] - row_up[i]);
test_picture.push_back(cv::Mat(tmp_size_row, tmp_size_column, CV_8U));
image(cv::Rect(column_left[i], row_down[i], tmp_size_column, tmp_size_row)).copyTo(test_picture[i]);
string str = "test_picture" + to_string(i);
namedWindow(str, 0);
imshow(str, test_picture[i]);
}
}
void Threshold(Mat & tmp)
{
for (int i = 0;i < tmp.rows;i++)
for (int j = 0;j < tmp.cols;j++)
if (tmp.at<uchar>(i, j) != 255)
{
tmp.at<uchar>(i, j) = 0;
}
}
int Check_image(Mat test_image, vector <cv::Mat> standart)
{
int min = 10000000;
int kmin = 0;
int sum;
for (int k = 0;k < standart.size();k++)
{
sum = 0;
for (int i = 0;i < test_image.rows;i++)
{
for (int j = 0;j < test_image.cols;j++)
{
//test_image.at<uchar>(i, j)*test_image.at<uchar>(i, j) - standart[k].at<uchar>(i, j)*standart[k].at<uchar>(i, j);
sum += sqrt(abs(test_image.at<uchar>(i, j)*test_image.at<uchar>(i, j) - standart[k].at<uchar>(i, j)*standart[k].at<uchar>(i, j)));
}
}
sum = (float)sum / (test_image.rows*test_image.cols);
//cout << sum << endl;
if (sum < min)
{
min = sum;
kmin = k;
}
}
return kmin;
}
public:
segm_line(string img)
{
Mat tmp = imread(img);
if (tmp.empty())
{
cout << "File not found" << endl;
system("pause");
exit(0);
}
cvtColor(tmp, image, COLOR_BGR2GRAY);
Threshold(image);
}
~segm_line()
{}
int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
string Do_segmentation(vector <cv::Mat> & standart)
{
Create_Words();
string result;
for (int i = 0;i < test_picture.size();i++)
{
int tmp = 0;
resize(/*sourse*/test_picture[i], /*destination*/test_picture[i], cvSize(16, 16));
tmp = Check_image(test_picture[i], standart);
result += convert(tmp);
}
return result;
}
string convert(int word)
{
string tmp;
switch (word)
{
case 0:
tmp = 'À';
break;
case 1:
tmp = 'Á';
break;
case 2:
tmp = 'Â';
break;
case 3:
tmp = 'Ã';
break;
case 4:
tmp = 'Ä';
break;
case 5:
tmp = 'Å';
break;
case 6:
tmp = '¨';
break;
case 7:
tmp = 'Æ';
break;
case 8:
tmp = 'Ç';
break;
case 9:
tmp = 'È';
break;
case 10:
tmp = 'É';
break;
case 11:
tmp = 'Ê';
break;
case 12:
tmp = 'Ë';
break;
case 13:
tmp = 'Ì';
break;
case 14:
tmp = 'Í';
break;
case 15:
tmp = 'Î';
break;
case 16:
tmp = 'Ï';
break;
case 17:
tmp = 'Ð';
break;
case 18:
tmp = 'Ñ';
break;
case 19:
tmp = 'Ò';
break;
case 20:
tmp = 'Ó';
break;
case 21:
tmp = 'Ô';
break;
case 22:
tmp = 'Õ';
break;
case 23:
tmp = 'Ö';
break;
case 24:
tmp = '×';
break;
case 25:
tmp = 'Ø';
break;
case 26:
tmp = 'Ù';
break;
case 27:
tmp = 'Ú';
break;
case 28:
tmp = 'Ü';
break;
case 29:
tmp = 'Ý';
break;
case 30:
tmp = 'Þ';
break;
case 31:
tmp = 'ß';
break;
default:
tmp = "Âîçìîæíî, ÿ ïîêà íå çíàþ òàêîé áóêâû";
break;
}
return tmp;
}
};