Skip to content

Instantly share code, notes, and snippets.

View gy2256's full-sized avatar
🚀
Focusing

Guang gy2256

🚀
Focusing
View GitHub Profile
def fitpolynomial_with_previous_fitting_value(binary_warped, left_fit_before, right_fit_before,ploty):
margin = 120
left_fit_current, right_fit_current = (None, None)
img_shape = binary_warped.shape
nonzero = binary_warped.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
left_lane_inds = ((nonzerox > (left_fit_before[0]*(nonzeroy**2) + left_fit_before[1]*nonzeroy +
left_fit_before[2] - margin)) & (nonzerox < (left_fit_before[0]*(nonzeroy**2) +
def measure_curvature_pixels(left_fit, right_fit, ploty, leftx, lefty, rightx, righty):
'''
Calculates the curvature of polynomial functions in pixels.
'''
# Define y-value where we want radius of curvature
# We'll choose the maximum y-value, corresponding to the bottom of the image
y_eval = np.max(ploty)
left_curverad = ((1 + (2*left_fit[0]*y_eval + left_fit[1])**2)**1.5) / np.absolute(2*left_fit[0])
right_curverad = ((1 + (2*right_fit[0]*y_eval + right_fit[1])**2)**1.5) / np.absolute(2*right_fit[0])
def find_lane_pixels(binary_warped):
# Take a histogram of the bottom half of the image
#binary_warped = binary_warped[:,:,0] #Just get the one of the three channels
histogram = np.sum(binary_warped[binary_warped.shape[0]//2:,:], axis=0)
plt.plot(histogram)
# Create an output image to draw on and visualize the result
out_img = (np.dstack((binary_warped, binary_warped, binary_warped))).astype(np.uint8)
def perspective_transform(image):
if len(image.shape) > 2:
height, width, chanels = image.shape
else:
height, width = image.shape
src = np.array([[(width*0.1,height*0.9),
(width/2.3,height/1.6),
(width/1.7,height/1.6),
(width*0.9,height*0.9)]],dtype=np.float32)
def mask_image(img, vertices):
mask = np.zeros_like(img)
if len(img.shape) > 2:
channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
#filling pixels inside the polygon defined by "vertices" with the fill color
cv2.fillPoly(mask, vertices, ignore_mask_color)
#returning the image only where mask pixels are nonzero
@gy2256
gy2256 / combined.py
Created December 17, 2018 22:38
combined threshold
def threshold(image,ksize, abs_sobel_thresh_param_x, abs_sobel_thresh_param_y ,mag_thresh_param, dir_thresh_param,saturation_thresh_param, hue_thresh_param):
# Convert original RGB image to Gray Image
grayImg = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Gradient Threshold
gradx = abs_sobel_thresh(grayImg, orient='x', sobel_kernel=ksize, thresh=abs_sobel_thresh_param_x)
grady = abs_sobel_thresh(grayImg, orient='y', sobel_kernel=ksize, thresh=abs_sobel_thresh_param_y)
mag_binary = mag_thresh(grayImg, sobel_kernel=ksize, mag_thresh=mag_thresh_param)
dir_binary = dir_threshold(grayImg, sobel_kernel=ksize, thresh=dir_thresh_param)
combined_gradient_binary = np.zeros_like(dir_binary)
@gy2256
gy2256 / color.py
Created December 17, 2018 22:36
color threshold
def color_threshold(imgage, S_thresh=(0, 255),H_thresh=(0, 255)):
hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
H = hls[:,:,0]
S = hls[:,:,2]
binary_output = np.zeros_like(S)
binary_output[(S > S_thresh[0]) & (S <= S_thresh[1])& (H > H_thresh[0])& (H <= H_thresh[1])] = 1
return binary_output
@gy2256
gy2256 / sobel.py
Created December 17, 2018 22:34
gradient threshold
def abs_sobel_thresh(img, orient='x', sobel_kernel=-1, thresh=(60, 150)):
# Calculate directional gradient
# Apply threshold
thresh_min = thresh[0]
thresh_max = thresh[1]
grayImg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
if orient =='x':
sobelDir = cv2.Sobel(grayImg, cv2.CV_64F, 1,0, ksize=sobel_kernel) #x oritentaion
elif orient =='y':
@gy2256
gy2256 / calibration.py
Created December 17, 2018 22:26
calirbation gist code block for markdown
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import json
%matplotlib qt
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*9,3), np.float32)
def draw_lines(img, lines, color=[255, 0, 0], thickness=4):
leftLineSegmentx = []
leftLineSegmenty = []
rightLineSegmentx = []
rightLineSegmenty = []
if lines is not None: #Error Handling
for line in lines:
for x1,y1,x2,y2 in line:
#Idetify whether the line is left or right line