Reorganize example so that all plotting code is at the end

This commit is contained in:
Tony S Yu
2012-05-08 21:28:50 -04:00
parent e6098e140b
commit 01d66fc501
+20 -22
View File
@@ -8,8 +8,8 @@ object in an image. The ``match_template`` function uses normalised correlation
techniques to find instances of the "target image" in the "test image".
The output of ``match_template`` is an image where we can easily identify peaks
by eye. Nevertheless, this example concludes with a simple peak extraction
algorithm to quantify the locations of matches (marked in red).
by eye. We mark the locations of matches (red dots), which are detected using
a simple peak extraction algorithm.
"""
import numpy as np
@@ -20,15 +20,6 @@ import matplotlib.pyplot as plt
# We first construct a simple image target:
size = 100
target = np.tri(size) + np.tri(size)[::-1]
#plt.gray()
plt.figure(figsize=(9, 3))
plt.subplot(1, 3, 1)
plt.imshow(target)
plt.title("Target image")
plt.axis('off')
# place target in an image at two positions, and add noise.
image = np.zeros((400, 400))
target_positions = [(50, 50), (200, 200)]
@@ -36,19 +27,9 @@ for x, y in target_positions:
image[x:x+size, y:y+size] = target
image += randn(400, 400)*2
plt.subplot(1, 3, 2)
plt.imshow(image)
plt.title("Test image")
plt.axis('off')
# Match the template.
result = match_template(image, target, method='norm-corr')
plt.subplot(1, 3, 3)
plt.imshow(result)
plt.title("Result from\n``match_template``")
plt.axis('off')
# peak extraction algorithm.
delta = 5
found_positions = []
@@ -64,8 +45,25 @@ for i in range(50):
result[y, x] = 0
if len(found_positions) == len(target_positions):
break
x_found, y_found = np.transpose(found_positions)
plt.gray()
plt.subplot(1, 3, 1)
plt.imshow(target)
plt.title("Target image")
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(image)
plt.title("Test image")
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(result)
plt.plot(x_found, y_found, 'ro')
plt.title("Result from\n``match_template``")
plt.autoscale(tight=True)
plt.axis('off')
plt.show()