diff --git a/doc/examples/plot_harris.py b/doc/examples/plot_harris.py index 0e0222cb..6212ac4a 100644 --- a/doc/examples/plot_harris.py +++ b/doc/examples/plot_harris.py @@ -9,7 +9,7 @@ detection in multiple directions. .. [1] http://en.wikipedia.org/wiki/Corner_detection .. [2] http://en.wikipedia.org/wiki/Interest_point_detection """ - +import numpy as np from matplotlib import pyplot as plt from skimage import data, img_as_float @@ -19,16 +19,24 @@ 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.') + y, x = np.transpose(filtered_coords) + plt.plot(x, y, 'b.') plt.axis('off') - plt.show() +# display results +plt.figure(figsize=(8, 3)) +im_lena = img_as_float(data.lena()) +im_text = img_as_float(data.text()) -im = img_as_float(data.lena()) -filtered_coords = harris(im, min_distance=6) -plot_harris_points(im, filtered_coords) +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() diff --git a/doc/examples/plot_peak_local_max.py b/doc/examples/plot_peak_local_max.py new file mode 100644 index 00000000..e8c78661 --- /dev/null +++ b/doc/examples/plot_peak_local_max.py @@ -0,0 +1,50 @@ +""" +=============================================================================== +Finding local maxima +=============================================================================== + +The ``peak_local_max`` function returns the coordinates of local peaks (maxima) +in an 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 +import matplotlib.pyplot as plt +from skimage.feature import peak_local_max +from skimage import data, img_as_float + +im = img_as_float(data.coins()) + +# 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(im, size=20, mode='constant') + +# 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)) +plt.subplot(131) +plt.imshow(im, cmap=plt.cm.gray) +plt.axis('off') +plt.title('Original') + +plt.subplot(132) +plt.imshow(image_max, cmap=plt.cm.gray) +plt.axis('off') +plt.title('Maximum filter') + +plt.subplot(133) +plt.imshow(im, cmap=plt.cm.gray) +plt.autoscale(False) +plt.plot([p[1] for p in coordinates], [p[0] for p in coordinates], 'r.') +plt.axis('off') +plt.title('Peak local max') + +plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, + bottom=0.02, left=0.02, right=0.98) + +plt.show() + diff --git a/skimage/data/__init__.py b/skimage/data/__init__.py index c5159092..cb3a5b71 100644 --- a/skimage/data/__init__.py +++ b/skimage/data/__init__.py @@ -44,6 +44,20 @@ def lena(): """ return load("lena.png") +def text(): + """ 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") + def checkerboard(): """Checkerboard image. diff --git a/skimage/data/text.png b/skimage/data/text.png new file mode 100644 index 00000000..7135075b Binary files /dev/null and b/skimage/data/text.png differ diff --git a/skimage/feature/harris.py b/skimage/feature/harris.py index 1a7e3b6d..6d0da9c0 100644 --- a/skimage/feature/harris.py +++ b/skimage/feature/harris.py @@ -76,7 +76,32 @@ def harris(image, min_distance=10, threshold=0.1, eps=1e-6, ------- coordinates : (N, 2) array (row, column) coordinates of interest points. + + Examples + ------- + >>> square = np.zeros([10,10]) + >>> 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.], + [ 0., 0., 1., 1., 1., 1., 1., 1., 0., 0.], + [ 0., 0., 1., 1., 1., 1., 1., 1., 0., 0.], + [ 0., 0., 1., 1., 1., 1., 1., 1., 0., 0.], + [ 0., 0., 1., 1., 1., 1., 1., 1., 0., 0.], + [ 0., 0., 1., 1., 1., 1., 1., 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.]]) + >>> harris(square, min_distance=1) + + Corners of the square + + array([[3, 3], + [3, 6], + [6, 3], + [6, 6]]) """ + harrisim = _compute_harris_response(image, eps=eps, gaussian_deviation=gaussian_deviation) coordinates = peak.peak_local_max(harrisim, min_distance=min_distance, diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index 280263af..5d3f375b 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -38,6 +38,36 @@ def peak_local_max(image, min_distance=10, threshold='deprecated', ------- 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 + -------- + >>> 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]]) + """ if np.all(image == image.flat[0]): return []