About

  1. import cv2
  2. import numpy as np
  3.  
  4. def make_coordinates(image, line_parameters):
  5. slope,intercept = line_parameters
  6. y1 = image.shape[0]
  7. y2 = int(y1*(3/5))
  8. x1 = int((y1-intercept)/slope)
  9. x2 = int((y2-intercept)/slope)
  10. #print(image.shape)
  11. return np.array([x1,y1,x2,y2])
  12.  
  13.  
  14. def average_slope_intercept(image,lines):
  15. left_fit = []
  16. right_fit = []
  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_coordinates(image,left_fit_average)
  29. right_line = make_coordinates(image,right_fit_average)
  30. return np.array([left_line,right_line])
  31.  
  32. def canny(image):
  33. gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  34. blur = cv2.GaussianBlur(gray, (5, 5),0)
  35. canny = cv2.Canny(blur,50,150)
  36. return canny
  37.  
  38. def display_lines(image, lines):
  39. line_image = np.zeros_like(image)
  40. if lines is not None:
  41. for x1,y1,x2,y2 in lines:
  42. cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
  43.  
  44. return line_image
  45.  
  46. def region_of_interest(image):
  47. height = image.shape[0]
  48. polygons = np.array([
  49. [(200,height),(1100,height),(550,250)]
  50. ])
  51. mask = np.zeros_like(image)
  52. cv2.fillPoly(mask,polygons, 255)
  53. masked_image = cv2.bitwise_and(image, mask) # used to mask the image only to show the region of interest
  54. return masked_image
  55.  
  56.  
  57. #image = cv2.imread('test_image.jpg')
  58. #lane_image = np.copy(image)
  59. #canny = canny(lane_image)
  60. #cropped_image = region_of_interest(canny)
  61. #lines = cv2.HoughLinesP(cropped_image,2,np.pi/180,100,np.array([]),minLineLength=40, maxLineGap=5)
  62. #averaged_lines = average_slope_intercept(lane_image,lines)
  63. #line_image = display_lines(lane_image,averaged_lines)
  64. #combo_image = cv2.addWeighted(lane_image,0.8,line_image,1,1)
  65. #cv2.imshow("result",combo_image)
  66. #cv2.waitKey(0) #displays the image for a particular amount of time
  67.  
  68.  
  69. cap = cv2.VideoCapture("test2.mp4")
  70. while(cap.isOpened()):
  71. _, frame = cap.read()
  72. canny_image = canny(frame)
  73. cropped_image = region_of_interest(canny_image)
  74. lines = cv2.HoughLinesP(cropped_image,2,np.pi/ 180,100,np.array([]),minLineLength=40, maxLineGap=5)
  75. averaged_lines = average_slope_intercept(frame,lines)
  76. line_image = display_lines(frame,averaged_lines)
  77. combo_image = cv2.addWeighted(frame,0.8,line_image,1,1)
  78. cv2.imshow("result",combo_image)
  79. if cv2.waitKey(1) & 0xFF == ord('q'):
  80. break
  81. cap.release()
  82. cv2.destrolAllWindows()
Design a site like this with WordPress.com
Get started