Ask Your Question
0

How to convert dashed lines to solid?

asked 2020-04-01 20:55:42 -0600

tenkun van gravatar image

I have a table with horizontal and vertical lines.Some lines are not solid because of poor image quality. This jeopardizes my approach to extract horizontal and vertical lines with line filter. Maybe I should convert dashed lines to solid lines first, then follow some procedures like erode and dilate, but I don't kown how, any advice? image description

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2020-04-04 12:56:16 -0600

supra56 gravatar image

You're using roi screenshot. Unfortunately, I cannot go any further. Fortunately, if you have whole image, I will have to work around.

import cv2 as cv
import numpy as np

def contoursConvexHull(contours):
    pts = []
    for i in range(0, len(contours)):
        for j in range(0, len(contours[i])):
            pts.append(contours[i][j])

    pts = np.array(pts)
    result = cv.convexHull(pts)
    return result


image = cv.imread('dash_line.png')
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (3, 35), 0)
thresh = cv.threshold(blurred, 65, 65, cv.THRESH_BINARY)[0]
imageCanny = cv.Canny(blurred, 0, 100, 0)

contours, hierarchy = cv.findContours(imageCanny, cv.RETR_TREE,
                                      cv.CHAIN_APPROX_NONE)

ConvexHullPoints = contoursConvexHull(contours)
cv.polylines(image, [ConvexHullPoints], True, (0, 0, 0), 4)
cv.imwrite('solid_line.png', image)
cv.imshow('Image', image)
cv.waitKey(0)

Output:

image description

edit flag offensive delete link more
1

answered 2020-04-02 10:12:17 -0600

HagymaGyilkos gravatar image

You almost answered your question: dilating, then eroding is a suitable solution for fixing such continuity errors. If the rate of dilation, and erosion is same, the image information should remain, but incontinouitiy will be reduced.

Since you don't specyfy any platform, I assume c++. My example is super minimalistic, read the official tutorial for more details:

cv::Mat image_to_process;
//it will have a 3 pixel wide effect, since kernel is Mat(), see tutorial for more
cv::dilate(image_to_process, image_to_process, cv::Mat());
cv::erode(image_to_process, image_to_process, cv::Mat());
//done

Also simple median blur,or gaussian blur can help since the incontinuity can decsribed as high frequency noise, but it may suffer from other problems.

edit flag offensive delete link more

Comments

thank you for your help. Maybe i just simplified my question, because after dilating and eroding, dense text in table will be a chunk of pixels. Since I use a filter to detect lines, I detect "lines" in this chunk. By the way, what I really need is the position of each cell in table, for further work like OCR, and I am trying to use some build-in functions like findContour() to get position information.

tenkun van gravatar imagetenkun van ( 2020-04-08 04:49:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-01 20:55:42 -0600

Seen: 4,115 times

Last updated: Apr 04 '20