Horizon Line Detection via Canny and Hough

I am trying to detect horizon line in python. In order to achieve my goal, firstly I used canny edge detection algorithm. sea.jpg

import cv2
import numpy as np

gray = cv2.imread('images/sea.jpg')
edges = cv2.Canny(gray,50,150,apertureSize = 3)

After edge detection, I applied hough transform to image.

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

a,b,c = lines.shape
for i in range(a):
    cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
    cv2.imwrite('images/output.jpg',gray)

cv2.imshow('output',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

But at the end, I output.jpg like this.

What am I doing wrong?


You'll want to filter out lines with a low theta value. I don't think you are filtering anything at the moment. Try making a new list of lines with only lines where y1 == y2, in other words horizontal lines.

链接地址: http://www.djcxy.com/p/79602.html

上一篇: OpenCV Java:从图像中提取卡片

下一篇: 通过Canny和Hough的地平线检测