Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hough Line Transform identifies only one line even though image contains many lines in OpenCV in Python

I used the Laplacian transform in OpenCV for edge detection and then used Hough Line Transform for detecting lines in it. These identified lines need to eventually removed from the image.

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('Feb_16-0.jpg',0)
kernel = np.ones((1,1),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blur = cv2.GaussianBlur(opening,(1,1),0)
ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 
laplacian = cv2.Laplacian(th4,cv2.CV_8UC1)
cst = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(laplacian,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(cst,(x1,y1),(x2,y2),(0,255,0),2)

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

I expect to identify all the lines in the Bill: Internet Bill

The results of the laplacian edge detection is as follows:

The edge detection results

whereas the results returned by Hough Line Transform identifies only one line as marked by the green line in the below image: Transformed Internet Bill

Could anyone help me figure out what modifications in code would be required so that all the bold horizontal/vertical lines of the Internet Bill could be identified?

like image 246
Anindita Bhowmik Avatar asked Jul 12 '16 09:07

Anindita Bhowmik


People also ask

What are the limitation of Hough transform?

Limitations. The Hough transform is only efficient if a high number of votes fall in the right bin, so that the bin can be easily detected amid the background noise. This means that the bin must not be too small, or else some votes will fall in the neighboring bins, thus reducing the visibility of the main bin.

For what purpose Hough transform is used explain the Hough transform with example?

The Hough transform (HT) can be used to detect lines circles or • The Hough transform (HT) can be used to detect lines, circles or other parametric curves. It was introduced in 1962 (Hough 1962) and first used to find lines in images a decade later (Duda 1972). The goal is to find the location of lines in images.

What is Hough transformation describe the technique of Hough transformation?

The Hough transform is a popular feature extraction technique that converts an image from Cartesian to polar coordinates. Any point within the image space is represented by a sinusoidal curve in the Hough space.

What are Hough lines Opencv?

The Hough Transform is a method that is used in image processing to detect any shape, if that shape can be represented in mathematical form. It can detect the shape even if it is broken or distorted a little bit.


1 Answers

It seems to me that you are only reading the first element of "lines" in:

for x1,y1,x2,y2 in lines[0]:
    cv2.line(cst,(x1,y1),(x2,y2),(0,255,0),2)

Thus, you are only drawing the first (most significant) line, that has been found ( the longest, most thick). I suggest you to try:

for line in lines:    
    for x1,y1,x2,y2 in line:
         cv2.line(cst,(x1,y1),(x2,y2),(0,255,0),2)

Further, I recommend to set minLineLength lower, if you still do not get the correct results.

like image 198
Nikolas Rieble Avatar answered Sep 29 '22 08:09

Nikolas Rieble