def get_lines(img, rho, theta, threshold, min_line_len, max_line_gap): # convert to grayscale img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # perform gaussian blur blur = gaussian_blur(img_gray, kernel_size=17) # perform edge detection canny_edges = canny(blur, low_threshold=50, high_threshold=70) detected_lines = hough_lines(img=canny_edges, rho=rho, theta=theta, threshold=threshold, min_line_len=min_line_len, max_line_gap=max_line_gap) #print('detected_lines',detected_lines) if detected_lines is None: return candidate_lines = [] for line in detected_lines: for x1, y1, x2, y2 in line: slope = get_slope(x1, y1, x2, y2) if 0.0 <= np.abs(slope) <= 2: candidate_lines.append({ "slope": slope, "bias": get_bias(slope, x1, y1) }) lane_lines = compute_candidates(candidate_lines, img_gray.shape) return lane_lines
def fine_lane_pipeline(image): imshape = image.shape xlength = imshape[1] ylength = imshape[0] gray = grayscale(image) # Define a kernel size and apply Gaussian smoothing blur_gray = gaussian_blur(gray, kernel_size=5) # Define our parameters for Canny and apply low_threshold = 50 high_threshold = 150 edges = canny(blur_gray, low_threshold, high_threshold) # Define the Hough transform parameters # Make a blank the same size as our image to draw on rho = 1 theta = np.pi / 180 threshold = 25 min_line_length = 10 max_line_gap = 5 # Run Hough on edge detected image line_image = hough_lines(edges, rho, theta, threshold, min_line_length, max_line_gap) vertices = np.array([[(0, ylength), (xlength / 2 - ylength / 10, ylength * 0.625), (xlength / 2 + ylength / 10, ylength * 0.625), (xlength, ylength)]], dtype=np.int32) combo = region_of_interest(line_image, vertices) combo = weighted_img(combo, image) return combo
def lane_detection_pipeline(img): """ Detect lane lines in an input image. Args: img: The original unmodified input image. Returns: A new image with lines drawn. """ # calculate shape (height, width, num_channels) = img.shape white_mask = utils.color_threshold(img, rgb_min=[150, 150, 150]) yellow_mask = utils.color_threshold(img, rgb_min=[150, 150, 0], rgb_max=[255, 255, 165]) color_mask = white_mask & yellow_mask # yellow_white = utils.boolean_mask(img, color_mask) # convert image to gray scale gray = utils.grayscale(img) # blur image with Gaussian smoothing blur = utils.gaussian_blur(gray, 5) # Define our parameters for Canny and apply edges = utils.canny(blur, 50, 150) # Only keep the portions that originally had white or yellow color_masked = utils.boolean_mask(edges, color_mask) # mask everything but the region of interest vertices = np.array([[(0, height), (width * 6 / 13, height * 3 / 5), (width * 7 / 13, height * 3 / 5), (width, height)]], dtype=np.int32) masked = utils.region_of_interest(color_masked, vertices) # Apply Hough transform detected_lines = utils.hough_lines( img=masked, rho=2, # distance resolution in pixels of the Hough grid theta=np.pi / 180, # angular resolution in radians of the Hough grid threshold=15, # min num of votes (intersections in Hough grid cell) min_line_len=40, # minimum number of pixels making up a line max_line_gap= 20 # maximum gap in pixels between connectable line segments ) # Overlay the detected lines on the original img output = utils.weighted_img(detected_lines, img) return output
def process_image(image_original): # Note: always make a copy rather than simply using "=" image_copy = np.copy(image_original) # Read in and grayscale the image image_gray = grayscale(image_copy) # Define a kernel size and apply Gaussian smoothing kernel_size = 3 image_blur_gray = gaussian_blur(image_gray, kernel_size) # Define our parameters for Canny and apply the Canny transform low_threshold = 50 #35 high_threshold = 200 #70 image_edges_canny = canny(image_blur_gray, low_threshold, high_threshold) # Next we'll create a masked edges image using cv2.fillPoly() # We are defining a four sided polygon to mask imshape = image_original.shape if (imshape[1] == 960): #small image use this mask vertices = [(150, imshape[0]), (480, 300), (490, 300), (imshape[1] - 30, imshape[0])] else: #large image use this mask vertices = [(int(imshape[1] * 6 / 32), 660), (620, 430), (710, 430), (imshape[1] - 150, 650)] mask_polygon = np.array([vertices], dtype=np.int32) image_masked_edges_canny = region_of_interest(image_edges_canny, mask_polygon) # Define the Hough transform parameters rho = 2 #2,1 distance resolution in pixels of the Hough grid theta = np.pi / 180 # angular resolution in radians of the Hough grid threshold = 80 #8,15,1 minimum number of votes (intersections in Hough grid cell) meaning at least 15 points in image space need to be associated with each line segment min_line_length = 10 #40,10 minimum number of pixels making up a line max_line_gap = 13 #20,5 maximum gap in pixels between connectable line segments # Make a blank the same size as our image to draw on # Run Hough on edge detected image #It returns an image with lines drawn on it, a blank image (all black) with lines drawn on it image_hough_lines_masked = draw_hough_lines_extrapolate( image_copy, image_masked_edges_canny, rho, theta, threshold, min_line_length, max_line_gap) #To generate videos you need to return this image return image_hough_lines_masked
def process_image(image): original_image = image.copy() ysize = image.shape[0] xsize = image.shape[1] gray = grayscale(image) # Define a kernel size and apply Gaussian smoothing kernel_size = 5 blur_gray = gaussian_blur(gray, kernel_size) # Define our parameters for Canny and apply low_threshold = 50 high_threshold = 150 edges = canny(blur_gray, low_threshold, high_threshold) left_bottom = [0, ysize] right_bottom = [xsize, ysize] apex = [xsize / 2, ysize / 1.72] # This time we are defining a four sided polygon to mask vertices = np.array([[left_bottom, apex, apex, right_bottom]], dtype=np.int32) masked_edges = region_of_interest(edges, vertices) # Define the Hough transform parameters # Make a blank the same size as our image to draw on rho = 2 # distance resolution in pixels of the Hough grid theta = np.pi / 180 # angular resolution in radians of the Hough grid threshold = 15 # minimum number of votes (intersections in Hough grid cell) min_line_length = 40 # minimum number of pixels making up a line max_line_gap = 20 # maximum gap in pixels between connectable line segments line_image = np.copy(image) * 0 # creating a blank to draw lines on # Run Hough on edge detected image # Output "lines" is an array containing endpoints of detected line segments # lines = hough_lines(masked_edges, rho, theta, threshold, min_line_length, max_line_gap) lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap) left_points, right_points = separate_by_slope(lines) if left_points: # Find the slope based on the generated points for the left line slope = slope_from_lin_reg(left_points) # Calculate x for the largest y. i.e find the lowest point on the image part of the extrapolate line x2 = int(max(left_points)[0] + (ysize - max(left_points)[1]) / slope) up_left_point = max(left_points) down_left_point = [x2, ysize] draw_line(line_image, up_left_point, down_left_point) if right_points: slope = slope_from_lin_reg(right_points) # Calculate x for the largest y. i.e find the lowest point on the image part of the extrapolate line x2 = int(max(right_points)[0] + (ysize - max(right_points)[1]) / slope) up_right_point = min(right_points) down_right_point = [x2, ysize] draw_line(line_image, up_right_point, down_right_point) # Draw the lines on the edge image lines_edges = weighted_img(line_image, original_image) return lines_edges
# image_name = 'solidYellowCurve2.jpg' # image_name = 'solidWhiteCurve.jpg' image = mpimg.imread('./test_images_challenge/'+image_name) # gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) gray = utils.grayscale(image) # Define a kernel size and apply Gaussian smoothing kernel_size = 5 # blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0) blur_gray = utils.gaussian_blur(gray, kernel_size) # Define our parameters for Canny and apply low_threshold = 50 high_threshold = 150 # edges = cv2.Canny(blur_gray, low_threshold, high_threshold) edges = utils.canny(blur_gray, low_threshold, high_threshold) plt.subplot(221),plt.imshow(image,cmap = 'gray') plt.title('Original Image \n{}'.format(image_name) ) plt.subplot(222),plt.imshow(edges,cmap = 'gray') plt.title('Canny Edges \n{}'.format(image_name) ) # Next we'll create a masked edges image using cv2.fillPoly() mask = np.zeros_like(edges) ignore_mask_color = 255 # This time we are defining a four sided polygon to mask imshape = image.shape img_width = 720 # left_bottom = [160, img_width - 60] # left_top = [500, 450]