Skip to content

Commit

Permalink
Fixes in reparing lines functions. Improve avg_line_height calculations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir55 committed Apr 6, 2018
1 parent e5e1aa0 commit 4ebda7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
23 changes: 20 additions & 3 deletions src/LineSegmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ LineSegmentation::get_initial_lines() {
valleys_min_abs_dist += avg_height;
}
valleys_min_abs_dist /= number_of_heights;
this->avg_line_height = valleys_min_abs_dist;
cout << "Estimated avg line height " << valleys_min_abs_dist << endl;
this->predicted_line_height = valleys_min_abs_dist;

// Start form the CHUNKS_TO_BE_PROCESSED chunk.
for (int i = CHUNKS_TO_BE_PROCESSED - 1; i >= 0; i--) {
Expand Down Expand Up @@ -209,6 +210,8 @@ LineSegmentation::generate_regions() {
r->update_region(this->binary_img, 0);
this->initial_lines[0]->above = r;
this->line_regions.push_back(r);
if (r->height < this->predicted_line_height * 2.5)
this->avg_line_height += r->height;

// Add rest of regions.
for (int i = 0; i < this->initial_lines.size(); ++i) {
Expand All @@ -226,8 +229,16 @@ LineSegmentation::generate_regions() {
if (bottom_line != nullptr)
bottom_line->above = r;

if (!res)
if (!res) {
this->line_regions.push_back(r);
if (r->height < this->predicted_line_height * 2.5)
this->avg_line_height += r->height;
}
}

if (this->line_regions.size() > 0) {
this->avg_line_height /= this->line_regions.size();
cout << "Avg line height is " << this->avg_line_height << endl;
}
}

Expand Down Expand Up @@ -276,7 +287,7 @@ LineSegmentation::repair_lines() {
if (y >= contour.tl().x && y <= contour.br().x && x >= contour.tl().y && x <= contour.br().y) {

// If contour is longer than the average height ignore.
if (contour.br().y - contour.tl().y > this->avg_line_height * 2.1) continue;
if (contour.br().y - contour.tl().y > this->avg_line_height * 0.9) continue;

bool is_component_above = component_belongs_to_above_region(*line, contour);

Expand Down Expand Up @@ -600,6 +611,7 @@ Valley::comp(const Valley *a, const Valley *b) {
Region::Region(Line *top, Line *bottom) {
this->top = top;
this->bottom = bottom;
this->height = 0;
}

bool
Expand All @@ -618,6 +630,11 @@ Region::update_region(Mat &binary_image, int region_id) {
for (int c = 0; c < binary_image.cols; c++) {
int start = ((top == nullptr) ? 0 : top->points[c].x);
int end = ((bottom == nullptr) ? binary_image.rows - 1 : bottom->points[c].x);

// Calculate region height
if (end > start)
this->height = max(this->height, end - start);

for (int i = start; i < end; i++) {
region.at<uchar>(i - min_region_row, c) = binary_image.at<uchar>(i, c);
}
Expand Down
10 changes: 4 additions & 6 deletions src/LineSegmentation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ class Region {
///< Region top boundaries;
Line *bottom;
///< Region bottom boundaries;
int start_row;
///<
int end_row;
///<
int height;
///< Region height (the maximum distance between the above and bottom line)
int row_offset;
///< The offset of each col to the original image matrix.
cv::Mat covariance;
Expand Down Expand Up @@ -251,8 +249,8 @@ class LineSegmentation {
/// The handwritten components found in the binary image.
int avg_line_height;
///< The average height of lines in the image.
int avg_space_height;
///< The average height of white spaces in the image.
int predicted_line_height;
///< The predicted height of lines in the image.
int chunk_width;
///< width of the chunk.
// ToDo @Samir55 add CHUNKS_TO_BE_PROCESSED when needed.
Expand Down

0 comments on commit 4ebda7c

Please sign in to comment.