def apply_thresholing(self, image): parameters = self._thresh_params image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) # image_gray = clahe.apply(image_gray) gradx_thresh = (parameters['sobel_x_thresh_min'], parameters['sobel_x_thresh_max']) grady_thresh = (parameters['sobel_y_thresh_min'], parameters['sobel_y_thresh_max']) magnitude_thresh = (parameters['mag_thresh_min'], parameters['mag_thresh_max']) dir_thresh = (parameters['dir_thresh_min'], parameters['dir_thresh_max']) ksize = parameters['sobel_kernel'] # Apply each of the thresholding functions gradx = abs_sobel_thresh(image_gray, orient='x', sobel_kernel=ksize, thresh=gradx_thresh) grady = abs_sobel_thresh(image_gray, orient='y', sobel_kernel=ksize, thresh=grady_thresh) mag_binary = mag_thresh(image_gray, sobel_kernel=ksize, thresh=magnitude_thresh) dir_binary = dir_threshold(image_gray, sobel_kernel=ksize, thresh=dir_thresh) gradient_threshold = np.zeros_like(dir_binary) gradient_threshold[((gradx == 1) & (grady == 1)) | ((dir_binary == 1) & (mag_binary == 1))] = 1 color_threshold = create_binary_image(image) binary = np.zeros_like(gradient_threshold) binary[(color_threshold == 1) | (gradient_threshold == 1)] = 1 # binary[(gradient_threshold == 1)] = 1 return binary
def process_video(self): test_image = self.input_image sobel_kernel = self.sobel_kernel.value sobel_x_thresh_min = self.sobel_x_thresh_min.value sobel_x_thresh_max = self.sobel_x_thresh_max.value sobel_y_thresh_min = self.sobel_y_thresh_min.value sobel_y_thresh_max = self.sobel_y_thresh_max.value mag_kernel = self.mag_kernel.value mag_thresh_min = self.mag_thresh_min.value mag_thresh_max = self.mag_thresh_max.value dir_kernel = self.dir_kernel.value dir_thresh_min = self.dir_thresh_min.value / 10 dir_thresh_max = self.dir_thresh_max.value / 10 sat_thresh_min = self.sat_thresh_min.value sat_thresh_max = self.sat_thresh_max.value # gradient_threshold = threshold_image(test_image, ksize=sobel_kernel, # gradx_thresh=(sobel_x_thresh_min, sobel_x_thresh_max), # grady_thresh=(sobel_y_thresh_min, sobel_y_thresh_max), # magnitude_thresh=(mag_thresh_min, mag_thresh_max), # dir_thresh=(dir_thresh_min, dir_thresh_max)) ksize = sobel_kernel # Apply each of the thresholding functions gradx = abs_sobel_thresh(test_image, orient='x', sobel_kernel=ksize, thresh=(sobel_x_thresh_min, sobel_x_thresh_max)) grady = abs_sobel_thresh(test_image, orient='y', sobel_kernel=ksize, thresh=(sobel_y_thresh_min, sobel_y_thresh_max)) mag_binary = mag_thresh(test_image, sobel_kernel=ksize, thresh=(mag_thresh_min, mag_thresh_max)) dir_binary = dir_threshold(test_image, sobel_kernel=ksize, thresh=(dir_thresh_min, dir_thresh_max)) gradient_threshold = np.zeros_like(dir_binary) # gradient_threshold[(gradx == 1)] = 1 gradient_threshold[((gradx == 1) & (grady == 1)) | ((dir_binary == 1) & (mag_binary == 1))] = 1 image_hsl = cv2.cvtColor(test_image, cv2.COLOR_RGB2HLS) s_channel = image_hsl[:, :, 2] color_threshold = np.zeros_like(s_channel) color_threshold[(s_channel >= sat_thresh_min) & (s_channel <= sat_thresh_max)] = 1 color_threshold = create_binary_image(test_image) binary = np.zeros_like(gradient_threshold) binary[(color_threshold == 1) | (gradient_threshold == 1)] = 1 # binary = np.zeros_like(gradient_threshold) # binary[(color_threshold == 1) | (gradient_threshold == 1)] = 1 # binary[(gradient_threshold == 1)] = 1 self._write_image(binary) self._binary = cv2.imread('out.jpg', 0) print('Created image') self._image.value = self._binary self._image.repaint()
def thresholding(img): #print(img.shape) #setting all sorts of thresholds #cv2.imshow("orig", img) x_thresh = utils.abs_sobel_thresh(img, orient='x', thresh_min=10, thresh_max=230) #cv2.imshow("x_thresh",x_thresh*255) mag_thresh = utils.mag_thresh(img, sobel_kernel=3, mag_thresh=(30, 150)) #cv2.imshow("mag_thresh",mag_thresh*255) dir_thresh = utils.dir_threshold(img, sobel_kernel=3, thresh=(0.7, 1.3)) #cv2.imshow("dir_thresh",dir_thresh*255) hls_thresh = utils.hls_select(img, thresh=(180, 255)) #cv2.imshow("hls_thresh",hls_thresh*255) lab_thresh = utils.lab_select(img, thresh=(155, 200)) #cv2.imshow("lab_thresh",lab_thresh*255) luv_thresh = utils.luv_select(img, thresh=(225, 255)) #cv2.imshow("luv_thresh",luv_thresh) #Thresholding combination threshholded = np.zeros_like(x_thresh) threshholded[((x_thresh == 1) & (mag_thresh == 1)) | ((dir_thresh == 1) & (hls_thresh == 1)) | (lab_thresh == 1) | (luv_thresh == 1)] = 1 #cv2.imshow("threshholded", threshholded*255) #cv2.waitKey(0) return threshholded
def thresholding(img): # x_thresh = utils.abs_sobel_thresh(img, orient='x', thresh_min=55, thresh_max=100) # mag_thresh = utils.mag_thresh(img, sobel_kernel=3, mag_thresh=(70, 255)) # dir_thresh = utils.dir_threshold(img, sobel_kernel=3, thresh=(0.7, 1.3)) # s_thresh = utils.hls_select(img,channel='s',thresh=(160, 255)) # s_thresh_2 = utils.hls_select(img,channel='s',thresh=(200, 240)) # # white_mask = utils.select_white(img) # yellow_mask = utils.select_yellow(img) x_thresh = utils.abs_sobel_thresh(img, orient='x', thresh_min=10, thresh_max=230) mag_thresh = utils.mag_thresh(img, sobel_kernel=3, mag_thresh=(30, 150)) dir_thresh = utils.dir_threshold(img, sobel_kernel=3, thresh=(0.7, 1.3)) hls_thresh = utils.hls_select(img, thresh=(180, 255)) lab_thresh = utils.lab_select(img, thresh=(155, 200)) luv_thresh = utils.luv_select(img, thresh=(225, 255)) #Thresholding combination threshholded = np.zeros_like(x_thresh) threshholded[((x_thresh == 1) & (mag_thresh == 1)) | ((dir_thresh == 1) & (hls_thresh == 1)) | (lab_thresh == 1) | (luv_thresh == 1)] = 1 # threshholded = np.zeros_like(x_thresh) # threshholded[((x_thresh == 1)) | ((mag_thresh == 1) & (dir_thresh == 1))| (white_mask>0)|(s_thresh == 1) ]=1 return threshholded
def thresholding(img): x_thresh = utils.abs_sobel_thresh(img, orient='x', thresh_min=10, thresh_max=230) mag_thresh = utils.mag_thresh(img, sobel_kernel=3, mag_thresh=(30, 150)) dir_thresh = utils.dir_threshold(img, sobel_kernel=3, thresh=(0.7, 1.3)) hls_thresh = utils.hls_select(img, thresh=(180, 255)) lab_thresh = utils.lab_select(img, thresh=(155, 200)) luv_thresh = utils.luv_select(img, thresh=(225, 255)) #Thresholding combination threshholded = np.zeros_like(x_thresh) threshholded[((x_thresh == 1) & (mag_thresh == 1)) | ((dir_thresh == 1) & (hls_thresh == 1)) | (lab_thresh == 1) | (luv_thresh == 1)] = 1 return threshholded
def processing(img, M, Minv, left_line, right_line): prev_time = time.time() img = Image.fromarray(img) undist = img #get the thresholded binary image img = np.array(img) blur_img = cv2.GaussianBlur(img, (3, 3), 0) Sobel_x_thresh = utils.abs_sobel_thresh(blur_img, orient='x', thresh_min=90, thresh_max=255) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) dilated = cv2.dilate(Sobel_x_thresh, kernel, iterations=2) #perform perspective transform thresholded_wraped = cv2.warpPerspective(dilated, M, img.shape[1::-1], flags=cv2.INTER_LINEAR) #perform detection if left_line.detected and right_line.detected: left_fit, right_fit = utils.find_line_by_previous( thresholded_wraped, left_line.current_fit, right_line.current_fit) else: left_fit, right_fit = utils.find_line(thresholded_wraped) left_line.update(left_fit) right_line.update(right_fit) # #draw the detected laneline and the information undist = Image.fromarray(img) area_img = utils.draw_area(undist, thresholded_wraped, Minv, left_fit, right_fit) area_img = np.array(area_img) curr_time = time.time() exec_time = curr_time - prev_time info = "time: %.2f ms" % (1000 * exec_time) print(info) return area_img, thresholded_wraped
img = utils.cal_undistort(img,object_points,img_points) undistorted.append(img) trans_on_test=[] for img in undistorted: src = np.float32([[(203, 720), (585, 460), (695, 460), (1127, 720)]]) dst = np.float32([[(320, 720), (320, 0), (960, 0), (960, 720)]]) M = cv2.getPerspectiveTransform(src, dst) trans = cv2.warpPerspective(img, M, img.shape[1::-1], flags=cv2.INTER_LINEAR) trans_on_test.append(trans) thresh = [] binary_wrapeds = [] histogram = [] for img in undistorted: x_thresh = utils.abs_sobel_thresh(img, orient='x', thresh_min=55, thresh_max=100) mag_thresh = utils.mag_thresh(img, sobel_kernel=3, mag_thresh=(70, 255)) dir_thresh = utils.dir_threshold(img, sobel_kernel=3, thresh=(0.7, 1.3)) s_thresh = utils.hls_select(img,channel='s',thresh=(160, 255)) s_thresh_2 = utils.hls_select(img,channel='s',thresh=(200, 240)) white_mask = utils.select_white(img) yellow_mask = utils.select_yellow(img) combined = np.zeros_like(mag_thresh) # combined[(x_thresh==1) | ((mag_thresh == 1) & (dir_thresh == 1)) | (s_thresh==1)] = 1 # combined[((mag_thresh == 1) & (dir_thresh == 1))] = 1 combined[((x_thresh == 1) | (s_thresh == 1)) | ((mag_thresh == 1) & (dir_thresh == 1))| (white_mask>0)|(s_thresh_2 == 1) ]=1 src = np.float32([[(203, 720), (585, 460), (695, 460), (1127, 720)]]) dst = np.float32([[(320, 720), (320, 0), (960, 0), (960, 720)]])
def detect(fname): image = mpimg.imread(fname + '.jpeg') height, width = image.shape[:2] image = cv2.resize(image, (1280, 720))[:, :, :3] image_original = image kernel_size = 5 img_size = np.shape(image) ht_window = np.uint(img_size[0] / 1.5) hb_window = np.uint(img_size[0]) c_window = np.uint(img_size[1] / 2) ctl_window = c_window - .36 * np.uint(img_size[1] / 2) ctr_window = c_window + .36 * np.uint(img_size[1] / 2) cbl_window = c_window - 0.9 * np.uint(img_size[1] / 2) cbr_window = c_window + 0.9 * np.uint(img_size[1] / 2) src = np.float32([[cbl_window, hb_window], [cbr_window, hb_window], [ctr_window, ht_window], [ctl_window, ht_window]]) dst = np.float32([[0, img_size[0]], [img_size[1], img_size[0]], [img_size[1], 0], [0, 0]]) warped, M_warp, Minv_warp = utils.warp_image(image, src, dst, (img_size[1], img_size[0])) image_HSV = cv2.cvtColor(warped, cv2.COLOR_RGB2HSV) yellow_hsv_low = np.array([0, 100, 100]) yellow_hsv_high = np.array([80, 255, 255]) res_mask = utils.color_mask(image_HSV, yellow_hsv_low, yellow_hsv_high) res = utils.apply_color_mask(image_HSV, warped, yellow_hsv_low, yellow_hsv_high) image_HSV = cv2.cvtColor(warped, cv2.COLOR_RGB2HSV) white_hsv_low = np.array([0, 0, 160]) white_hsv_high = np.array([255, 80, 255]) res1 = utils.apply_color_mask(image_HSV, warped, white_hsv_low, white_hsv_high) mask_yellow = utils.color_mask(image_HSV, yellow_hsv_low, yellow_hsv_high) mask_white = utils.color_mask(image_HSV, white_hsv_low, white_hsv_high) mask_lane = cv2.bitwise_or(mask_yellow, mask_white) image = utils.gaussian_blur(warped, kernel=5) image_HLS = cv2.cvtColor(warped, cv2.COLOR_RGB2HLS) img_gs = image_HLS[:, :, 1] sobel_c = utils.sobel_combined(img_gs) img_abs_x = utils.abs_sobel_thresh(img_gs, 'x', 5, (50, 225)) img_abs_y = utils.abs_sobel_thresh(img_gs, 'y', 5, (50, 225)) wraped2 = np.copy(cv2.bitwise_or(img_abs_x, img_abs_y)) img_gs = image_HLS[:, :, 2] sobel_c = utils.sobel_combined(img_gs) img_abs_x = utils.abs_sobel_thresh(img_gs, 'x', 5, (50, 255)) img_abs_y = utils.abs_sobel_thresh(img_gs, 'y', 5, (50, 255)) wraped3 = np.copy(cv2.bitwise_or(img_abs_x, img_abs_y)) image_cmb = cv2.bitwise_or(wraped2, wraped3) image_cmb = utils.gaussian_blur(image_cmb, 3) image_cmb = cv2.bitwise_or(wraped2, wraped3) image_cmb1 = np.zeros_like(image_cmb) image_cmb1[(mask_lane >= .5) | (image_cmb >= .5)] = 1 mov_filtsize = img_size[1] / 50. mean_lane = np.mean(image_cmb1[img_size[0] / 2:, :], axis=0) indexes = find_peaks_cwt(mean_lane, [100], max_distances=[800]) window_size = 50 val_ind = np.array([mean_lane[indexes[i]] for i in range(len(indexes))]) ind_sorted = np.argsort(-val_ind) ind_peakR = indexes[ind_sorted[0]] ind_peakL = indexes[ind_sorted[1]] if ind_peakR < ind_peakL: ind_temp = ind_peakR ind_peakR = ind_peakL ind_peakL = ind_temp n_vals = 8 ind_min_L = ind_peakL - 50 ind_max_L = ind_peakL + 50 ind_min_R = ind_peakR - 50 ind_max_R = ind_peakR + 50 mask_L_poly = np.zeros_like(image_cmb1) mask_R_poly = np.zeros_like(image_cmb1) ind_peakR_prev = ind_peakR ind_peakL_prev = ind_peakL for i in range(8): img_y1 = img_size[0] - img_size[0] * i / 8 img_y2 = img_size[0] - img_size[0] * (i + 1) / 8 mean_lane_y = np.mean(image_cmb1[img_y2:img_y1, :], axis=0) indexes = find_peaks_cwt(mean_lane_y, [100], max_distances=[800]) if len(indexes) > 1.5: val_ind = np.array( [mean_lane[indexes[i]] for i in range(len(indexes))]) ind_sorted = np.argsort(-val_ind) ind_peakR = indexes[ind_sorted[0]] ind_peakL = indexes[ind_sorted[1]] if ind_peakR < ind_peakL: ind_temp = ind_peakR ind_peakR = ind_peakL ind_peakL = ind_temp else: if len(indexes) == 1: if np.abs(indexes[0] - ind_peakR_prev) < np.abs(indexes[0] - ind_peakL_prev): ind_peakR = indexes[0] ind_peakL = ind_peakL_prev else: ind_peakL = indexes[0] ind_peakR = ind_peakR_prev else: ind_peakL = ind_peakL_prev ind_peakR = ind_peakR_prev if np.abs(ind_peakL - ind_peakL_prev) >= 100: ind_peakL = ind_peakL_prev if np.abs(ind_peakR - ind_peakR_prev) >= 100: ind_peakR = ind_peakR_prev mask_L_poly[img_y2:img_y1, ind_peakL - window_size:ind_peakL + window_size] = 1. mask_R_poly[img_y2:img_y1, ind_peakR - window_size:ind_peakR + window_size] = 1. ind_peakL_prev = ind_peakL ind_peakR_prev = ind_peakR mask_L_poly, mask_R_poly = utils.get_initial_mask(image_cmb1, 50, mean_lane) mask_L = mask_L_poly img_L = np.copy(image_cmb1) img_L = cv2.bitwise_and(img_L, img_L, mask=mask_L_poly) mask_R = mask_R_poly img_R = np.copy(image_cmb1) img_R = cv2.bitwise_and(img_R, img_R, mask=mask_R_poly) vals = np.argwhere(img_L > .5) all_x = vals.T[0] all_y = vals.T[1] left_fit = np.polyfit(all_x, all_y, 2) left_y = np.arange(11) * img_size[0] / 10 left_fitx = left_fit[0] * left_y**2 + left_fit[1] * left_y + left_fit[2] vals = np.argwhere(img_R > .5) all_x = vals.T[0] all_y = vals.T[1] right_fit = np.polyfit(all_x, all_y, 2) right_y = np.arange(11) * img_size[0] / 10 right_fitx = right_fit[0] * right_y**2 + right_fit[ 1] * right_y + right_fit[2] window_sz = 20 mask_L_poly = np.zeros_like(image_cmb1) mask_R_poly = np.zeros_like(image_cmb1) left_pts = [] right_pts = [] pt_y_all = [] for i in range(8): img_y1 = img_size[0] - img_size[0] * i / 8 img_y2 = img_size[0] - img_size[0] * (i + 1) / 8 pt_y = (img_y1 + img_y2) / 2 pt_y_all.append(pt_y) left_pt = np.round(left_fit[0] * pt_y**2 + left_fit[1] * pt_y + left_fit[2]) right_pt = np.round(right_fit[0] * pt_y**2 + right_fit[1] * pt_y + right_fit[2]) right_pts.append(right_fit[0] * pt_y**2 + right_fit[1] * pt_y + right_fit[2]) left_pts.append(left_fit[0] * pt_y**2 + left_fit[1] * pt_y + left_fit[2]) warp_zero = np.zeros_like(image_cmb1).astype(np.uint8) color_warp = np.dstack((warp_zero, warp_zero, warp_zero)) pts_left = np.array([np.transpose(np.vstack([left_fitx, left_y]))]) pts_right = np.array( [np.flipud(np.transpose(np.vstack([right_fitx, right_y])))]) pts = np.hstack((pts_left, pts_right)) cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 255)) col_L = (255, 255, 0) col_R = (255, 255, 255) utils.draw_pw_lines(color_warp, np.int_(pts_left), col_L) utils.draw_pw_lines(color_warp, np.int_(pts_right), col_R) newwarp = cv2.warpPerspective(color_warp, Minv_warp, (image.shape[1], image.shape[0])) result = cv2.addWeighted(image_original, 1, newwarp, 0.5, 0) grid = [] coordinates = [] a = [[left_fitx[i], i * 72] for i in range(0, 11)] b = [[right_fitx[i], i * 72] for i in range(0, 11)] c = np.concatenate([a, b]) c = np.array([c], dtype='float32') coordinates = cv2.perspectiveTransform(c, Minv_warp)[0] return coordinates, result