SHARE
TWEET

Untitled

a guest Jul 18th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import numpy as np
  3.  
  4. def make_points(image, line_parameters):
  5.     slope, intercept = line_parameters
  6.     y1 = int(image.shape[0])
  7.     y2 = int(y1*3/5)
  8.     x1 = int((y1 - intercept)/slope)
  9.     x2 = int((y2 - intercept)/slope)
  10.     return [[x1, y1, x2, y2]]
  11.  
  12. def average_slope_intercept(image, lines):
  13.     left_fit = []
  14.     right_fit = []
  15.     if lines is None:
  16.         return None
  17.     for line in lines:
  18.         x1, y1, x2, y2 = line.reshape(4)
  19.         parameters = np.polyfit((x1, x2), (y1, y2), 1)
  20.         slope = parameters[0]
  21.         intercept = parameters[1]
  22.         if slope < 0:
  23.             left_fit.append((slope, intercept))
  24.         else:
  25.             right_fit.append((slope, intercept))
  26.     left_fit_average = np.average(left_fit, axis = 0)
  27.     right_fit_average = np.average(right_fit, axis = 0)
  28.     left_line = make_points(image, left_fit_average)
  29.     right_line = make_points(image, right_fit_average)
  30.     return np.array((left_line, right_line))
  31.  
  32.  
  33. def CannyEdge(image):
  34.   gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  35.   blur = cv2.GaussianBlur(gray, (5,5), 0)
  36.   cannyImage = cv2.Canny(blur, 50, 150)
  37.   return cannyImage
  38.  
  39. def region_of_interest(image):
  40.     height = image.shape[0]
  41.     triangle = np.array([[(200, height),(550, 250),(1100, height),]], np.int32)
  42.     mask = np.zeros_like(image)
  43.     cv2.fillPoly(mask, triangle, 255)
  44.     masked_image = cv2.bitwise_and(image, mask)
  45.     return masked_image
  46.  
  47. def display_lines(image, lines):
  48.     line_image = np.zeros_like(image)
  49.     if lines is not None:
  50.         for line in lines:
  51.             x1, y1, x2, y2 = line.reshape(4)
  52.             cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10)
  53.     return line_image
  54.  
  55.  
  56.  
  57. cap = cv2.VideoCapture("test.mp4")
  58. while(cap.isOpened()):
  59.     _, frame = cap.read()
  60.     canny = CannyEdge(frame)
  61.     cropped_Image = region_of_interest(canny)
  62.     rho = 2
  63.     theta = np.pi/180
  64.     threshold = 100
  65.     lines = cv2.HoughLinesP(cropped_Image,rho, theta, threshold, np.array ([ ]), minLineLength=40, maxLineGap=5)
  66.     averaged_lines = average_slope_intercept(frame, lines)
  67.     line_image = display_lines(frame, averaged_lines)
  68.     combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
  69.     cv2.imshow("Image", combo_image)
  70.     if cv2.waitKey(1) & 0xFF == ord('q'):
  71.         break
  72. cap.release()
  73. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top