2

I am new to opencv. I am working with opencv and C++ on visual studio 2013. I have a task to extract all horizontal and vertical lines of a document containing a table separately and use that to extract the cells present in the table.

I can only use morphological operation to achieve that.

Can anyone suggest the procedure to achieve that?

Here is a sample document.

Sample Image

4

1 Answer 1

2

enter image description hereenter image description hereFinally got the output. Look at the code.

string src = "d://sabari//23.jpg";
Mat im = imread(src);
Mat gray;


if (im.channels() == 3)
{
    cvtColor(im, gray, CV_BGR2GRAY);
}
else
{
    gray = im;
}

adaptiveThreshold(~gray, gray, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
Mat vertical = gray.clone();
int horizontalsize = gray.cols / 30;

Mat structure = getStructuringElement(MORPH_RECT, Size(horizontalsize,1));

erode(gray, gray,structure, Point(-1, -1));
dilate(gray, gray,structure, Point(-1, -1));

imshow("ans", gray);
imwrite("d://out2.jpg", gray);

int verticalsize = vertical.rows / 30;

Mat verticalStructure = getStructuringElement(MORPH_RECT, Size( 1,verticalsize));

erode(vertical, vertical, verticalStructure, Point(-1, -1));
dilate(vertical, vertical, verticalStructure, Point(-1, -1));

imshow("ans1", vertical);
imwrite("d://out3.jpg", vertical);
Sign up to request clarification or add additional context in comments.

2 Comments

I got horizontal line separately and vertical lines separately. As i used line as my structuring element, it is able to delete rest elements and i am left with only lines. When i need vertical lines i use structure element "verticalStructure" as mentioned in the code.
Well done, if you show an output image it will be even more helpful to people reading this post in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.