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 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 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): 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
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)]]) M = cv2.getPerspectiveTransform(src, dst) binary_warped = cv2.warpPerspective(combined, M, img.shape[1::-1], flags=cv2.INTER_LINEAR)
def detect(self, img, debug=False): img = self._cc.undistort(img) # Convert to a single layer warped_img, (src, dst) = warper(img, top_c_shift=self._center_shift, top_v_shift=self._vertical_shift) # Detect edges # 1. Define color filters gray = cv2.cvtColor(warped_img, cv2.COLOR_RGB2GRAY) s = cv2.cvtColor(warped_img, cv2.COLOR_RGB2HLS)[:, :, 0] sb = np.zeros_like(s, dtype=np.uint8) sb[(s >= self._h_thresh[0]) & (s < self._h_thresh[1])] = 1 y = cv2.cvtColor(warped_img, cv2.COLOR_RGB2YUV)[:, :, 0] yb = np.zeros_like(y, dtype=np.uint8) yb[(y >= self._y_thresh[0]) & (y < self._y_thresh[1])] = 1 # 2. Overlay a direction filter dir_gradient = dir_threshold(gray, kernel=self._kernel, thresh=self._dir_thresh) # 3. Combine results of color and direction filters combined = np.zeros_like(gray, dtype=np.uint8) combined[((sb == 1) & (dir_gradient == 1)) | (yb == 1)] = 1 # 4. Remove noise on the far left side of the image combined[:, :250] = 0 # find curvature of the lanes try: # 1. Find centroids centroids = find_window_centroids(combined, self._window_width, self._window_height, self._search_margin) # 2. Get pixels for left and right lines associated with identified centroids l_pixels, r_pixels = map_window(combined, centroids, self._window_width, self._window_height) # 3. Calculate curve coefficients self._left_lane.current_fit = np.polyfit(l_pixels[0], l_pixels[1], 2) self._right_lane.current_fit = np.polyfit(r_pixels[0], r_pixels[1], 2) # draw curves describing lanes self._left_lane.allx, self._left_lane.ally = plot_2nd_degree_polynomial( self._left_lane.best_fit, warped_img.shape[0]) self._right_lane.allx, self._right_lane.ally = plot_2nd_degree_polynomial( self._right_lane.best_fit, warped_img.shape[0]) # Calculating radius of curvature # Find fit in real-world coordinates left_fit = np.polyfit(l_pixels[0] * self._y_scaller, l_pixels[1] * self._x_scaller, 2) right_fit = np.polyfit(r_pixels[0] * self._y_scaller, r_pixels[1] * self._x_scaller, 2) # Calucate radius self._left_lane.radius_of_curvature = R( left_fit, self._left_lane.ally).mean() self._right_lane.radius_of_curvature = R( right_fit, self._right_lane.ally).mean() # Assign coordinates of lines self._left_lane.line_base_pos = centroids[0][0] self._right_lane.line_base_pos = centroids[0][1] except: self._left_lane.current_fit = self._left_lane.best_fit self._right_lane.current_fit = self._right_lane.best_fit # estimate car position car_position = (warped_img.shape[1] - self._left_lane.line_base_pos - self._right_lane.line_base_pos) / 2 # Visualize result binary_filled = visualize_lanes(combined, self._left_lane.ally, self._left_lane.allx, self._right_lane.allx) unwarp_binary, (src, dst) = warper(binary_filled, src=dst, dst=src) mapped_img = cv2.addWeighted(img, 1, unwarp_binary, 0.3, 0) cv2.putText(mapped_img, "Radius of curvature: {}m".format( (self._left_lane.radius_of_curvature + self._right_lane.radius_of_curvature) // 2), (5, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), thickness=1) cv2.putText(mapped_img, "Vehicle is {:.1f}m {} of center".format(np.abs(car_position * self._x_scaller), "left" if car_position < 0 \ else "right"), (int(mapped_img.shape[1] / 2), 30), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), thickness=1) if debug: # Visualize steps for debugging mapped_img_ = cv2.resize( mapped_img, (mapped_img.shape[1] // 2, mapped_img.shape[0] // 2)) sb = cv2.merge((sb, sb, sb)) * 255 sb = cv2.resize(sb, (sb.shape[1] // 2, sb.shape[0] // 2)) yb = cv2.merge((yb, yb, yb)) * 255 yb = cv2.resize(yb, (yb.shape[1] // 2, yb.shape[0] // 2)) combined = cv2.merge((combined, combined, combined)) * 255 combined = cv2.resize( combined, (combined.shape[1] // 2, combined.shape[0] // 2)) mapped_img = np.zeros_like(img, dtype=np.uint8) mapped_img[:sb.shape[0], :sb.shape[1]] = sb mapped_img[:yb.shape[0], yb.shape[1]:] = yb mapped_img[combined.shape[0]:, :combined.shape[1]] = combined mapped_img[mapped_img_.shape[0]:, mapped_img_.shape[1]:] = mapped_img_ return mapped_img, (self._left_lane, self._right_lane)