Skip to content

Instantly share code, notes, and snippets.

@fjolublar
Created July 28, 2020 15:00
Show Gist options
  • Save fjolublar/de75cd7dfeefd2ccced8106b946032ea to your computer and use it in GitHub Desktop.
Save fjolublar/de75cd7dfeefd2ccced8106b946032ea to your computer and use it in GitHub Desktop.
Finding lines in frames in OpenCV
def find_lines(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5,5),0)
# ret, gray = cv2.threshold(gray,190,255,cv2.THRESH_BINARY_INV)
edges = cv2.Canny(gray, 30, 150, apertureSize = 3)
# edges = cv2.Canny(gray, 50, 200, apertureSize = 3)
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)
if lines is not None:
for line in lines:
# for rho,theta in lines[1]:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(frame, (x1,y1), (x2,y2), (0,0,255), 2)
# lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)
# if lines is not None:
# for line in lines:
# x1, y1, x2, y2 = line[0]
# cv2.line(frame, (x1, y1), (x2, y2), (255, 0, 0), 3)
frame = cv2.flip(frame, 1)
return frame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment