def process_frame(self, img_org, flag=False):

    img_undist = undistort(img_org, mtx, dist)
    img_undist_gray = cv2.cvtColor(img_undist, cv2.COLOR_RGB2GRAY)

    img_ylane_th = lab_select(img_undist, thresh=(150, 200))
    img_wlane_th = luv_select(img_undist, thresh=(225, 255))
    img_grad_abx = abs_sobel_th(img_undist, orient='x', ksize=ks, thresh=th)
    img_grad_mag = mag_sobel_th(img_undist, ks, th)
    img_grad_dir = dir_sobel_th(img_undist, ksize=7, thresh=(1.0, 1.2))

    img_combined = np.zeros_like(img_undist_gray)
    #    img_combined[ (img_wlane_th == 1) | (img_ylane_th == 1) ] = 1
    img_combined[(img_wlane_th == 1) | (img_ylane_th == 1) |
                 (img_grad_abx == 1) | (img_grad_mag == 1)] = 1
    #                  (img_grad_abx == 1) | (img_grad_mag == 1) | (img_grad_dir == 1) ] = 1
    img_pers, persM = corners_unwarp(img_combined, nx, ny, mtx, dist, src, dst)

    img_combined_pers = np.copy(img_pers)
    roi_combined = mask_roi(img_combined_pers)

    win_c, lc_pts, rc_pts = find_window_centroids(roi_combined, w_win, h_win,
                                                  margin)
    left_fitx, right_fitx, ploty_win = polyfit_window(h_img, lc_pts, rc_pts,
                                                      h_win)
    avg_curve_r = measure_curve_r(ploty_img, lc_pts, rc_pts, ploty_win, h_img)
    offset_m = get_offset(lc_pts, rc_pts, w_img)

    dist_bw_lines = np.average(left_fitx) - np.average(right_fitx)
    self.dist_bw_lines.append(dist_bw_lines)
    avg_dist_bw_lines = np.average(self.dist_bw_lines[-60:])
    dist_diff_squa = (avg_dist_bw_lines - dist_bw_lines)**2

    dist_diff_squa_stack.append(dist_diff_squa)
    dist_mse = np.average(dist_diff_squa_stack)  # Mean Square Error
    print("dist_bw_lines", dist_bw_lines)
    print("dist_mse", dist_mse)

    if dist_mse <= 750:
        self.l_curr_fitx.append([left_fitx])
        self.r_curr_fitx.append([right_fitx])

    l_avg_fitx = np.average(self.l_curr_fitx[-5:], axis=0)
    r_avg_fitx = np.average(self.r_curr_fitx[-5:], axis=0)

    img_fin = draw_lane(roi_combined, l_avg_fitx, r_avg_fitx, ploty_img,
                        img_undist, Minv)

    cv2.putText(img_fin, ("Curve Radius: " + str(avg_curve_r) + "m"),
                (256, 50), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(img_fin, ("Center Offset: " + str(offset_m) + "m"), (896, 50),
                font, 1, (255, 255, 255), 2, cv2.LINE_AA)
    """
    cv2.imwrite("../output_images/output_img_undist.jpg", img_undist)
    cv2.imwrite("../output_images/output_img_wlane_th.jpg", img_wlane_th*255)
    cv2.imwrite("../output_images/output_img_ylane_th.jpg", img_ylane_th*255)
    cv2.imwrite("../output_images/output_img_grad_abx.jpg", img_grad_abx*255)
    cv2.imwrite("../output_images/output_img_grad_mag.jpg", img_grad_mag*255)
    cv2.imwrite("../output_images/output_img_grad_dir.jpg", img_grad_dir*255)
    cv2.imwrite("../output_images/output_img_combined.jpg", img_combined*255)
    cv2.imwrite("../output_images/output_img_combined_pers.jpg", img_combined_pers*255)
    cv2.imwrite("../output_images/output_img_fin.jpg", img_fin)
    """

    if flag is False:
        return img_fin
    else:
        show_window_centroids(img_combined, w_win, h_win, win_c)
        return img_fin, roi_grad_abx, roi_grad_mag, roi_grad_dir, roi_hls_ch_s, img_combined, show_window_centroids, roi_wline_th, roi_yline_th
Exemplo n.º 2
0
    img_grad_dir = dir_sobel_th(img_undist, ksize=7, thresh=(1.0, 1.2))

    img_combined = np.zeros_like(img_undist_gray)
#    img_combined[ (img_wlane_th == 1) | (img_ylane_th == 1) ] = 1
    img_combined[ (img_wlane_th == 1) | (img_ylane_th == 1) |
                  (img_grad_abx == 1) | (img_grad_mag == 1) ] = 1 
#                  (img_grad_abx == 1) | (img_grad_mag == 1) | (img_grad_dir == 1) ] = 1 
#    img_pers, persM = corners_unwarp(img_combined, nx, ny, mtx, dist, src, dst)
    
    img_combined_pers = np.copy(img_pers)
    roi_combined = mask_roi(img_combined_pers)
	"""

    win_c, lc_pts, rc_pts = find_window_centroids(roi_combined, w_win, h_win, margin)
    left_fitx, right_fitx, ploty_win = polyfit_window(h_img, lc_pts, rc_pts, h_win)
    avg_curve_r = measure_curve_r(ploty_img, lc_pts, rc_pts, ploty_win, h_img)
    offset_m = get_offset(lc_pts, rc_pts, w_img)
    
    dist_bw_lines = np.average(left_fitx) - np.average(right_fitx)
    self.dist_bw_lines.append(dist_bw_lines)
    avg_dist_bw_lines = np.average(self.dist_bw_lines[-60:])
    dist_diff_squa = (avg_dist_bw_lines-dist_bw_lines)**2
    
    dist_diff_squa_stack.append(dist_diff_squa)
    dist_mse = np.average(dist_diff_squa_stack) # Mean Square Error
    print("dist_bw_lines", dist_bw_lines)
    print("dist_mse", dist_mse)
    
    
    if dist_mse <= 750:
        self.l_curr_fitx.append([left_fitx])
def process_frame(self, img_org, flag=False):

    img_undist = undistort(img_org, mtx, dist)
    img_pers, Mpers = corners_unwarp(img_undist, src, dst)

    img_hls_ch_s = hls_select(img_pers, ch='s', thresh=(125, 190))

    img_grad_abx = abs_sobel_th(img_pers,
                                orient='x',
                                ksize=ks,
                                thresh=(35, 115))
    img_grad_mag = mag_sobel_th(img_pers, ksize=ks, thresh=(35, 115))
    img_grad_dir = dir_sobel_th(img_pers, ksize=7, thresh=(0.8, 1.4))

    img_wlane_th = threshold(cv2.cvtColor(img_pers, cv2.COLOR_RGB2GRAY),
                             thresh=(225, 255))
    img_roi_wlane = mask_roi(img_wlane_th)

    img_ylane_th = threshold(img_pers[:, :, 0], thresh=(235, 255))
    img_roi_ylane = mask_roi(img_ylane_th)

    img_roi_abx = mask_roi(img_grad_abx)
    img_roi_mag = mask_roi(img_grad_mag)
    img_roi_hls = mask_roi(img_hls_ch_s)

    img_combined = np.zeros_like(img_grad_dir)
    img_combined[(img_roi_abx == 1) | (img_roi_hls == 1) | (img_roi_ylane == 1)
                 | ((img_roi_mag == 1) &
                    (img_grad_dir == 0)) | (img_roi_wlane == 1)] = 1

    win_c, lc_pts, rc_pts = find_window_centroids(img_combined, w_win, h_win,
                                                  margin)

    left_fitx, right_fitx, ploty_win = polyfit_window(h_img, lc_pts, rc_pts,
                                                      h_win)
    avg_curve_r = measure_curve_r(ploty_img, lc_pts, rc_pts, ploty_win, h_img)
    offset_m = get_offset(lc_pts, rc_pts, w_img)

    dist_bw_lines = abs(np.average(left_fitx) - np.average(right_fitx))
    self.dist_bw_lines.append(dist_bw_lines)
    avg_dist_bw_lines = np.average(self.dist_bw_lines[-60:])
    dist_diff_squa = (abs(avg_dist_bw_lines - dist_bw_lines))**2

    dist_diff_squa_stack.append(dist_diff_squa)
    dist_mse = np.average(dist_diff_squa_stack)  # Mean Square Error

    if dist_diff_squa <= 750:
        self.l_curr_fitx.append([left_fitx])
        self.r_curr_fitx.append([right_fitx])

    l_avg_fitx = np.average(self.l_curr_fitx[-5:], axis=0)
    r_avg_fitx = np.average(self.r_curr_fitx[-5:], axis=0)

    img_fin = draw_lane(img_combined, l_avg_fitx, r_avg_fitx, ploty_img,
                        img_undist, Minvs)

    cv2.putText(img_fin, ("Curve Radius: " + str(avg_curve_r) + "m"),
                (256, 50), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(img_fin, ("Center Offset: " + str(offset_m) + "m"), (256, 100),
                font, 1, (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(img_fin, ("Distance b/w Lines: " + str(dist_bw_lines)),
                (256, 150), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(img_fin, ("Distance Square Err: " + str(dist_diff_squa)),
                (256, 200), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(img_fin, ("Distance MSE: " + str(dist_mse)), (256, 250), font,
                1, (255, 255, 255), 2, cv2.LINE_AA)

    if flag is False:
        return img_fin
    else:
        show_window_centroids(img_combined, w_win, h_win, win_c)
        return img_fin, img_grad_abx, img_grad_mag, img_grad_dir, img_combined, show_window_centroids, img_wline_th, img_yline_th