Пример #1
0
def select_white_yellow(image, lightest_percent, debug):
    converted = convert_hls(image)
    #converted = convert_hsv(image)
    #show_images([converted])
    # white color mask
    lower = np.uint8([0, get_white_threshold(converted, lightest_percent), 0])
    upper = np.uint8([255, 255, 255])
    #sensitivity = 164
    #lower = np.array([0,0,255-sensitivity])
    #upper = np.array([255,sensitivity,255])
    white_mask = cv2.inRange(converted, lower, upper)
    # yellow color mask
    #lower = np.uint8([ 10,   0, 100])
    #upper = np.uint8([ 40, 255, 255])
    #yellow_mask = cv2.inRange(converted, lower, upper)
    # combine the mask
    #mask = cv2.bitwise_or(white_mask, yellow_mask)
    mask = white_mask
    ret = cv2.bitwise_and(image, image, mask=mask)

    if (debug):
        print('after white color selection')
        utilities.show_images([ret])

    return ret
Пример #2
0
def perspective_transform(vp, img, debug):
    #vp = calculate_vanishing_point(img)
    width, height = img.shape[1], img.shape[0]
    rem_width, rem_height = min(vp[0, 0], width-vp[0, 0]), height - vp[1, 0]
    x_off, y_off =  int(0.65*rem_width), int(0.15*rem_height)
    y_base = vp[1, 0] + int(0.6*rem_height)
	#y_base = img.shape[0]-100
    #x_off, y_off =  200, 30
    p1, p2 = [vp[0,0]-x_off, vp[1, 0]+y_off], [vp[0,0]+x_off, vp[1, 0]+y_off]
    p3, p4 = [find_x(p1, [vp[0, 0], vp[1, 0]], y_base), y_base], [find_x(p2, [vp[0, 0], vp[1, 0]], y_base), y_base]
		
    if(debug):        
        ps = [p1, p2, p3, p4]
        for p in ps:
            p[0], p[1] = int(p[0]), int(p[1])
        display_trapezoid(img, p1, p2, p3, p4, vp)
            
    map_size = (300, 600)
    src = np.float32([p1, p2, p3, p4])
    dst = np.float32([[0,0], [map_size[0]-1, 0], [0, map_size[1]-1], [map_size[0]-1, map_size[1]-1]])
    H, H_inv = cv2.getPerspectiveTransform(src, dst), cv2.getPerspectiveTransform(dst, src)
    warped = cv2.warpPerspective(img, H, map_size)
    
    if(debug):
    	# warped image
        utilities.show_images([cv2.cvtColor(warped, cv2.COLOR_BGR2RGB)])
    
    return H, H_inv, warped
Пример #3
0
def calculate_vanishing_point(lines, ud_img_BGR, debug):

    if (debug):
        ud_img = np.array(ud_img_BGR)

    t1 = np.zeros((2, 2), dtype=np.float)
    t2 = np.zeros((2, 1), dtype=np.float)

    for line in lines:

        a, b = getnormal(line[0], True)
        x1, y1, x2, y2 = utilities.get2pts(line[0], True)

        n = np.array([[a], [b]], dtype=np.float)
        n_x_nt = np.matmul(n, np.transpose(n))
        p = np.array([[x1], [y1]], dtype=np.float)
        t1 += n_x_nt
        t2 += np.matmul(n_x_nt, p)

        if (debug):
            cv2.line(ud_img, (x1, y1), (x2, y2), (0, 0, 255), 2)

    vp = np.matmul(np.linalg.pinv(t1), t2)
    vp = np.array(vp, dtype=np.int)

    if (debug):
        print(vp)
        cv2.line(ud_img, (vp[0, 0], vp[1, 0]), (vp[0, 0], vp[1, 0]),
                 (255, 0, 0), 20)
        utilities.show_images([cv2.cvtColor(ud_img, cv2.COLOR_BGR2RGB)])

    return vp
Пример #4
0
def detect_edges(image, debug, low_threshold=50, high_threshold=80):
    ret = cv2.Canny(image, low_threshold, high_threshold)

    if (debug):
        print('after edge detection')
        utilities.show_images([ret])

    return ret
Пример #5
0
def convert_gray_scale(image, debug):
    ret = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

    if (debug):
        print('after converting to grayscale')
        utilities.show_images([ret])

    return ret
Пример #6
0
def display_trapezoid(img_orig, p1, p2, p3, p4, vp):
    img = np.array(img_orig)
    
    cv2.line(img,(p1[0],p1[1]),(p3[0],p3[1]),(255,0,0), 5)
    cv2.line(img,(p2[0],p2[1]),(p4[0],p4[1]),(255,0,0), 5)
    cv2.line(img,(p2[0],p2[1]),(p1[0],p1[1]),(255,0,0), 5)
    cv2.line(img,(p3[0],p3[1]),(p4[0],p4[1]),(255,0,0), 5)
    cv2.line(img,(vp[0,0],vp[1,0]),(vp[0,0],vp[1,0]),(255,0,0),20)
    
    utilities.show_images([cv2.cvtColor(img, cv2.COLOR_BGR2RGB)])
Пример #7
0
def apply_smoothing(image, debug, kernel_size=15):
    """
    kernel_size must be postivie and odd
    """
    ret = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)

    if (debug):
        print('after smoothing')
        utilities.show_images([ret])

    return ret
