def test_rotated_lena(): """ The harris filter should yield the same results with an image and it's rotation. """ im = img_as_float(data.lena().mean(axis=2)) results = harris(im) im_rotated = im.T results_rotated = harris(im_rotated) assert (np.sort(results[:, 0]) == np.sort(results_rotated[:, 1])).all() assert (np.sort(results[:, 1]) == np.sort(results_rotated[:, 0])).all()
def test_noisy_square_image(): im = np.zeros((50, 50)).astype(float) im[:25, :25] = 1. im = im + np.random.uniform(size=im.shape) * .5 results = harris(im) assert results.any() assert len(results) == 1
def test_squared_dot(): im = np.zeros((50, 50)) im[4:8, 4:8] = 1 im = img_as_float(im) results = harris(im, min_distance=3) print results assert (results == np.array([[6, 6]])).all()
def guess_corners(bw): """ Infer the corners of an image using a Sobel filter to find the edges and a Harris filter to find the corners. Takes only as single color chanel. Parameters ---------- bw : (m x n) ndarray of ints Returns ------- corners : pixel coordinates of plot corners, unsorted outline : (m x n) ndarray of bools True -> plot area """ assert len(bw.shape) == 2 bw = img_as_uint(bw) e_map = ndimage.sobel(bw) markers = np.zeros(bw.shape, dtype=int) markers[bw < 30] = 1 markers[bw > 150] = 2 seg = ndimage.watershed_ift(e_map, np.asarray(markers, dtype=int)) outline = ndimage.binary_fill_holes(1 - seg) corners = harris(np.asarray(outline, dtype=int)) corners = approximate_polygon(corners, 1) return corners, outline
def get_wire_tip_coordinates(self): "this should actually find the tip with the harris algorithm" y, x = numpy.transpose( feature.harris(self.image, min_distance=100, threshold=0.3, gaussian_deviation=5)) n = x.size x, y = x[0], y[0] return x, y, n
def harris(parameters): """Harris interest point operator. It wraps `skimage.feature.harris`. The `threshold`, `eps` and `gaussian_deviation` options are not supported. This function returns an array of 0s and 1s. Harris points are marked with 1s. This way the result can be easily transformed to an image. It works on RGB and greyscale images. The wrapped function returns a set of point coordinates in a list. For some reason it is not possible to do something like: >>> points = feature.harris(data, min_distance=5) >>> data[points] = 1 so a for loop is used. .. note:: The coordinates returned are not directly on the corner, but a pixel inside the object (TODO: is this expected?). :param parameters['data'][0]: input array :type parameters['data'][0]: numpy.array :param parameters['min_distance']: minimum number of pixels separating interest points and image boundary, defaults to 10 :type parameters['min_distance']: float :return: numpy.array, it contains 1s where points were found, otherwise 0 """ data = parameters['data'][0] min_distance = parameters.get('min_distance', 10) points = feature.harris(data, min_distance=min_distance) result = numpy.zeros((data.shape[0], data.shape[1]), dtype='uint8') for point in points: result[point[0], point[1]] = 1 return result
from matplotlib import pyplot as plt from skimage import data, img_as_float from skimage.feature import harris def plot_harris_points(image, filtered_coords): """ plots corners found in image""" plt.imshow(image) y, x = np.transpose(filtered_coords) plt.plot(x, y, 'b.') plt.axis('off') # display results plt.figure(figsize=(8, 3)) im_lena = img_as_float(data.lena()) im_text = img_as_float(data.text()) filtered_coords = harris(im_lena, min_distance=4) plt.axes([0, 0, 0.3, 0.95]) plot_harris_points(im_lena, filtered_coords) filtered_coords = harris(im_text, min_distance=4) plt.axes([0.2, 0, 0.77, 1]) plot_harris_points(im_text, filtered_coords) plt.show()
def extract_features(image_path_list): ''' Our feature extraction function. It takes in a list of image paths, does some measurement on each image, then returns a list of the image paths paired with the results of the feature measurement. ''' feature_list = [] for image_path in image_path_list: image_array = imread(image_path) if len(image_array.shape) != 3: image_array = color.gray2rgb(image_array) # Create a sub-image of the center. Useful for color of subject. height, width = image_array.shape[0], image_array.shape[1] central_y, central_x = int(round(height/2.0)), int(round(width/2.0)) central_image_array = image_array[central_y-height/10 : central_y+height/10, central_x-width/10 : central_x+width/10, :] # crop to square integer 16x16 size new_height, new_width = 16*(height/16), 16*(width/16) if new_height < new_width: new_width = new_height elif new_height > new_width: new_height = new_width cropped_image_array = image_array[central_y-new_height/2 : central_y+new_height/2, central_x-new_width/2 : central_x+new_width/2, :] #Equalize the image image_array_gray_equalized = exposure.equalize(color.rgb2gray(cropped_image_array)) # Now zoom it to 128x128 for easier shape processing small_square_gray_image = ndimage.interpolation.zoom(image_array_gray_equalized, 128.0/new_height) # Create sobel filtered image sobel_image = ndimage.filters.sobel(image_array) # (1) Number of pixels in the image (super simple) n_pixels = image_array.size # (2) Median red value med_red_val = median(central_image_array[:,:,0]) # (3) Median green value med_green_val = median(central_image_array[:,:,1]) # (4) Median blue value med_blue_val = median(central_image_array[:,:,2]) # (5) Standard Deviation of red value std_red_val = central_image_array[:,:,0].std() # (6) Standard Deviation of green value std_green_val = central_image_array[:,:,1].std() # (7) Standard Deviation of blue value std_blue_val = central_image_array[:,:,2].std() # (8) Image aspect ratio (width/height) aspect_ratio = float(width)/height # (9) Histogram Otsu thershold otsu_thresh = filter.threshold_otsu(image_array) # (10, 11, 12) Center of Mass center_of_mass_x, center_of_mass_y, center_of_mass_value = ndimage.measurements.center_of_mass(image_array) # (13) Ratio of sum of bottom half vs top half of pixels vertical_ratio = small_square_gray_image[:64,:].sum()/small_square_gray_image[64:,:].sum() # (14) Ratio of sum of left half vs right half of pixels horizontal_ratio = small_square_gray_image[:,:64].sum()/small_square_gray_image[:,64:].sum() # (15, 16, 17) Location of maximum max_x, max_y, max_band = ndimage.measurements.maximum_position(image_array) # (18, 19, 20) Location of minimum min_x, min_y, min_band = ndimage.measurements.minimum_position(image_array) # (21) Median sobel red value med_sobel_red_val = median(sobel_image[:,:,0]) # (22) Median sobel green value med_sobel_green_val = median(sobel_image[:,:,1]) # (23) Median sobel blue value med_sobel_blue_val = median(sobel_image[:,:,2]) # (24) Standard Deviation of sobel red value std_sobel_red_val = sobel_image[:,:,0].std() # (25) Standard Deviation of sobel green value std_sobel_green_val = sobel_image[:,:,1].std() # (26) Standard Deviation of sobel blue value std_sobel_blue_val = sobel_image[:,:,2].std() # (27) Number of Harris points n_harris_points = feature.harris(small_square_gray_image, min_distance=6).size # (28) Mean of Histogram of Gradients (HOG) fd = feature.hog(image_array_gray_equalized, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=False) mean_hog = fd.mean() # (29) Standard Deviation of Histogram of Gradients (HOG) std_hog = fd.std() # (30) Ratio of white pixels in skeletonized image edge_image = filter.canny(image_array_gray_equalized, sigma=1) skeleton_image = morphology.skeletonize(edge_image) skeleton_ratio = float(skeleton_image.sum())/skeleton_image.size feature_list.append([image_path, n_pixels, med_red_val, med_green_val, med_blue_val, std_red_val, std_green_val, std_blue_val, aspect_ratio, otsu_thresh, center_of_mass_x, center_of_mass_y, center_of_mass_value, vertical_ratio, horizontal_ratio, max_x, max_y, max_band, min_x, min_y, min_band, med_sobel_red_val, med_sobel_green_val, med_sobel_blue_val, std_sobel_red_val, std_sobel_green_val, std_sobel_blue_val, n_harris_points, mean_hog, std_hog, skeleton_ratio]) return feature_list
from sklearn.datasets import load_sample_images from matplotlib.pyplot import imshow, show, axis, plot import numpy as np from skimage.feature import harris dataset = load_sample_images() img = dataset.images[0] harris_coords = harris(img) print "Harris coords shape", harris_coords.shape y, x = np.transpose(harris_coords) axis('off') imshow(img) plot(x, y, 'ro') show()
def test_squared_dot(): im = np.zeros((50, 50)) im[4:8, 4:8] = 1 im = img_as_float(im) results = harris(im, min_distance=3) assert (results == np.array([[6, 6]])).all()
def test_square_image(): im = np.zeros((50, 50)).astype(float) im[:25, :25] = 1. results = harris(im) assert results.any() assert len(results) == 1
from skimage import data, img_as_float from skimage.feature import harris def plot_harris_points(image, filtered_coords): """ plots corners found in image""" plt.imshow(image) y, x = np.transpose(filtered_coords) plt.plot(x, y, 'b.') plt.axis('off') # display results plt.figure(figsize=(8, 3)) im_lena = img_as_float(data.lena()) im_text = img_as_float(data.text()) filtered_coords = harris(im_lena, min_distance=4) plt.axes([0, 0, 0.3, 0.95]) plot_harris_points(im_lena, filtered_coords) filtered_coords = harris(im_text, min_distance=4) plt.axes([0.2, 0, 0.77, 1]) plot_harris_points(im_text, filtered_coords) plt.show()
import numpy as np from matplotlib import pyplot as plt from skimage import data, img_as_float from skimage.feature import harris def plot_harris_points(image, filtered_coords): plt.imshow(image) y, x = np.transpose(filtered_coords) plt.plot(x, y, 'b.') plt.axis('off') # resultados plt.figure(figsize=(8, 3)) image = img_as_float(data.load("c.jpeg")) filtered_coords = harris(image, min_distance=4) plt.axes([0.2, 0, 0.77, 1]) plot_harris_points(image, filtered_coords) plt.show()
from sklearn.datasets import load_sample_images from matplotlib.pyplot import imshow, show, axis, plot import numpy from skimage.feature import harris dataset = load_sample_images() img = dataset.images[0] harris_coords = harris(img) print "Harris coords shape", harris_coords.shape y, x = numpy.transpose(harris_coords) axis('off') imshow(img) plot(x, y, 'ro') show()
The Harris corner filter [1]_ detects "interest points" [2]_ using edge detection in multiple directions. .. [1] http://en.wikipedia.org/wiki/Corner_detection .. [2] http://en.wikipedia.org/wiki/Interest_point_detection """ from matplotlib import pyplot as plt from skimage import data, img_as_float from skimage.feature import harris def plot_harris_points(image, filtered_coords): """ plots corners found in image""" plt.plot() plt.imshow(image) plt.plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], 'b.') plt.axis('off') plt.show() im = img_as_float(data.lena()) filtered_coords = harris(im, min_distance=6) plot_harris_points(im, filtered_coords)