SHARE
TWEET

Untitled

a guest Jan 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # TODO: Build your pipeline that will draw lane lines on the test_images
  2. # then save them to the test_images_output directory.
  3.  
  4. class Line:
  5.     def __init__(self, x1, y1, x2, y2):
  6.         self.x1 = np.float32(x1)
  7.         self.x2 = np.float32(x2)
  8.         self.y1 = np.float32(y1)
  9.         self.y2 = np.float32(y2)
  10.        
  11.         self.get_slope()
  12.        
  13.     def get_slope(self):
  14.         self.slope = (self.y2 - self.y1) / (self.x2 - self.x1 + np.finfo(float).eps)
  15.  
  16. # Get all images
  17. images = []
  18. for image_name in os.listdir("test_images/"):
  19.     image = mpimg.imread('test_images/solidWhiteRight.jpg')
  20.     images.append(image)
  21. np_images = np.array(images)
  22.  
  23. # Loop over all images
  24. for image in images:
  25.     # Convert to grayscale
  26.     image_gray = grayscale(image)
  27.        
  28.     # Gaussian smooth the image
  29.     image_smoothed = gaussian_blur(image_gray, 7)
  30.    
  31.     # Run canny edge detector on image
  32.     image_canny = canny(image_smoothed, 50, 150)
  33.    
  34.     # Run an "opening" morphological filter
  35.     #image_canny = cv2.morphologyEx(image_canny, cv2.MORPH_OPEN, (50, 50))
  36.     #image_canny = cv2.dilate(image_canny, (50,50), iterations = 5)
  37.    
  38.     # Apply hough transform to get image with lines
  39.     #def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
  40.     #image = hough_lines(image_canny, 400, 45, 5, 5, 100)
  41.     detected_lines, line_image = hough_lines(img=image_canny,
  42.                                             rho=1,
  43.                                             theta=np.pi / 180,
  44.                                             threshold=1,
  45.                                             min_line_len=15,
  46.                                             max_line_gap=5)
  47.    
  48.     # Run RANSAC on lines?
  49.     # python scikit: linear_model.RANSACRegressor(linear_model.LinearRegression()
  50.    
  51.     # Filter lines and interpolate lines to find both lanes
  52.     preferred_lines = np.array([])
  53.     for detected_line in detected_lines:
  54.         # Only lines with slope between 30 and 120 degrees
  55.         line = Line(detected_line[0][0], detected_line[0][1], detected_line[0][2], detected_line[0][3])
  56.         if (np.pi / 6) <= np.abs(line.slope) <= (2 * np.pi / 3):
  57.             np.append(preferred_lines, [detected_line[0][0],
  58.                                         detected_line[0][1],
  59.                                         detected_line[0][2],
  60.                                         detected_line[0][3]])
  61.     # interpolate lines candidates to find both lanes
  62.     #lane_lines = compute_lane_from_candidates(candidate_lines, img_gray.shape)
  63.        
  64.     # Draw lines on a clear mask
  65.     image_with_lines = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
  66.     draw_lines(image_with_lines, preferred_lines)
  67.    
  68.     # Select the region of interest in image
  69.     region = [[[0, int(image.shape[0] / 2)],
  70.                [image.shape[1], int(image.shape[0] / 2)],
  71.                [image.shape[1], image.shape[1]],
  72.                [0, image.shape[1]]]]
  73.     image_roi = region_of_interest(image_with_lines, np.array(region))
  74.    
  75.     # Created weighted masked_image
  76.     weighted_image = weighted_img(image_roi, image, α=0.8, β=1., γ=0.)
  77.    
  78.     # Show and save image to the new folder
  79.     plt.figure()
  80.     plt.imshow(image_roi, cmap='gray')
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top