def emboss3d(image, azimuth=np.pi / 2, elevation=np.pi / 4, depth=10): image = vision.bgr_to_gray(image) image = image.astype("float") gradients = np.gradient(image) grad_x, grad_y = gradients # projection of unit vector to light source on to ground ground_length = np.cos(elevation) # the components in x and y directions ground_x = ground_length * np.cos(azimuth) ground_y = ground_length * np.sin(azimuth) # projection of unit vector to light source in z direction up_z = np.sin(elevation) # adjusting the gradients by depth factor grad_x = grad_x * depth / 100 grad_y = grad_y * depth / 100 # gradient magnitudes [add a little extra to ensure magnitude is non-zero] mag = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1) unit_x = grad_x / mag unit_y = grad_y / mag unit_z = 1.0 / mag # compute the projection of gradient to the direction of light source projection = ground_x * unit_x + ground_y * unit_y + up_z * unit_z # map to 0-255 range projection = 255 * projection projection = projection.clip(0, 255) projection = projection.astype("uint8") return projection
def cartoonize(image, kernel_size=5): """Constructs a black and white cartoon version of image :param image: Input image in BGR or monochrome format :type image: array_like :param kernel_size: Size of Laplacian filter kernel, defaults to 3 :type kernel_size: int, optional :return: Cartoonized image :rtype: ndarray """ # Convert to gray scale if vision.is_3channel(image): image = vision.bgr_to_gray(image) # Apply median filtering image = cv2.medianBlur(image, 7) # Detect edges in the image edges = cv2.Laplacian(image, cv2.CV_8U, ksize=kernel_size) # Edges are where the Laplacian is small # We prepare an edge mask for small values mask = vision.threshold_below(edges, 100) # We need to do some erosion to merge black edge points # prepare the structuring element se = np.ones((3, 3), np.uint8) # perform erosion mask = cv2.erode(mask, se, iterations=1) # get rid of isolated edge points again mask = cv2.medianBlur(mask, ksize=5) # This is our cartoonized image return mask
def sobel_energy_l2(image): '''Computes the energy matrix for the image using sobel gradients by computing the gradient magnitude''' image = vision.bgr_to_gray(image) # bring image to 0,1 range image = img_as_float(image) x_g = sobel_x(image) y_g = sobel_x(image) energy = np.sqrt(x_g*x_g + y_g*y_g) return energy
def sobel_energy_l1(image): '''Computes the energy matrix for the image using sobel gradients by taking their absolute sums''' image = vision.bgr_to_gray(image) # bring image to 0,1 range image = img_as_float(image) x_g = sobel_x(image, ddepth=cv2.CV_64F) y_g = sobel_x(image, ddepth=cv2.CV_64F) energy = np.absolute(x_g) + np.absolute(y_g) return energy
def _apply_filter(image, filter_name): filter = _SPECIAL_FILTERS[filter_name] if filter["gray"]: image = vision.bgr_to_gray(image) kernel = filter["kernel"] scale = filter["scale"] offset = filter["offset"] kernel = kernel / scale result = cv2.filter2D(image, -1, kernel, delta=offset) return result
def monochrome(image, format="bgr"): """Converts image to gray scale * If a monochrome image is provided as input, it will be returned as it is. * If a 2D array is provided as input, it will be returned as it is. :param image: An input image :type image: array_like :param format: Format of input image, defaults to 'bgr'. Supported formats: 'rgb', 'bgr' :type format: str, optional :return: Converted monochrome image :rtype: ndarray, 2D """ if format == 'rgb': return vision.rgb_to_gray(image) if format == 'bgr': return vision.bgr_to_gray(image) return image
''' Example for image translation ''' #pylint: disable=C0103 import os import cv2 from dirsetup import IMAGES_DIR from cr import vision as vision image_path = os.path.join(IMAGES_DIR, 'bookpage_dark_scan.jpg') image = cv2.imread(image_path) h, w, _ = image.shape gray_image = vision.bgr_to_gray(image) thresholded_image = vision.adaptive_threshold_gaussian(gray_image, block_size=115, constant=1) cv2.imshow('Original', image) cv2.moveWindow('Original', 10, 10) cv2.imshow('Corrected', thresholded_image) cv2.waitKey() cv2.destroyAllWindows()