def process_image(image, minimum_distance=640, smooth=True, threshold=0.1, use_mask=True): global previous_lane undistorted = undistort(image) warped = warp(undistorted, borderMode=cv2.BORDER_REFLECT) binary = combined_threshold(warped) lane = find_lane(binary, minimum_distance=minimum_distance, smooth=smooth, threshold=threshold, use_mask=use_mask) if lane.sanity_check(): previous_lane = lane elif previous_lane is not None: lane = previous_lane radius_label = "CURVATURE = {:.2e} m - POSITION= {:.2e} m".format( lane.get_curvature(), lane.get_position()) cv2.putText(undistorted, radius_label, (0, 24), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), thickness=2) unwarped_lane_projection = unwarp(lane.get_projection(image)) return cv2.addWeighted(undistorted, 1, unwarped_lane_projection, 0.3, 0)
def demo_mask(): # Read in a thresholded image #warped = mpimg.imread('warped_example.jpg') image = mpimg.imread('test_images/test1.jpg') undist = undistort(image) warped, M, M_inverse = warp(undist) binary_warped = threshold_image(warped) #plt.imshow(binary_warped) #plt.show() #warped = mpimg.imread('output_images/straight_lines1_warped.jpg') # window settings convolution = createConvolution() lane = convolution.find_lane(binary_warped) window_width = convolution.window_width window_height = convolution.window_height # If we found any window centers if lane is not None: # Points used to draw all the left and right windows l_points = np.zeros_like(binary_warped) r_points = np.zeros_like(binary_warped) # Go through each level and draw the windows for level in range(0, len(lane.leftx)): # Window_mask is a function to draw window areas l_mask = window_mask(window_width, window_height, binary_warped, lane.leftx[level], level) r_mask = window_mask(window_width, window_height, binary_warped, lane.rightx[level], level) # Add graphic points from window mask here to total pixels found l_points[(l_points == 255) | ((l_mask == 1))] = 255 r_points[(r_points == 255) | ((r_mask == 1))] = 255 # Draw the results template = np.array( r_points + l_points, np.uint8) # add both left and right window pixels together zero_channel = np.zeros_like(template) # create a zero color channel template = np.array(cv2.merge((zero_channel, template, zero_channel)), np.uint8) # make window pixels green warpage = np.array( cv2.merge((binary_warped, binary_warped, binary_warped)), np.uint8) # making the original road pixels 3 color channels output = cv2.addWeighted( warpage, 1, template, 0.5, 0.0) # overlay the orignal road image with window results # If no window centers found, just display orginal road image else: output = np.array( cv2.merge((binary_warped, binary_warped, binary_warped)), np.uint8) # Display the final results side_by_side_plot(binary_warped, output, im1_title="Binary Warped", im2_title="Conv Search", im1_cmap='gray', im2_cmap='gray')
def processFrame(img): objpoints, imgpoints = calibrate(9, 6) undist = undistort(objpoints, imgpoints, img) sxybinary = convertBinary(undist) masked_sxybinary = mask(sxybinary) binary_warped, Minv = warp(masked_sxybinary) left_fit, right_fit, left_fitx, right_fitx, ploty, leftx, lefty, rightx, righty = visualizeLanes( binary_warped) left_radius, right_radius = curvature(leftx, lefty, rightx, righty) distance = lanePosition(left_fitx, right_fitx, undist) img = addData(undist, left_radius, right_radius, distance) return drawLane(img, left_fit, right_fit, left_fitx, right_fitx, ploty, binary_warped, Minv)
def demo_threshold_image(): image = mpimg.imread('test_images/straight_lines1.jpg') undist = undistort(image) warped, M, M_inverse = warp(undist) binary_warped = threshold_image(warped) side_by_side_plot(image, binary_warped, im1_title='Image', im2_title='Binary Warped', im2_cmap='gray') #plt.imshow(binary_image, cmap = 'gray') #plt.show() #demo_threshold_image()
def demo_convolution(): # Read in a thresholded image image = mpimg.imread('test_images/straight_lines2.jpg') undist = undistort(image) warped, M, M_inverse = warp(undist) binary_warped = threshold_image(warped) #plt.imshow(binary_warped) #plt.show() #warped = mpimg.imread('output_images/straight_lines1_warped.jpg') # window settings height = binary_warped.shape[0] convolution = createConvolution() lane = convolution.find_lane(binary_warped) left_fit, right_fit = lane.polyfit() y = np.linspace(0, height - 1, 72) leftx, rightx = lane.ploty(y) y_meter_per_pixel = 30 / 720 # meters per pixel in y dimension x_meter_per_pixel = 3.7 / 700 # meters per pixel in x dimension left_curverad, right_curverad = lane.measure_curvature( x_meter_per_pixel=x_meter_per_pixel, y_meter_per_pixel=y_meter_per_pixel) center_offset = convolution.center_offset(lane, x_meter_per_pixel) lane_drawn = convolution.draw_lane(lane, M_inverse) result = cv2.addWeighted(undist, 1, lane_drawn, 0.3, 0) plt.imshow(result) plt.plot(leftx, y, color='yellow') plt.plot(rightx, y, color='yellow') plt.text(5, 30, 'Left, Right Curvature: {:.2f}m, {:.2f}m'.format( left_curverad, right_curverad), fontsize=10, color='red') plt.text(5, 50, 'Center Lane Offset: {:.2f}m, Confidence: {:.2f}%'.format( center_offset, lane.confidence * 100), fontsize=10, color='red') plt.xlim(0, 1280) plt.ylim(720, 0) plt.show()
def detect(self, rgb_image): undist_image = undistort(rgb_image) warped, M, M_inverse = warp(undist_image) binary_warped = threshold_image(warped) lane = None if self.best_lane is not None: lane = self.convolution.find_lane_with_hint(self.best_lane.midx(), binary_warped) if lane is None: lane = self.convolution.find_lane(binary_warped) self.add_lane(lane) lane_selected = None if self.best_lane is not None: lane_selected = self.best_lane self.n_best_lane_used += 1 elif (lane is not None) and lane.good_confidence(): lane_selected = lane self.n_lane_used += 1 else: lane_selected = self.last_good_lane self.n_last_good_lane_used += 1 if (lane is not None) and lane.good_confidence(): self.last_good_lane = lane if lane_selected is not None: left_curverad, right_curverad = self.measure_curvature(lane_selected) center_offset = self.center_offset(lane_selected) lane_drawn = self.convolution.draw_lane(lane_selected, M_inverse = M_inverse) out_image = cv2.addWeighted(undist_image, 1, lane_drawn, 0.3, 0) cv2.putText(out_image, 'Left, Right Curvature: {:.2f}m, {:.2f}m'.format(left_curverad, right_curverad), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.putText(out_image, 'Center Lane Offset: {:.2f}m'.format(center_offset), (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) return out_image else: if lane is not None: print(lane.confidence) side_by_side_plot(undist_image, binary_warped) self.n_no_lane_used += 1 return undist_image
right_line = Right(right_points, image_height, image_width, Y_METERS_PER_PIXEL, X_METERS_PER_PIXEL, smooth) lane = Lane(left_line, right_line, image) return lane if __name__ == '__main__': test_images = glob.glob('../test_images/*.jpg') output_images = [] for test_image in test_images: image = cv2.imread(test_image) undistorted = undistort(image) warped = warp(undistorted, borderMode=cv2.BORDER_REFLECT) binary = combined_threshold(warped) lane = find_lane(binary, minimum_distance=640, threshold=0.1, use_mask=False) annotated = annotate(binary, lane=lane) output_images.append(warped) output_images.append(annotated) collage = util.collage(output_images, len(test_images), 2) cv2.imwrite('../output_images/lane_finder.png', collage)
def pipeline(img, left_lane=None, right_lane=None ,mtx=None, dist=None, fname=None, testMode=False): """ Pipeline for finding lane lines on a given image. If camera calibration was not done in a previous step and the result are not given as mtx and dist to the function, it will calibrate the camera on itself. The pipeline consists of serveral steps: 1) Camera Calibration (done if necessary) 2) Distortion correction 3) Color & Gradient Threshold 4) Perspective Transform 5) Find lanes, calculate curvature and distance to center with peaks in histogram. Draw found lanes in transformed image 6) Retransform image and stack undistorted and rewarped image 7) Write text with calculations of step 5 on image Furthermore, it will save the images to output_images folder if it is run in test mode. """ ### Prepare lanes if function was called without if left_lane is None: left_lane = lane.Lane() if right_lane is None: right_lane = lane.Lane() ### Step 1 if mtx is None or dist is None: mtx, dist = camera_calibration.get_camera_calibration_values() ### Step 2 dst = cv2.undistort(img, mtx, dist, None, None) ### Step 3 combined = color_gradient_threshold.apply_thresholds(dst) ### Step 4 mask = perspective_transform.apply_standard_mask_to_image(combined) warped = perspective_transform.warp(mask) ### Step 5 left_lane, right_lane, center_distance, identified_lanes_image = find_lanes.find_lanes_with_histogram(warped, left_lane, right_lane) filled_image = find_lanes.fillPolySpace(identified_lanes_image, left_lane, right_lane) ### Step 6 rewarped = perspective_transform.warp(identified_lanes_image, toBirdView=False) result = perspective_transform.weighted_img(dst, rewarped, α=0.8, β=1, λ=0) ### Step 7 result = __put_texts_on_image(result, left_lane, right_lane, center_distance) ### Plot result of each step to the output_images folder if run in test mode if testMode: f, ((ax11, ax12, ax13, ax14),(ax21, ax22, ax23, ax24)) = plt.subplots(2, 4, figsize=(24, 9)) f.tight_layout() ax11.imshow(img) ax11.set_title('Original Image', fontsize=50) ax12.imshow(dst) ax12.set_title('Undistorted Image', fontsize=50) ax13.imshow(combined, cmap='gray') ax13.set_title('Combination', fontsize=50) ax14.imshow(mask, cmap='gray') ax14.set_title('Masked Image', fontsize=50) ax21.imshow(warped, cmap='gray') ax21.set_title('Warped Image', fontsize=50) ax22.imshow(identified_lanes_image) ax22.plot(left_lane.current_xfitted, left_lane.ploty, color='yellow') ax22.plot(right_lane.current_xfitted, right_lane.ploty, color='yellow') ax22.set_title('Identified Lanes', fontsize=50) ax23.imshow(rewarped) ax23.set_title('Rewarped Image', fontsize=50) ax24.imshow(result) ax24.set_title('Final Image', fontsize=50) plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.) plt.savefig('./output_images/'+fname.split('/')[-1], dpi=100) return result
def to_binary_image(image): undist = undistort(image) binary_warped, M, M_inverse = warp(undist) return threshold_image(binary_warped)
return combined, color_thresholded, xy_thresholded, g_thresholded def combined_threshold(image): return _combined_threshold(image)[0] if __name__ == '__main__': test_images = glob.glob('../test_images/*.jpg') color_threshold_images = [] for test_image in test_images: image = cv2.imread(test_image) image = warp(undistort(image)) color_thresholded, h_thresholded, l_thresholded, s_thresholded = color_threshold(image) color_threshold_images.append(image) color_threshold_images.append(h_thresholded) color_threshold_images.append(l_thresholded) color_threshold_images.append(s_thresholded) color_threshold_images.append(color_thresholded) collage = util.collage(color_threshold_images, len(test_images), 5) cv2.imwrite('../output_images/color_threshold.png', collage) absolute_threshold_images = [] for test_image in test_images: image = cv2.imread(test_image)