How to detect only vertical edges from the image?

Question:

I have the following image:

Original Image:

Original Image

I want to detect the 3 vertical edges shown in red in the below image:

Desired output:

Desired output

I’ve tried the following:

#green is the original image
gray = cv2.cvtColor(green, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray green" ,gray)

ret, thresh1 = cv2.threshold(gray, 140, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("140 Thresh", thresh1)

edges = cv2.Canny(thresh1,100,250,apertureSize = 7)
cv2.imshow("Edges", edges)

The canny edge detector shows this output:

Canny result:

Canny result

I’ve tried using sobel vertical detection as follows:

 sobel_vertical = cv2.Sobel(thresh1, cv2.CV_64F, 1, 0, ksize=7)
 cv2.imshow("Vertical", sobel_vertical)

Here’s the sobel vertical output:

Sobel Vertical output

Sobel Vertical output

Following this

this

as an answer, I tried the same code, as follows:

minLineLength=100

lines = cv2.HoughLinesP(image=edges,rho=1.4,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=10)

a,b,c = lines.shape

for i in range(a):
    cv2.line(green, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 1, cv2.LINE_AA)

cv2.imshow("Lines", green)

Which gives me this output:

Houghlines output:

Houghlines output

Can anyone suggest to me what I am doing wrong, or what I can do to get the desired output, or close to that? I don’t want horizontal edges to be detected at all. Thank you!

Asked By: Lin

||

Answers:

For the lines you are getting from the Houghlines output, you can calculate the slope or inclination of each line and you can apply a condition where you display only those lines which have their slopes in a given bound say 0 to 90 degrees

Answered By: Atharva Gundawar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.