def _featurize(self, img): '''Featurizes the input image using VGG-16. The image is resized to 224 x 224 internally, if necessary. Args: img: the input image Returns: the feature vector, a 1D array of length 4096 ''' if etai.is_gray(img): img = etai.gray_to_rgb(img) elif etai.has_alpha(img): img = img[:, :, :3] imgs = [etai.resize(img, 224, 224)] return self.vgg16.evaluate(imgs, layer=self.vgg16.fc2l)[0]
def _perform_convolution(convolution_config): '''Performs convolution of an input image with a kernel specified by the configuration parameters, and writes the result to the path specified by "filtered_image". Args: convolution_config: the configuration file for the module ''' kernel_type = convolution_config.parameters.kernel_type if kernel_type == "x_derivative": kernel = _create_x_derivative_kernel() elif kernel_type == "y_derivative": kernel = _create_y_derivative_kernel() elif kernel_type == "sobel_vertical": kernel = _create_sobel_vertical_kernel() elif kernel_type == "sobel_horizontal": kernel = _create_sobel_horizontal_kernel() else: # this will be the Gaussian kernel # (make sure to remove this comment!) kernel = _create_gaussian_kernel( convolution_config.parameters.gaussian_sigma) for data in convolution_config.data: in_img = etai.read(data.input_image) if convolution_config.parameters.image_type == "grayscale": in_img = etai.rgb_to_gray(in_img) else: # if the image should be a color image, convert the grayscale # image to color (simply converts the image into a 3-channel # image) if etai.is_gray(in_img): in_img = etai.gray_to_rgb(in_img) if convolution_config.parameters.image_max_range == 1: in_img = (in_img.astype(float)) / 255.0 filtered_image = _convolve(kernel, in_img) if data.filtered_matrix: etau.ensure_basedir(data.filtered_matrix) np.savez(data.filtered_matrix, filtered_matrix=filtered_image) if data.filtered_image: etau.ensure_basedir(data.filtered_image) etai.write(filtered_image, data.filtered_image)
def _visualize_corners(in_img, corner_locations): '''Creates an image that shows the detected corners. Args: in_img: the original image corner_locations: the locations of each detected corner in the image, stored in an Nx2 matrix Returns: out_img: an image with the detected corners colored in red ''' if etai.is_gray(in_img): out_img = etai.gray_to_rgb(in_img) else: out_img = in_img.copy() for i in range(corner_locations.shape[0]): cv2.circle(out_img, (int(corner_locations[i][0]), int(corner_locations[i][1])), 2, (255, 0, 0), -1) return out_img