12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import cv2
- import numpy as np
- # define a video capture object
- vid = cv2.VideoCapture(0)
- while(True):
- # Capture the video frame
- # by frame
- ret, frame = vid.read()
- # Convert to grayscale.
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # Apply edge detection method on the image
- edges = cv2.Canny(gray, 50, 150, apertureSize=3)
- # This returns an array of r and theta values
- lines = cv2.HoughLines(edges, 1, np.pi/180, 200)
- # The below for loop runs till r and theta values
- # are in the range of the 2d array
- if lines is not None:
- for r_theta in lines:
- arr = np.array(r_theta[0], dtype=np.float64)
- r, theta = arr
- # Stores the value of cos(theta) in a
- a = np.cos(theta)
- # Stores the value of sin(theta) in b
- b = np.sin(theta)
- # x0 stores the value rcos(theta)
- x0 = a*r
- # y0 stores the value rsin(theta)
- y0 = b*r
- # x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
- x1 = int(x0 + 1000*(-b))
- # y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
- y1 = int(y0 + 1000*(a))
- # x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
- x2 = int(x0 - 1000*(-b))
- # y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
- y2 = int(y0 - 1000*(a))
- # cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
- # (0,0,255) denotes the colour of the line to be
- # drawn. In this case, it is red.
- cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
- # Display the resulting frame
- cv2.imshow('frame', frame)
- # the 'q' button is set as the
- # quitting button you may use any
- # desired button of your choice
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # After the loop release the cap object
- vid.release()
- # Destroy all the windows
- cv2.destroyAllWindows()
|