Пример #8
0
def calibrate(debug):
    
    vi, vj = 9, 6
    
    # termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((vi*vj,3), np.float32)
    objp[:,:2] = np.mgrid[0:vi,0:vj].T.reshape(-1,2)

    # Arrays to store object points and image points from all the images.
    objpoints = [] # 3d point in real world space
    imgpoints = [] # 2d points in image plane.
    
    images = glob.glob(os.getcwd() +'\\chess\\*.jpg')

    for fname in images:
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

        # Find the chess board corners
        ret, corners = cv2.findChessboardCorners(gray, (vi, vj),None)

        # If found, add object points, image points (after refining them)
        if ret == True:
            objpoints.append(objp)

            corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
            imgpoints.append(corners2)

            # Draw and display the corners
            img = cv2.drawChessboardCorners(img, (vi, vj), corners2,ret)
            
            if(debug):
                utilities.show_images([cv2.cvtColor(img, cv2.COLOR_BGR2RGB)])
            
            cv2.waitKey(500)

    cv2.destroyAllWindows()
    
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)    
    
    return ret, mtx, dist, rvecs, tvecs
Пример #9
0
def select_region(image, debug):
    """
    It keeps the region surrounded by the `vertices` (i.e. polygon).  Other area is set to 0 (black).
    """
    # first, define the polygon by vertices
    # cols, rows where: 0.1, 0.4, 0.9, 0.6 | 0.95, 0.6, 0.95, 0.6
    rows, cols = image.shape[:2]
    bottom_left = [cols * 0.0, rows * 0.9]
    top_left = [cols * 0.0, rows * 0.55]
    bottom_right = [cols * 1.0, rows * 0.9]
    top_right = [cols * 1.0, rows * 0.55]
    # the vertices are an array of polygons (i.e array of arrays) and the data type must be integer
    vertices = np.array([[bottom_left, top_left, top_right, bottom_right]],
                        dtype=np.int32)
    ret = filter_region(image, vertices)

    if (debug):
        print('after ROI')
        utilities.show_images([ret])

    return ret
Пример #10
0
def get_ratio(H, H_inv, orig_warped, mtx, debug):
    meter_to_feet = 1/3.28084
    low_thresh = 50
    high_thresh = 100
	
    if(debug):
        warped = np.array(orig_warped)
    
    #gray = cv2.cvtColor(warped,cv2.COLOR_BGR2GRAY)
    #gray = cv2.GaussianBlur(gray,(9, 9),0)
    #edges = cv2.Canny(gray,low_thresh,high_thresh,apertureSize = 3)
    #utilities.show_images([edges])
    #lines = cv2.HoughLines(edges,1,np.pi/180,100)
    detector = lane_detection.LaneDetector()
    lines = detector.process(cv2.cvtColor(orig_warped,cv2.COLOR_BGR2RGB), False, 0.02, debug)
    '''lines = cv2.HoughLinesP(
        edges,
        rho=2,
        theta=np.pi / 180,
        threshold=50,
        lines=np.array([]),
        minLineLength=40,
        maxLineGap=120
    )'''
    
    x_min = 1000000
    x_max = 0
	
    tot_xlft, cnt_xlft = 0, 0
    tot_xrit, cnt_xrit = 0, 0 
    
    for line in lines:
        x1, y1, x2, y2 = utilities.get2pts(line[0], True)

        '''if(x1 < x_min):
            x_min = x1
        if(x1 > x_max):
            x_max = x1'''

        if(min(x1, x2) < 0.5*orig_warped.shape[1]):
            tot_xlft += float(x1+x2) / 2
            cnt_xlft += 1
        else:
            tot_xrit += float(x1+x2) / 2
            cnt_xrit += 1
			
        if(debug):
            cv2.line(warped,(x1,y1),(x2,y2),(0,0,255),2)
    
    warped_height = orig_warped.shape[0]
    '''left_low, left_high, right_low, right_high = get_mapped_pxl([x_min, warped_height-1], H_inv),\
    get_mapped_pxl([x_min, 0], H_inv), get_mapped_pxl([x_max, warped_height-1], H_inv),\
    get_mapped_pxl([x_max, 0], H_inv)'''
    
    avg_xlft, avg_xrit = int(float(tot_xlft) / cnt_xlft), int(float(tot_xrit) / cnt_xrit)
	
    left_low, left_high, right_low, right_high = get_mapped_pxl([avg_xlft, warped_height-1], H_inv),\
    get_mapped_pxl([avg_xlft, 0], H_inv), get_mapped_pxl([avg_xrit, warped_height-1], H_inv),\
    get_mapped_pxl([avg_xrit, 0], H_inv)
	
    if(debug):
    	# lanes on warped
        utilities.show_images([cv2.cvtColor(warped, cv2.COLOR_BGR2RGB)])
    
    approx_dist = avg_xrit - avg_xlft
    x_pixels_per_meter = approx_dist / (12 * meter_to_feet)
    
    inv_H_x_mtx = np.linalg.inv(np.matmul(H , mtx))
    x_norm = np.linalg.norm(inv_H_x_mtx[:,0])
    y_norm = np.linalg.norm(inv_H_x_mtx[:,1])
    scale = x_norm / y_norm
    y_pixels_per_meter = x_pixels_per_meter * scale
    
    return x_pixels_per_meter , y_pixels_per_meter, left_low, left_high, right_low, right_high