def find_corners(self, img): """ Finds the chessboard corners of a image """ gray = utils.grayscale(img) ret, corners = cv2.findChessboardCorners(gray, (self.nx, self.ny), None) return ret, corners, gray
def gray_threshold(self, img, thresh=(130, 255)): """ Grayscales and threshold an image. Attributes: img: the image. thresh: threshold lower and upper bounds tuple. """ gray = utils.grayscale(img) return self.threshold(gray, thresh)
def unwarp(self, img, grayscale=False): """ Unwarps an image """ if grayscale: gray = utils.grayscale(img) else: gray = img img_size = (gray.shape[1], gray.shape[0]) M, Minv = self.get_matrices() return cv2.warpPerspective(img, Minv, img_size, flags=cv2.INTER_LINEAR)
def direction(self, img, kernel=3, thresh=(0.7, 1.3)): """ Applies sobel x and y, computes the direction of the gradient and then applies a threshold. Attributes: img: the image. kernel: The sobel kernel. thresh: threshold lower and upper bounds tuple. """ gray = utils.grayscale(img) # Take the gradient in x and y separately sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=kernel) sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=kernel) # Take the absolute value of the x and y gradients absgraddir = np.arctan2(np.absolute(sobely), np.absolute(sobelx)) return self.threshold(absgraddir, thresh)
def sobel(self, img, orient='x', kernel=3, thresh=(20, 100)): """ Applies sobel filter in the *orient* direction. Attributes: img: the image. orient: orientation for to apply the filter. thresh: threshold lower and upper bounds tuple. """ gray = utils.grayscale(img) if orient == 'x': sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=kernel) else: sobel = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=kernel) # Take the absolute value of the derivative or gradient abs_sobel = np.absolute(sobel) # Scale to 8-bit (0 - 255) then convert to type = np.uint8 scaled_sobel = np.uint8(255 * abs_sobel / np.max(abs_sobel)) return self.threshold(scaled_sobel, thresh)
def grad_magnitude(self, img, kernel=3, thresh=(30, 100)): """ Applies sobel on both orientations (x and y) and then computes the gradient magnitude and applies a threshold. Attributes: img: the image. kernel: The sobel kernel. thresh: threshold lower and upper bounds tuple. """ gray = utils.grayscale(img) # Take the gradient in x and y separately sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=kernel) sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=kernel) # Calculate the magnitude gradmag = np.sqrt(sobelx**2 + sobely**2) # Scale to 8-bit (0 - 255) and convert to type = np.uint8 scale_factor = np.max(gradmag) / 255 gradmag = (gradmag / scale_factor).astype(np.uint8) return self.threshold(gradmag, thresh)