Problem in line detection with hough transform

asked 2017-02-06 05:57:51 -0500

RajatGupta gravatar image

updated 2017-02-06 07:04:51 -0500

I am trying to detect all the vertical lines and horizontal lines in an image . I have been able to detect all the horizontal lines successfully . But having an issue with vertical lines detection. (My first time on Opencv so don't have enough credits to upload image here ) so posting imgur link Sorry for this .

Original Image :- imgur.com/a/mZJny

After canny Edge detection

edges :- imgur.com/a/hkrve

output

o/p :- imgur.com/a/kdKwv

As you can see i have been able to detect all edges perfectly but when i am trying to detect lines with hough transform , haven't been able to do so .

Code :-

to detect vertical lines

    img = cv2.imread("sample.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


bw = cv2.adaptiveThreshold( ~gray,255,cv2.cv.CV_ADAPTIVE_THRESH_MEAN_C,        cv2.cv.CV_THRESH_BINARY,15,-2)  

vertical = np.copy(bw)
shape = 45
verticalSize = vertical.shape[0]/shape
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalSize/2))
vertical = cv2.erode(vertical, verticalStructure)
cv2.imwrite("vertical.png", vertical)
vertical = cv2.dilate(vertical, verticalStructure)
not_vertical = cv2.bitwise_not(vertical)
cv2.imwrite("vertical.png", vertical)
edges = cv2.Canny(not_vertical,50,150,apertureSize = 3)
cv2.imwrite("edges1.png", edges)
minLineLength = 4
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
edit retag flag offensive close merge delete