From 2aca8cd791637bf91930365e22ab654e80337078 Mon Sep 17 00:00:00 2001 From: Vincent Albufera Date: Mon, 26 Mar 2012 15:39:59 +0200 Subject: [PATCH] Addition of examples in the harris and peak local max function --- doc/examples/plot_harris.py | 26 ++++----- doc/examples/plot_peak_local_max.py | 30 +++++----- skimage/data/__init__.py | 11 +++- skimage/feature/harris.py | 5 +- skimage/feature/peak.py | 87 +++++++++-------------------- 5 files changed, 67 insertions(+), 92 deletions(-) diff --git a/doc/examples/plot_harris.py b/doc/examples/plot_harris.py index 229be6e5..7daf6051 100644 --- a/doc/examples/plot_harris.py +++ b/doc/examples/plot_harris.py @@ -20,28 +20,24 @@ def plot_harris_points(image, filtered_coords): """ plots corners found in image""" plt.plot() - plt.imshow(image, cmap=plt.cm.gray) + plt.imshow(image) plt.plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], - 'r.') + 'b.') plt.axis('off') plt.show() # display results +plt.figure(figsize=(9, 3)) +im_lena = img_as_float(data.lena()) +im_text = img_as_float(data.text()) -plt.figure(figsize=(8, 6)) -im = img_as_float(data.lena()) -im2 = img_as_float(data.text()) +filtered_coords = harris(im_lena, min_distance=4) -filtered_coords = harris(im, min_distance=4) +plt.axes([0, 0, 0.3, 0.95]) +plot_harris_points(im_lena, filtered_coords) -plt.subplot(121) -plot_harris_points(im, filtered_coords) +filtered_coords = harris(im_text, min_distance=4) -filtered_coords = harris(im2, min_distance=4) - -plt.subplot(122) -plot_harris_points(im2, filtered_coords) - -plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, - bottom=0.02, left=0.02, right=0.98) +plt.axes([0.2, 0, 0.77, 1]) +plot_harris_points(im_text, filtered_coords) diff --git a/doc/examples/plot_peak_local_max.py b/doc/examples/plot_peak_local_max.py index 0c462303..9891141b 100644 --- a/doc/examples/plot_peak_local_max.py +++ b/doc/examples/plot_peak_local_max.py @@ -1,14 +1,15 @@ """ =============================================================================== -Peak local maximum +Finding local maxima =============================================================================== -The peak local maximum return coordinates of peaks in a image. The maximum -filter is used for finding the maximum peaks in the image. It dilates the -original image and is used within peak local max function to find the -coordinates of maximum peaks, comparing the dilated image with the original. -Then, the peak local max function returns the coordinates of points where -image = dilated image. +The peak local maximum function returns the coordinates of local peaks (maxima) +in a image. A maximum filter is used for finding local maxima. This operation +dilates the original image and merges neighboring local maxima closer than the +size of the dilation. Locations where the original image is equal to the +dilated image are returned as local maxima. + + """ from scipy import ndimage @@ -18,14 +19,13 @@ from skimage.feature import peak_local_max from skimage import data, img_as_float im = img_as_float(data.coins()) -image = im.copy() -# The image_max is the dilation of im with a 20*20 structuring element +# image_max is the dilation of im with a 20*20 structuring element # It is used within peak_local_max function -image_max = ndimage.maximum_filter(image, size=20, mode='constant') +image_max = ndimage.maximum_filter(im, size=20, mode='constant') -# Comparison between image_max and im to find the coordinates of maximum peaks -coordinates = peak_local_max(im, min_distance = 20) +# Comparison between image_max and im to find the coordinates of local maxima +coordinates = peak_local_max(im, min_distance=20) # display results plt.figure(figsize=(8, 3)) @@ -42,9 +42,9 @@ plt.title('Maximum filter') plt.subplot(133) plt.imshow(im, cmap=plt.cm.gray) a, b = im.shape -plt.plot([p[1] for p in coordinates],[p[0] for p in coordinates],'r.') -plt.xlim(0,b) -plt.ylim(a,0) +plt.plot([p[1] for p in coordinates], [p[0] for p in coordinates], 'r.') +plt.xlim(0, b) +plt.ylim(a, 0) plt.axis('off') plt.title('Peak local max') diff --git a/skimage/data/__init__.py b/skimage/data/__init__.py index bbef4f9b..1e5f96bb 100644 --- a/skimage/data/__init__.py +++ b/skimage/data/__init__.py @@ -45,7 +45,16 @@ def lena(): return load("lena.png") def text(): - """ Gray-level "text" image used for corner detection""" + """ Gray-level "text" image used for corner detection. + + Notes + ----- + This image was downloaded from Wikipedia + `__. + + No known copyright restrictions, released into the public domain. + + """ return load("text.png") diff --git a/skimage/feature/harris.py b/skimage/feature/harris.py index aa9f9ec7..6d0da9c0 100644 --- a/skimage/feature/harris.py +++ b/skimage/feature/harris.py @@ -80,7 +80,7 @@ def harris(image, min_distance=10, threshold=0.1, eps=1e-6, Examples ------- >>> square = np.zeros([10,10]) - >>> square[2:8,2:8]=1 + >>> square[2:8,2:8] = 1 >>> square array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], @@ -93,6 +93,9 @@ def harris(image, min_distance=10, threshold=0.1, eps=1e-6, [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) >>> harris(square, min_distance=1) + + Corners of the square + array([[3, 3], [3, 6], [6, 3], diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index 5d25935a..c55f07a7 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -23,68 +23,35 @@ def peak_local_max(image, min_distance=10, threshold=0.1): ------- coordinates : (N, 2) array (row, column) coordinates of peaks. - + + Notes + ----- + The peak local maximum function returns the coordinates of local peaks (maxima) + in a image. A maximum filter is used for finding local maxima. This operation + dilates the original image. After comparison between dilated and original image, + peak_local_max function returns the coordinates of peaks where + dilated image = original. + Examples -------- - >>> square = np.zeros([10,10]) - >>> square[2:8,2:8]=1 - >>> square[3:7,3:7]=0 - >>> square - array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], - [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], - [ 0., 0., 1., 1., 1., 1., 1., 1., 0., 0.], - [ 0., 0., 1., 0., 0., 0., 0., 1., 0., 0.], - [ 0., 0., 1., 0., 0., 0., 0., 1., 0., 0.], - [ 0., 0., 1., 0., 0., 0., 0., 1., 0., 0.], - [ 0., 0., 1., 0., 0., 0., 0., 1., 0., 0.], - [ 0., 0., 1., 1., 1., 1., 1., 1., 0., 0.], - [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], - [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) - - >>> image_max = ndimage.maximum_filter(square, size=3, - mode='constant') - - image_max is computed in peak_local_max function to enable - the calculation of coordinates of peaks. It is the dilation of - square - - >>> image_max - array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], - [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], - [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], - [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], - [ 0., 1., 1., 1., 0., 0., 1., 1., 1., 0.], - [ 0., 1., 1., 1., 0., 0., 1., 1., 1., 0.], - [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], - [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], - [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 0.], - [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) - - After comparison between image_max and square, peak_local_max - function returns the coordinates of peaks where - square = image_max - - >>> peak_local_max(square, min_distance=1) - array([[2, 2], - [2, 3], - [2, 4], - [2, 5], - [2, 6], - [2, 7], - [3, 2], - [3, 7], - [4, 2], - [4, 7], - [5, 2], - [5, 7], - [6, 2], - [6, 7], - [7, 2], - [7, 3], - [7, 4], - [7, 5], - [7, 6], - [7, 7]]) + >>> im = np.zeros((7, 7)) + >>> im[3, 4] = 1 + >>> im[3, 2] = 1.5 + >>> im + array([[ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], + [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], + [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], + [ 0. , 0. , 1.5, 0. , 1. , 0. , 0. ], + [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], + [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], + [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ]]) + + >>> peak_local_max(im, min_distance=1) + array([[3, 2], + [3, 4]]) + + >>> peak_local_max(im, min_distance=2) + array([[3, 2]]) """ image = image.copy()