From 60c8e95905d11e1b2b1c852fb61d662826958f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 9 Dec 2012 18:14:36 +0100 Subject: [PATCH] Add function to detect corner peaks as a wrapper to peak_local_max --- doc/examples/plot_corner.py | 4 +- skimage/feature/__init__.py | 2 +- skimage/feature/corner.py | 87 ++++++++++++++++++++++++++++++------- 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/doc/examples/plot_corner.py b/doc/examples/plot_corner.py index 7c9caa34..4d01d177 100644 --- a/doc/examples/plot_corner.py +++ b/doc/examples/plot_corner.py @@ -15,7 +15,7 @@ import numpy as np from matplotlib import pyplot as plt from skimage import data -from skimage.feature import corner_harris, corner_subpix, peak_local_max +from skimage.feature import corner_harris, corner_subpix, corner_peaks from skimage.transform import warp, AffineTransform from skimage.draw import ellipse @@ -27,7 +27,7 @@ image[rr, cc] = 1 image[180:230, 10:60] = 1 image[230:280, 60:110] = 1 -coords = peak_local_max(corner_harris(image), min_distance=5) +coords = corner_peaks(corner_harris(image), min_distance=5) coords_subpix = corner_subpix(image, coords, window_size=13) plt.gray() diff --git a/skimage/feature/__init__.py b/skimage/feature/__init__.py index ec81849f..e0756bfe 100644 --- a/skimage/feature/__init__.py +++ b/skimage/feature/__init__.py @@ -2,6 +2,6 @@ from ._hog import hog from .texture import greycomatrix, greycoprops, local_binary_pattern from .peak import peak_local_max from .corner import (corner_kitchen_rosenfeld, corner_harris, corner_shi_tomasi, - corner_foerstner, corner_subpix) + corner_foerstner, corner_subpix, corner_peaks) from .corner_cy import corner_moravec from .template import match_template diff --git a/skimage/feature/corner.py b/skimage/feature/corner.py index e48a0b41..cbdbf881 100644 --- a/skimage/feature/corner.py +++ b/skimage/feature/corner.py @@ -3,7 +3,7 @@ from scipy import ndimage from scipy import stats from skimage.color import rgb2grey from skimage.util import img_as_float -from . import peak +from skimage.feature import peak_local_max def _compute_derivatives(image): @@ -141,7 +141,7 @@ def corner_harris(image, method='k', k=0.05, eps=1e-6, sigma=1): Examples ------- - >>> from skimage.feature import corner_harris, peak_local_max + >>> from skimage.feature import corner_harris, corner_peaks >>> square = np.zeros([10, 10]) >>> square[2:8, 2:8] = 1 >>> square @@ -155,11 +155,11 @@ def corner_harris(image, method='k', k=0.05, eps=1e-6, sigma=1): [ 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]]) - >>> peak_local_max(corner_harris(square), min_distance=1) - array([[3, 3], - [3, 6], - [6, 3], - [6, 6]]) + >>> corner_peaks(corner_harris(square), min_distance=1) + array([[2, 2], + [2, 7], + [7, 2], + [7, 7]]) """ @@ -211,7 +211,7 @@ def corner_shi_tomasi(image, sigma=1): Examples ------- - >>> from skimage.feature import corner_shi_tomasi, peak_local_max + >>> from skimage.feature import corner_shi_tomasi, corner_peaks >>> square = np.zeros([10, 10]) >>> square[2:8, 2:8] = 1 >>> square @@ -225,11 +225,11 @@ def corner_shi_tomasi(image, sigma=1): [ 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]]) - >>> peak_local_max(corner_shi_tomasi(square), min_distance=1) - array([[3, 3], - [3, 6], - [6, 3], - [6, 6]]) + >>> corner_peaks(corner_shi_tomasi(square), min_distance=1) + array([[2, 2], + [2, 7], + [7, 2], + [7, 7]]) """ @@ -278,7 +278,7 @@ def corner_foerstner(image, sigma=1): Examples ------- - >>> from skimage.feature import corner_foerstner, peak_local_max + >>> from skimage.feature import corner_foerstner, corner_peaks >>> square = np.zeros([10, 10]) >>> square[2:8, 2:8] = 1 >>> square @@ -296,7 +296,7 @@ def corner_foerstner(image, sigma=1): >>> accuracy_thresh = 0.5 >>> roundness_thresh = 0.3 >>> foerstner = (q > roundness_thresh) * (w > accuracy_thresh) * w - >>> peak_local_max(foerstner, min_distance=1) + >>> corner_peaks(foerstner, min_distance=1) array([[2, 2], [2, 7], [7, 2], @@ -441,3 +441,60 @@ def corner_subpix(image, corners, window_size=11, alpha=0.99): corners_subpix[i, :] = y0 + est_edge[0], x0 + est_edge[1] return corners_subpix + + +def corner_peaks(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, + exclude_border=True, indices=True, num_peaks=np.inf, + footprint=None, labels=None): + """Find corners in corner measure response image. + + This differs from `skimage.feature.peak_local_max` in that it suppresses + multiple connected peaks with the same accumulator value. + + Parameters + ---------- + See `skimage.feature.peak_local_max`. + + Returns + ------- + See `skimage.feature.peak_local_max`. + + Examples + -------- + >>> from skimage.feature import peak_local_max, corner_peaks + >>> response = np.zeros((5, 5)) + >>> response[2:4, 2:4] = 1 + >>> response + array([[ 0., 0., 0., 0., 0.], + [ 0., 0., 0., 0., 0.], + [ 0., 0., 1., 1., 0.], + [ 0., 0., 1., 1., 0.], + [ 0., 0., 0., 0., 0.]]) + >>> peak_local_max(response, exclude_border=False) + array([[2, 2], + [2, 3], + [3, 2], + [3, 3]]) + >>> corner_peaks(response, exclude_border=False) + array([[2, 2]]) + + """ + + peaks = peak_local_max(image, min_distance=min_distance, + threshold_abs=threshold_abs, + threshold_rel=threshold_rel, + exclude_border=exclude_border, + indices=False, num_peaks=np.inf, + footprint=footprint, labels=labels) + if min_distance > 0: + coords = np.transpose(peaks.nonzero()) + for r, c in coords: + if peaks[r, c]: + peaks[r - min_distance:r + min_distance + 1, + c - min_distance:c + min_distance + 1] = False + peaks[r, c] = True + + if indices is True: + return np.transpose(peaks.nonzero()) + else: + return peaks