Ask Your Question

Revision history [back]

Line detection with OpenCV Python and Hough transform

0 down vote favorite I am trying to detect table lines and extract full table from an image with Python OpenCV and with Hough Transform algorithm. I need to have all coordinates of each line with the aim for draw the same table with same proportions. I understand theory how Hough transform works and tried to implement it without OpenCV, but it is very slow on big images.

Here is the code from example OpenCV Hough Transfrom

import cv2
import numpy as np

img = cv2.imread('image1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow("image", edges)
cv2.waitKey(0)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, minLineLength, maxLineGap)
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.imwrite('houghlines5.jpg', img)

Canny edge detection returned an image Resulf of Canny edge detectione

But the result of detection is Resulf of Housh Transform

I do not know why Hough Transform left some lines of the table. Can you recommend something to do? Maybe another way to extract table from image? Thank you!

Line detection with OpenCV Python and Hough transform

0 down vote favorite I am trying to detect table lines and extract full table from an image with Python OpenCV and with Hough Transform algorithm. I need to have all coordinates of each line with the aim for draw the same table with same proportions. I understand theory how Hough transform works and tried to implement it without OpenCV, but it is very slow on big images.

Here is the code from example OpenCV Hough Transfrom

import cv2
import numpy as np

img = cv2.imread('image1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow("image", edges)
cv2.waitKey(0)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, minLineLength, maxLineGap)
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.imwrite('houghlines5.jpg', img)

Canny edge detection returned an image Resulf of Canny edge detectione

But the result of detection is Resulf of Housh Transform

I do not know why Hough Transform left some lines of the table. Can you recommend something to do? Maybe another way to extract table from image? Thank you!

Line detection with OpenCV Python and Hough transform

I am trying to detect table lines and extract full table from an image with Python OpenCV and with Hough Transform algorithm. I need to have all coordinates of each line with the aim for draw the same table with same proportions. I understand theory how Hough transform works and tried to implement it without OpenCV, but it is very slow on big images.

Here is the code from example OpenCV Hough Transfrom

import cv2
import numpy as np

img = cv2.imread('image1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow("image", edges)
cv2.waitKey(0)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, minLineLength, maxLineGap)
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.imwrite('houghlines5.jpg', img)

Canny edge detection returned an image Resulf of Canny edge detectioneCanne edge detection

But the result of detection is Resulf of Housh TransformResulf of Housh Transform

I do not know why Hough Transform left some lines of the table. Can you recommend something to do? Maybe another way to extract table from image? Thank you!