Wednesday 24 March 2021

opencv 23 hough transform | line detection


hough transform is applied after canny edge detection, lines are detected

canny edge


#main.py
import numpy as np
import cv2

cap = cv2.VideoCapture("assets/Tokyo night drive.mp4")

while True:
    ret, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    edges = cv2.Canny(gray, 254, 255, apertureSize=3)


    lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)

    if not lines is None:
        for line in lines:
            x1, y1, x2, y2 = line[0]
            cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 255), 5)

    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)
    cv2.imshow('canny edge', edges)

    if cv2.waitKey(1) == ord('q'):
        break

    if cv2.waitKey(1) == ord('p'):
        # wait until any key is pressed
        cv2.waitKey(-1)

cap.release()
cv2.destroyAllWindows()

reference:

No comments:

Post a Comment