Addition of examples in the harris and peak local max function

This commit is contained in:
Vincent Albufera
2012-03-21 14:27:12 +01:00
parent 42b5e91b7c
commit 8c9777b967
6 changed files with 161 additions and 3 deletions
+16 -3
View File
@@ -20,15 +20,28 @@ def plot_harris_points(image, filtered_coords):
""" plots corners found in image"""
plt.plot()
plt.imshow(image)
plt.imshow(image, cmap=plt.cm.gray)
plt.plot([p[1] for p in filtered_coords],
[p[0] for p in filtered_coords],
'b.')
'r.')
plt.axis('off')
plt.show()
# display results
plt.figure(figsize=(8, 6))
im = img_as_float(data.lena())
filtered_coords = harris(im, min_distance=6)
im2 = img_as_float(data.text())
filtered_coords = harris(im, min_distance=4)
plt.subplot(121)
plot_harris_points(im, filtered_coords)
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)
+55
View File
@@ -0,0 +1,55 @@
"""
===============================================================================
Peak local maximum
===============================================================================
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.
"""
from scipy import ndimage
import matplotlib.pyplot as plt
import numpy as np
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
# It is used within peak_local_max function
image_max = ndimage.maximum_filter(image, 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)
# 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)
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.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()
+5
View File
@@ -43,6 +43,11 @@ def lena():
"""
return load("lena.png")
def text():
""" Gray-level "text" image used for corner detection"""
return load("text.png")
def checkerboard():
"""Checkerboard image.
Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

+22
View File
@@ -76,7 +76,29 @@ 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)
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,
+63
View File
@@ -23,6 +23,69 @@ def peak_local_max(image, min_distance=10, threshold=0.1):
-------
coordinates : (N, 2) array
(row, column) coordinates of peaks.
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]])
"""
image = image.copy()
# Non maximum filter