Add convenience function for plotting matches

This commit is contained in:
Johannes Schönberger
2013-11-30 12:40:26 +01:00
parent abd1295a8d
commit 8fbc81eaac
3 changed files with 103 additions and 41 deletions
+3 -1
View File
@@ -13,6 +13,7 @@ from .brief import BRIEF
from .censure import CenSurE
from .orb import ORB
from .match import match_descriptors
from .util import plot_matches
__all__ = ['daisy',
@@ -38,4 +39,5 @@ __all__ = ['daisy',
'BRIEF',
'CenSurE',
'ORB',
'match_descriptors']
'match_descriptors',
'plot_matches']
+87
View File
@@ -33,6 +33,93 @@ class DescriptorExtractor(object):
raise NotImplementedError()
def plot_matches(ax, image1, image2, keypoints1, keypoints2,
indices1, indices2, keypoints_color='k', matches_color=None,
only_matches=False):
"""Plot matched features.
Parameters
----------
ax : matplotlib.axes.Axes
Matches and image are drawn in this ax.
image1 : (N, M [, 3]) array
First grayscale or color image.
image2 : (N, M [, 3]) array
Second grayscale or color image.
keypoints : (K1, 2) array
First keypoint coordinates as ``(row, col)``.
keypoints : (K2, 2) array
Second keypoint coordinates as ``(row, col)``.
keypoints : (K1, 2) array
Keypoint coordinates as ``(row, col)``.
indices1 : (Q, ) array
Indices of corresponding matches for first set of keypoints.
indices2 : (Q, ) array
Indices of corresponding matches for second set of keypoints.
keypoints_color : matplotlib color
Color for keypoint locations.
matches_color : matplotlib color
Color for lines which connect keypoint matches. By default the
color is chosen randomly.
only_matches : bool
Whether to only plot matches and not plot the keypoint locations.
"""
image1 = img_as_float(image1)
image2 = img_as_float(image2)
new_shape1 = image1.shape
new_shape2 = image2.shape
if image1.shape[0] < image2.shape[0]:
new_shape1[0] = image2.shape[0]
elif image1.shape[0] > image2.shape[0]:
new_shape2[0] = image1.shape[0]
if image1.shape[1] < image2.shape[1]:
new_shape1[1] = image2.shape[1]
elif image1.shape[1] > image2.shape[1]:
new_shape2[1] = image1.shape[1]
if new_shape1 != image1.shape:
new_image1 = np.zeros(new_shape1, dtype=image1.dtype)
new_image1[:image1.shape[0], :image1.shape[1]] = image1
image1 = new_image1
if new_shape2 != image2.shape:
new_image2 = np.zeros(new_shape2, dtype=image2.dtype)
new_image2[:image2.shape[0], :image2.shape[1]] = image2
image2 = new_image2
image = np.concatenate([image1, image2], axis=1)
offset = image1.shape
if not only_matches:
ax.scatter(keypoints1[:, 1], keypoints1[:, 0],
facecolors='none', edgecolors=keypoints_color)
ax.scatter(keypoints2[:, 1] + offset[1], keypoints2[:, 0],
facecolors='none', edgecolors=keypoints_color)
ax.imshow(image)
ax.axis((0, 2 * offset[1], offset[0], 0))
for i in range(len(indices1)):
idx1 = indices1[i]
idx2 = indices2[i]
if matches_color is None:
color = np.random.rand(3, 1)
else:
color = matches_color
ax.plot((keypoints1[idx1, 1], keypoints2[idx2, 1] + offset[1]),
(keypoints1[idx1, 0], keypoints2[idx2, 0]),
'-', color=color)
def _prepare_grayscale_input_2D(image):
image = np.squeeze(image)
if image.ndim != 2: