diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..9cb3539 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "${workspaceFolder}/**", + "/usr/local/Cellar/opencv/3.4.1_5/include/opencv" + ], + "defines": [], + "macFrameworkPath": [ + "/System/Library/Frameworks", + "/Library/Frameworks" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4bbfe3c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense para saber los atributos posibles. + // Mantenga el puntero para ver las descripciones de los existentes atributos + // Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "imageSegmentation", + "type": "cppdbg", + "request": "launch", + "program": "enter program name, for example ${workspaceFolder}/a.out", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": true, + "MIMode": "lldb" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2b32f0d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,37 @@ +{ + "files.associations": { + "__functional_base": "cpp", + "array": "cpp", + "istream": "cpp", + "locale": "cpp", + "memory": "cpp", + "tuple": "cpp", + "utility": "cpp", + "iosfwd": "cpp", + "ostream": "cpp", + "__config": "cpp", + "__nullptr": "cpp", + "cstddef": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "new": "cpp", + "stdexcept": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "algorithm": "cpp", + "__locale": "cpp", + "string_view": "cpp", + "iterator": "cpp", + "vector": "cpp", + "__split_buffer": "cpp", + "deque": "cpp", + "string": "cpp", + "iostream": "cpp", + "chrono": "cpp", + "limits": "cpp", + "ratio": "cpp", + "__tree": "cpp", + "bitset": "cpp", + "map": "cpp" + } +} \ No newline at end of file diff --git a/img/out/Binary_image.jpg b/img/out/Binary_image.jpg new file mode 100644 index 0000000..34b7138 Binary files /dev/null and b/img/out/Binary_image.jpg differ diff --git a/img/out/Chunk0.jpg b/img/out/Chunk0.jpg new file mode 100644 index 0000000..2a99944 Binary files /dev/null and b/img/out/Chunk0.jpg differ diff --git a/img/out/Chunk1.jpg b/img/out/Chunk1.jpg new file mode 100644 index 0000000..9ffb495 Binary files /dev/null and b/img/out/Chunk1.jpg differ diff --git a/img/out/Chunk10.jpg b/img/out/Chunk10.jpg new file mode 100644 index 0000000..f6c708b Binary files /dev/null and b/img/out/Chunk10.jpg differ diff --git a/img/out/Chunk11.jpg b/img/out/Chunk11.jpg new file mode 100644 index 0000000..da4779e Binary files /dev/null and b/img/out/Chunk11.jpg differ diff --git a/img/out/Chunk12.jpg b/img/out/Chunk12.jpg new file mode 100644 index 0000000..df4f038 Binary files /dev/null and b/img/out/Chunk12.jpg differ diff --git a/img/out/Chunk13.jpg b/img/out/Chunk13.jpg new file mode 100644 index 0000000..cad00f6 Binary files /dev/null and b/img/out/Chunk13.jpg differ diff --git a/img/out/Chunk14.jpg b/img/out/Chunk14.jpg new file mode 100644 index 0000000..b62bea2 Binary files /dev/null and b/img/out/Chunk14.jpg differ diff --git a/img/out/Chunk15.jpg b/img/out/Chunk15.jpg new file mode 100644 index 0000000..cfdc10b Binary files /dev/null and b/img/out/Chunk15.jpg differ diff --git a/img/out/Chunk16.jpg b/img/out/Chunk16.jpg new file mode 100644 index 0000000..4d4ab47 Binary files /dev/null and b/img/out/Chunk16.jpg differ diff --git a/img/out/Chunk17.jpg b/img/out/Chunk17.jpg new file mode 100644 index 0000000..4d4ab47 Binary files /dev/null and b/img/out/Chunk17.jpg differ diff --git a/img/out/Chunk18.jpg b/img/out/Chunk18.jpg new file mode 100644 index 0000000..4d4ab47 Binary files /dev/null and b/img/out/Chunk18.jpg differ diff --git a/img/out/Chunk19.jpg b/img/out/Chunk19.jpg new file mode 100644 index 0000000..4d4ab47 Binary files /dev/null and b/img/out/Chunk19.jpg differ diff --git a/img/out/Chunk2.jpg b/img/out/Chunk2.jpg new file mode 100644 index 0000000..ca10781 Binary files /dev/null and b/img/out/Chunk2.jpg differ diff --git a/img/out/Chunk3.jpg b/img/out/Chunk3.jpg new file mode 100644 index 0000000..ae4a0eb Binary files /dev/null and b/img/out/Chunk3.jpg differ diff --git a/img/out/Chunk4.jpg b/img/out/Chunk4.jpg new file mode 100644 index 0000000..45aad40 Binary files /dev/null and b/img/out/Chunk4.jpg differ diff --git a/img/out/Chunk5.jpg b/img/out/Chunk5.jpg new file mode 100644 index 0000000..4ddc4a8 Binary files /dev/null and b/img/out/Chunk5.jpg differ diff --git a/img/out/Chunk6.jpg b/img/out/Chunk6.jpg new file mode 100644 index 0000000..12ae7a6 Binary files /dev/null and b/img/out/Chunk6.jpg differ diff --git a/img/out/Chunk7.jpg b/img/out/Chunk7.jpg new file mode 100644 index 0000000..61701ca Binary files /dev/null and b/img/out/Chunk7.jpg differ diff --git a/img/out/Chunk8.jpg b/img/out/Chunk8.jpg new file mode 100644 index 0000000..360fb3f Binary files /dev/null and b/img/out/Chunk8.jpg differ diff --git a/img/out/Chunk9.jpg b/img/out/Chunk9.jpg new file mode 100644 index 0000000..23617f3 Binary files /dev/null and b/img/out/Chunk9.jpg differ diff --git a/img/out/Copia de labels.bmp b/img/out/Copia de labels.bmp new file mode 100644 index 0000000..08676ee Binary files /dev/null and b/img/out/Copia de labels.bmp differ diff --git a/img/out/Final_Lines.bmp b/img/out/Final_Lines.bmp new file mode 100644 index 0000000..0921ebe Binary files /dev/null and b/img/out/Final_Lines.bmp differ diff --git a/img/out/Initial_Lines.jpg b/img/out/Initial_Lines.jpg new file mode 100644 index 0000000..73d587b Binary files /dev/null and b/img/out/Initial_Lines.jpg differ diff --git a/img/out/Line_0.jpg b/img/out/Line_0.jpg new file mode 100644 index 0000000..412a60a Binary files /dev/null and b/img/out/Line_0.jpg differ diff --git a/img/out/Line_1.jpg b/img/out/Line_1.jpg new file mode 100644 index 0000000..60e7d48 Binary files /dev/null and b/img/out/Line_1.jpg differ diff --git a/img/out/Line_10.jpg b/img/out/Line_10.jpg new file mode 100644 index 0000000..6e207f2 Binary files /dev/null and b/img/out/Line_10.jpg differ diff --git a/img/out/Line_11.jpg b/img/out/Line_11.jpg new file mode 100644 index 0000000..cd3ac49 Binary files /dev/null and b/img/out/Line_11.jpg differ diff --git a/img/out/Line_2.jpg b/img/out/Line_2.jpg new file mode 100644 index 0000000..ecb4a7d Binary files /dev/null and b/img/out/Line_2.jpg differ diff --git a/img/out/Line_3.jpg b/img/out/Line_3.jpg new file mode 100644 index 0000000..c8dcad5 Binary files /dev/null and b/img/out/Line_3.jpg differ diff --git a/img/out/Line_4.jpg b/img/out/Line_4.jpg new file mode 100644 index 0000000..c25f9ed Binary files /dev/null and b/img/out/Line_4.jpg differ diff --git a/img/out/Line_5.jpg b/img/out/Line_5.jpg new file mode 100644 index 0000000..e9c4159 Binary files /dev/null and b/img/out/Line_5.jpg differ diff --git a/img/out/Line_6.jpg b/img/out/Line_6.jpg new file mode 100644 index 0000000..8f5e5a4 Binary files /dev/null and b/img/out/Line_6.jpg differ diff --git a/img/out/Line_7.jpg b/img/out/Line_7.jpg new file mode 100644 index 0000000..e92147d Binary files /dev/null and b/img/out/Line_7.jpg differ diff --git a/img/out/Line_8.jpg b/img/out/Line_8.jpg new file mode 100644 index 0000000..bb284f4 Binary files /dev/null and b/img/out/Line_8.jpg differ diff --git a/img/out/Line_9.jpg b/img/out/Line_9.jpg new file mode 100644 index 0000000..d3b1713 Binary files /dev/null and b/img/out/Line_9.jpg differ diff --git a/img/out/contours.jpg b/img/out/contours.jpg new file mode 100644 index 0000000..65f6473 Binary files /dev/null and b/img/out/contours.jpg differ diff --git a/img/out/labels.bmp b/img/out/labels.bmp new file mode 100644 index 0000000..edcd39f Binary files /dev/null and b/img/out/labels.bmp differ diff --git a/src/LineSegmentation.cpp b/src/LineSegmentation.cpp index 5034bcd..5ab9ca8 100755 --- a/src/LineSegmentation.cpp +++ b/src/LineSegmentation.cpp @@ -1,8 +1,8 @@ #include "LineSegmentation.hpp" -LineSegmentation::LineSegmentation(string path_of_image) { +LineSegmentation::LineSegmentation(string path_of_image, string out) { this->image_path = path_of_image; - + this->OUT_PATH = out; // Initialize Sieve. sieve(); } @@ -23,7 +23,7 @@ LineSegmentation::pre_process_image() { // OTSU threshold and Binarization. cv::threshold(smoothed_img, binary_img, 0.0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); - imwrite("Binary_image.jpg", this->binary_img); + imwrite(OUT_PATH+"Binary_image.jpg", this->binary_img); } void @@ -78,7 +78,7 @@ LineSegmentation::find_contours() { // ToDo @Samir55 Remove. for (size_t i = 0; i < merged_rectangles.size(); i++) rectangle(drawing, merged_rectangles[i].tl(), merged_rectangles[i].br(), TEST_LINE_COLOR, 2, 8, 0); - cv::imwrite("contours.jpg", drawing); + cv::imwrite(OUT_PATH+"contours.jpg", drawing); this->contours = merged_rectangles; } @@ -95,7 +95,7 @@ LineSegmentation::generate_chunks() { chunk_width))); this->chunks.push_back(c); - imwrite("Chunk" + to_string(i_chunk) + ".jpg", this->chunks.back()->img); + imwrite(OUT_PATH+"Chunk" + to_string(i_chunk) + ".jpg", this->chunks.back()->img); start_pixel += chunk_width; } @@ -173,28 +173,76 @@ LineSegmentation::save_image_with_lines(string path) { for (auto line : initial_lines) { int last_row = -1; - + //std::cout<<"new line"<points) { img_clone.at(point.x, point.y) = TEST_LINE_COLOR; - + //std::cout<<" ii="+std::to_string(point.y)+" jj="+std::to_string(point.x)<(i, point.y) = TEST_LINE_COLOR; + //std::cout<<" i="+std::to_string(point.y)+" j="+std::to_string(i)< &lines, string path) { +LineSegmentation::save_lines_to_file(const vector &lines) { int idx = 0; for (auto m : lines) { - imwrite(path + "Line " + to_string(idx++) + ".jpg", m); + imwrite(OUT_PATH + "Line_" + to_string(idx++) + ".jpg", m); + } +} + +void +LineSegmentation::labelImage(string path) { + cv::Mat img_clone = this->color_img.clone(); + vector pointactualLine(initial_lines[0]->points.size()); + vector pointnextLine; + //initialize line at 0,0 + for(int i = 0; ipoints; + + this->labelComponent(pointnextLine, pointactualLine, img_clone); + + pointactualLine = pointnextLine; + } + + //for label last text line + for(int i = 0; icolor_img.cols, 0); + + this->labelComponent(pointnextLine, pointactualLine, img_clone); + cv::imwrite(path, img_clone); +} + +void +LineSegmentation::labelComponent(const std::vector &pointnextLine, const vector &pointactualLine, cv::Mat &img_clone){ + //for random color generation + int max = 220; + int min = 30; + cv::Vec3b randomColor = cv::Vec3b(rand() % max + min,rand() % max + min, redStart); + + for (int indexPoint = 0; indexPoint < pointactualLine.size(); indexPoint++) + { + auto nextPoint = pointnextLine [indexPoint]; + auto point = pointactualLine[indexPoint]; + + for(int x_ = point.x; x_(x_, indexPoint)[2]<125) + img_clone.at(x_, indexPoint) = randomColor; + } + redStart+=5; } } @@ -228,7 +276,6 @@ LineSegmentation::generate_regions() { if (bottom_line != nullptr) bottom_line->above = r; - if (!res) { this->line_regions.push_back(r); if (r->height < this->predicted_line_height * 2.5) @@ -247,12 +294,10 @@ LineSegmentation::repair_lines() { // Loop over the regions. for (Line *line : initial_lines) { map column_processed = map(); - for (int i = 0; i < line->points.size(); i++) { Point &point = line->points[i]; int x = (line->points[i]).x, y = (line->points[i]).y; - // Check for vertical line intersection // In lines, we don't save all the vertical points we save only the start point and the end point. // So in line->points all we save is the horizontal points so, we know there exists a vertical line by @@ -281,7 +326,6 @@ LineSegmentation::repair_lines() { // Mark column as processed. column_processed[y] = true; - for (auto contour : this->contours) { // Check line & contour intersection if (y >= contour.tl().x && y <= contour.br().x && x >= contour.tl().y && x <= contour.br().y) { @@ -299,7 +343,6 @@ LineSegmentation::repair_lines() { new_row = contour.br().y; line->max_row_position = max(new_row, line->max_row_position); } - for (int k = contour.tl().x; k < contour.tl().x + contour.width; k++) { line->points[k].x = new_row; } @@ -368,7 +411,7 @@ LineSegmentation::segment() { // Get initial lines. this->get_initial_lines(); - this->save_image_with_lines("Initial_Lines.jpg"); + this->save_image_with_lines(OUT_PATH+"Initial_Lines.jpg"); // Get initial line regions. this->generate_regions(); @@ -379,7 +422,10 @@ LineSegmentation::segment() { // Generate the final line regions. this->generate_regions(); - this->save_image_with_lines("Final_Lines.jpg"); + this->save_image_with_lines(OUT_PATH+"Final_Lines.bmp"); + + //is neccesary to use bitmap or tiff for componente labeling + this->labelImage(OUT_PATH+"labels.bmp"); return this->get_regions(); } diff --git a/src/LineSegmentation.hpp b/src/LineSegmentation.hpp index a28d25a..a8d9808 100755 --- a/src/LineSegmentation.hpp +++ b/src/LineSegmentation.hpp @@ -15,10 +15,10 @@ #ifndef M_PI #define M_PI 3.14159265358979323846 #endif - +//In the paper is explained that every chunk have a 5% of the width #define CHUNKS_NUMBER 20 #define CHUNKS_TO_BE_PROCESSED 5 -#define TEST_LINE_COLOR cv::Vec3b(255, 0, 255) // Magenta color. +#define TEST_LINE_COLOR cv::Vec3b(0, 0, 255) // Magenta color. typedef int valley_id; @@ -30,7 +30,7 @@ class LineSegmentation; class Region; class Valley; - +extern string OUT_PATH; /// A class representing the separator between line regions. class Line { friend class LineSegmentation; @@ -213,8 +213,11 @@ class LineSegmentation { void addPrimesToVector(int, vector &); + //first color index for identify regions from up to down + int redStart = 20; + public: - LineSegmentation(string path_of_image); + LineSegmentation(string path_of_image, string out); /// Generate the lines found in the saved image. /// \return vector a vector containing each line as a 2D mat. @@ -224,9 +227,18 @@ class LineSegmentation { /// Save current line regions returned from get_regions() or segment() functions in jpg files. /// \param lines /// \param path - void save_lines_to_file(const vector &lines, string path); + void save_lines_to_file(const vector &lines); + + //document image labelling + void labelImage(string path); + //label a section of the image from y-pointactualLine to y1 pointnextLine + void + labelComponent(const vector &pointnextLine, const vector &pointactualLine, cv::Mat &img_clone); + + private: + string OUT_PATH; string image_path; ///< Path of the image. cv::Mat color_img; diff --git a/src/main.cpp b/src/main.cpp index e0096f5..f387218 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,11 +2,12 @@ int main(int argc, char *argv[]) { cout << argv[1] << endl; + cout << argv[2] << endl; + string img_path = argv[1]; - LineSegmentation line_segmentation(img_path); + LineSegmentation line_segmentation(img_path, argv[2]); vector lines = line_segmentation.segment(); - // Save lines to path. - line_segmentation.save_lines_to_file(lines, ""); + line_segmentation.save_lines_to_file(lines); return 0; } \ No newline at end of file