Ask Your Question
0

Line detection with OpenCV Python and Hough transform

asked 2018-10-15 06:54:24 -0600

bohdan.inhliziian gravatar image

updated 2018-10-15 07:03:24 -0600

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 Canne edge detection

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!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-10-17 06:17:08 -0600

supra56 gravatar image

@bohdan.inhliziian . I am not candidate to answer under moderator berak.

#!usr/bin/env python
#OpenCV 4.0
#using rapberry pi 3, linux, kernel 4.14.76

import cv2
import numpy as np

img = cv2.imread('image1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 150, apertureSize=3)

minLineLength =2
maxLineGap = 4
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 350, minLineLength, maxLineGap, 10)
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)
cv2.imshow("image", edges)
cv2.imshow('img', img)
cv2.waitKey(0)

image description

edit flag offensive delete link more

Comments

Thank you so much! I am confused of it, mistake in argument maxLineGap.

bohdan.inhliziian gravatar imagebohdan.inhliziian ( 2018-10-17 06:27:10 -0600 )edit
1

@supra56 -- you're hereby authorized to answer questions ;)

(was there any trouble ? i don't recall !)

berak gravatar imageberak ( 2018-10-17 06:29:19 -0600 )edit
1

@ohdan.inhliziian U left out parameter in cv2.HoughLinesP

supra56 gravatar imagesupra56 ( 2018-10-17 06:48:36 -0600 )edit

@supra56 -- Welcome back. :D

sjhalayka gravatar imagesjhalayka ( 2018-10-17 08:42:08 -0600 )edit

@sjhalayka. U're welcome.

supra56 gravatar imagesupra56 ( 2018-10-17 10:09:24 -0600 )edit
1

As Line Segment Detector has been removed due to license conflict, I suggest you all add a thumbs-up to this issue! "Restore LineSegmentDetector LSD & avoid license conflict": https://github.com/opencv/opencv_cont...

ruelj2 gravatar imageruelj2 ( 2020-05-09 14:31:23 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-10-15 06:54:24 -0600

Seen: 7,830 times

Last updated: Oct 17 '18