-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslide_proc.cpp
More file actions
162 lines (132 loc) · 5.08 KB
/
slide_proc.cpp
File metadata and controls
162 lines (132 loc) · 5.08 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
#include "slide_proc.h"
Slide_proc::Slide_proc(){
}
void Slide_proc::proccess(Mat image){
add_white_border(image,image,50);
#if SLIDE_PROC_DEBUG == 1
imshow("got img",image);
waitKey();
#endif
Mat gaus_im;
Mat src_gray;
GaussianBlur(image, gaus_im, Size(3, 3), 0, 0, BORDER_DEFAULT);
cvtColor(gaus_im, src_gray, COLOR_BGR2GRAY);
threshold(src_gray,src_gray,180,255,THRESH_BINARY );
// My filter
float left_filter[9] = {1, 0, -1,
2, 0, -2,
1, 0, -1};
float right_filter[9] = {-1, 0, 1,
-2, 0, 2,
-1, 0, 1};
// Prepairing
erode(src_gray,src_gray,getStructuringElement(MORPH_RECT,Size(1,BOTTOM_STICK_LENGTH)));
//
Mat left_border, right_border;
filter2D(src_gray,left_border,-1, Mat(3,3,CV_32F,left_filter) );
filter2D(src_gray,right_border,-1, Mat(3,3,CV_32F,right_filter));
addWeighted(left_border,1,right_border,1,0,left_border);
// end filter
#if SLIDE_PROC_DEBUG == 1
imshow("after filter",left_border);
waitKey();
#endif
// Start morphology
Mat mask;
// оставляем палки длинны от bottom до upper
morphologyEx(left_border,left_border,MORPH_OPEN,getStructuringElement(MORPH_RECT,Size(1,BOTTOM_STICK_LENGTH)));// нижняя граница
morphologyEx(left_border,mask,MORPH_OPEN,getStructuringElement(MORPH_RECT,Size(1,UPPER_SICK_LENGTH)));// верхняя граница
my_inv(left_border);//инвертируем
Mat result;
addWeighted(left_border,1,mask,1,0,result);//Соединияем
morphologyEx(result,result,MORPH_CLOSE,getStructuringElement(MORPH_RECT,Size(1,BOTTOM_STICK_LENGTH)));// нижняя граница
#if SLIDE_PROC_DEBUG == 1
imshow("intermediate result",result);
#endif
morphologyEx(result,result,MORPH_OPEN,getStructuringElement(MORPH_RECT,Size(UPPER_SICK_LENGTH * 25/20,2)),Point(-1,-1),3);//добавим
morphologyEx(result,result,MORPH_CLOSE,getStructuringElement(MORPH_RECT,Size(5,BOTTOM_STICK_LENGTH)));
#if SLIDE_PROC_DEBUG == 1
imshow("after morphology",result);
waitKey();
#endif
// gen bounding rect
RNG rng(12345);
vector<vector<Point> > contours;
Mat countoured_image = image.clone();
findContours( result, contours, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<Rect> boundRect( contours.size() );
vector<Mat> pieces ( contours.size());
for( size_t i = 0; i < contours.size(); i++ ){
boundRect[i] = boundingRect( Mat(contours[i]) );
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
//drawContours( image, contours, (int)i, color);
pieces[i] = image(boundRect[i]);
rectangle(countoured_image,boundRect[i],color);
//if (! same_shape(image, boundRect[i] ) )
// pieces.push_back(image(boundRect[i]));
}
#if SLIDE_PROC_DEBUG == 1
imshow("founded contours",countoured_image);
waitKey();
#endif
// filter pieces
vector<Piece> filtered_pieces;
to_Piece(pieces,boundRect,filtered_pieces);
filter_pieces(image,filtered_pieces,filtered_pieces);
#if SLIDE_PROC_DEBUG == 1
{
vector<Rect> _;
for(auto &x : filtered_pieces)
_.push_back(x.coord);
show_rects(image,_,"filtered contours");
waitKey();
}
#endif
//drop_non_text(filtered_pieces,filtered_pieces);
#if SLIDE_PROC_DEBUG == 2
{
vector<Rect> _;
for(auto &x : filtered_pieces)
_.push_back(x.coord);
show_rects(image,_,"contours after dnt");
waitKey();
}
#endif
// Reconize text
// Create Tesseract object
auto *ocr = new tesseract::TessBaseAPI();
// Initialize tesseract to use English (eng) and the LSTM OCR engine.
ocr->Init(NULL, "rus", tesseract::OEM_LSTM_ONLY);
// Set Page segmentation mode to PSM_AUTO (3)
ocr->SetPageSegMode(tesseract::PSM_AUTO);
Mat image_minus_text = image.clone();
// recognize the text blocks
for( auto &piece : filtered_pieces ){
ocr->SetImage(piece.pic.data,piece.pic.cols,piece.pic.rows,3,piece.pic.step);
auto text = string(ocr->GetUTF8Text());
if ( ! text.empty() ){
text_blocks.push_back({piece.coord,string(ocr->GetUTF8Text())});
#if SLIDE_PROC_DEBUG == 1
imshow(text_blocks.back().text,piece.pic);
cout <<endl<< text_blocks.back().text << endl;
#endif
fill_block_white(image_minus_text,image_minus_text,piece.coord);
}
#if SLIDE_PROC_DEBUG == 1
imshow("minus_text",image_minus_text);
waitKey();
#endif
}
// threshold image without text
cvtColor(image_minus_text,image_minus_text,COLOR_BGR2GRAY);
threshold(image_minus_text,image_minus_text,150,255,THRESH_BINARY);
// img area
area.slide = image.cols * image.rows;
area.pics = area.slide - countNonZero(image_minus_text);
for( auto &block : text_blocks )
area.text += block.coord.area();
#if SLIDE_PROC_DEBUG == 1
imshow("pic area" + to_string(area.pics),image_minus_text);
waitKey();
#endif
}