-
Notifications
You must be signed in to change notification settings - Fork 5
/
ParagraphDetection.cpp
203 lines (142 loc) · 5.53 KB
/
ParagraphDetection.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
//~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*
// Project : DRD
// Author : Soumyadeep Dey soumyadeepdey@gmail.com
// Creation Date : NOV 10 -2014. Rights Reserved
//~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*~^~*
#include "myheader.h"
vector<nocc> newparagraph;
int npar;
Mat src;
int binary_threshold_value;
RNG rng(12345);
/*-------------------------------------------------------------------------------------------------------------------------------------------*/
/**
* @function readme
*/
void readme()
{ std::cout << " Usage: ./para <input.xml> <output.xml> <output_image_name>" << std::endl; }
/*-------------------------------------------------MAIN--------------------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
if( argc != 4 )
{ readme(); return -1; }
char *input_image_name;
input_image_name = (char *)malloc(4000*sizeof(char));
IITKGP_XML_IO::XML_IO xml;
input_image_name = xml.readXML(argv[1]);
printf("\nInput image name %s\n",input_image_name);
src=imread(input_image_name,1);
IITkgp_functions::BinaryImage bin;
binary_threshold_value = 120;
Mat binary_dst;
binary_dst = bin.binarization(src,4);
//imwrite("BinaryImage.jpg",binary_dst);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat temp_img;
IITkgp_functions::ImageProcFunc imgproc;
binary_dst.copyTo(temp_img);
temp_img = imgproc.FindImageInverse(temp_img);
/// Find contours
findContours( temp_img, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Approximate contours to polygons + get bounding rects and circles
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
for( int j = 0; j < contours.size(); j++ )
{
approxPolyDP( Mat(contours[j]), contours_poly[j], 3, true );
boundRect[j] = boundingRect( Mat(contours_poly[j]) );
}
/**
* @Var initial_contour_flag
* @brief size equal to number of original Contour from eroded image
* @brief flag value is 1 if parent contour
* flag value is 0 if child contour
* flag value is 2 if contour are large_block(parent)
* flag value is 3 if contour ae small and suppose to be noise
* */
int *initial_contour_flag;
initial_contour_flag = (int *)malloc(contours.size()*sizeof(int));
for(int j = 0; j < contours.size(); j++ )
initial_contour_flag[j]=1;
Mat drawing;
src.copyTo(drawing);
int avg_height = 0;
int number_of_block = 0;
for( int j = 0; j< contours.size(); j++ )
{
if(hierarchy[j][3] == -1)
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
rectangle( drawing, boundRect[j].tl(), boundRect[j].br(), color, 6, 8, 0 );
Rect tmpr = boundRect[j];
avg_height = avg_height + tmpr.height;
number_of_block = number_of_block + 1;
}
else
initial_contour_flag[j] = 0;
}
avg_height = avg_height/number_of_block;
printf("Average Height = %d\n",avg_height);
drawing.release();
//imwrite(argv[3], drawing);
IITkgp_functions::SmoothingGapfilling sg;
Mat horizgapimage = sg.horizontal_gapfilling(binary_dst,avg_height*2);
Mat verigapimage = sg.vertical_gapfilling(horizgapimage,avg_height*2);
temp_img.release();
verigapimage.copyTo(temp_img);
verigapimage.release();
temp_img = imgproc.FindImageInverse(temp_img);
horizgapimage.release();
contours.clear();
hierarchy.clear();
/// Find contours
findContours( temp_img, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
temp_img.release();
contours_poly.clear();
boundRect.clear();
contours_poly.resize(contours.size());
boundRect.resize(contours.size());
for( int j = 0; j < contours.size(); j++ )
{
approxPolyDP( Mat(contours[j]), contours_poly[j], 3, true );
boundRect[j] = boundingRect( Mat(contours_poly[j]) );
}
src.copyTo(drawing);
int pageframexmax = 0;
int pageframeymax = 0;
int pageframexmin = src.cols;
int pageframeymin = src.rows;
for( int j = 0; j< contours.size(); j++ )
{
if(hierarchy[j][3] == -1)
{
//Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// rectangle( drawing, boundRect[j].tl(), boundRect[j].br(), color, 6, 8, 0 );
//page frame
nocc tempcc;
tempcc.xmin = boundRect[j].x;
tempcc.ymin = boundRect[j].y;
tempcc.xmax = boundRect[j].x+boundRect[j].width;
tempcc.ymax = boundRect[j].y+boundRect[j].height;
newparagraph.push_back(tempcc);
if(pageframexmin > boundRect[j].x)
pageframexmin = boundRect[j].x;
if(pageframeymin > boundRect[j].y)
pageframeymin = boundRect[j].y;
if(pageframexmax < boundRect[j].x+boundRect[j].width)
pageframexmax = boundRect[j].x+boundRect[j].width;
if(pageframeymax < boundRect[j].y+boundRect[j].height)
pageframeymax = boundRect[j].y+boundRect[j].height;
}
}
npar = newparagraph.size();
// rectangle( drawing, Point(pageframexmin,pageframeymin), Point(pageframexmax,pageframeymax), Scalar(0,0,255), 6, 8, 0 );
imwrite(argv[3], drawing);
drawing.release();
char *xmlchild;
xmlchild = "TextBlock";
xml.writemyxml(argv[1],argv[2],xmlchild,input_image_name,argv[3],"NULL", "NULL", pageframexmin, pageframeymin, pageframexmax, pageframeymax);
printf("\n");
return 0;
}