# How to remove the unnecessary line segments from the image?

I want to turn my original image "Original Image" into the target image "Target Image". And I manage to use OpenCV's Hough Transform to remove the line segments. However, the results come out not as expected (Referring to "Resulting image"). From the resulting image, the line segments are not extracted as I want.

I have looked into this web page, but since some line segments are not in a horizontal direction. I find it is unsuitable to apply in my work.

From the resulting image, the left vertical line is divided into two parts. But what I hope is to extract a whole vertical line segments seen in the original image. For the extracted upper line and the skew line segments, they are not shaped as the actual lines in the original image. I wonder how to perfectly remove those lines segments? What is the efficient way to get my work done? Thanks.

My code:

    Mat tempImg,binaryImg, edges;

cvtColor(im, tempImg, CV_BGR2GRAY);

imwrite("binaryImg.png",binaryImg);

Canny(binaryImg, edges, 40, 120, 3);

threshold(edges, edges, 128, 255.0, THRESH_BINARY);

vector<Vec4i> lines;
HoughLinesP(edges, lines, 1, CV_PI/180, 50, 20, 10 );
cv::Mat outImg = cv::Mat::zeros(binaryImg.rows, binaryImg.cols, CV_8UC3);

for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
line( outImg, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,255,255), 3, CV_AA);
}

imwrite("outImg.bmp",outImg);


Original Image

Target Image

Resulting image

I have tried to use OpenCV's inpainting, but I find the inpainting is not perfect enough. Though I have the mask, the resulting image has still a large area of black region on the left hand side. This area is not removed under OpenCV's impainting. I don't really know why the mask doesn't take into effect. I wonder how to make the impainting better.

My code:

Mat tempImg,binaryImg, edges,eroded;

cvtColor(im, tempImg, CV_BGR2GRAY);

imwrite("binaryImg.png",binaryImg);

Canny(binaryImg, edges, 40, 120, 3);

threshold(edges, edges, 128, 255.0, THRESH_BINARY);

vector<Vec4i> lines;
HoughLinesP(edges, lines, 1, CV_PI/180, 50, 20, 10 );
cv::Mat outImg = cv::Mat::zeros(binaryImg.rows, binaryImg.cols, CV_8UC3);

for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
line( outImg, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,255,255), 3, CV_AA);
}

Mat element(10,10,CV_8U,Scalar(1));
dilate(outImg,eroded,element);

std::vector<std::vector<cv::Point> > contours;
cv::findContours(tempImg.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

for (int i = 0; i < contours.size(); i++)
{
cv::Rect r = cv::boundingRect(contours[i]);

if (std::abs(1 - (tempImg.cols/r.width)) > 0.2)
}

inpaint(im, mask, newImg, 1, cv::INPAINT_TELEA ...
edit retag close merge delete

Have you tried Hough to detect lines and then eliminate those lines? I have thought of something like create the mask of the lines and apply inpainting. This should do your expected work.

( 2015-04-23 06:09:59 -0500 )edit

Thank you very much. I will follow your idea to do my work. :) By the way, the resulting image is formed by the Hough transform applied to the original image.

( 2015-04-23 21:31:09 -0500 )edit

I have done the impainting., but the impainting is not well enough as mentioned in my updated question. @thdrksdfthmn

( 2015-04-23 23:07:50 -0500 )edit

Maybe using closing instead of dilate? Inpainting is not very good on thick lines that contain edges...

( 2015-04-24 02:22:32 -0500 )edit

Thanks. I now plan to use counting to remove them.

( 2015-05-03 23:00:18 -0500 )edit