In [24]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import signal
img = mpimg.imread('../figures/lena_greyscale.png')
arr = np.asarray(img)
template = np.copy(arr[240:290, 240:290])
#plt.figure(figsize=(15,10))
plt.subplot(1,2,1)
plt.title('Template - olhos de Lena')
plt.imshow(template, cmap='gray')
#plt.axis(off)
plt.figure(figsize=(15,10))
plt.subplot(1,2,2)
plt.title('Imagem alvo')
plt.imshow(arr, cmap='gray')
plt.show()
In [26]:
arr = arr - arr.mean()
template -= template.mean()
#arr = arr + np.random.randn(*arr.shape) * 100 # add noise
corr = signal.correlate2d(arr, template, boundary='fill', mode='same')
y, x = np.unravel_index(np.argmax(corr), corr.shape) # Encontrar a correspondencia: Converte um índice simples ou uma matriz de índices simples em uma tupla de matrizes de coordenadas
print(np.argmax(corr))
print(corr.shape)
print(y,x)
#print(template)
#print(corr)
#plt.imshow(template, cmap='gray')
In [27]:
fig, (ax_orig, ax_template, ax_corr, ax_result) = plt.subplots(4, 1, figsize=(6, 15))
ax_orig.imshow(arr, cmap='gray')
ax_orig.set_title('Original')
#ax_orig.set_axis_off()
ax_template.imshow(template, cmap='gray')
ax_template.set_title('Template')
ax_template.set_axis_off()
ax_corr.imshow(corr, cmap='gray')
ax_corr.set_title('Cross-correlation')
ax_corr.set_axis_off()
ax_result.imshow(img, cmap='gray')
ax_result.set_title('Resultado da correspondência')
ax_result.set_axis_off()
ax_result.plot(x, y, 'ro')
fig.show()
In [8]:
%matplotlib inline
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
h1,g2=np.histogram(arr,bins=255)
h2,g2=np.histogram(corr,bins=255)
plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
plt.plot(h1)
plt.subplot(1,2,2)
plt.plot(h2)
plt.show()
plt.axes(projection='3d')
Out[8]:
In [6]:
import timeit
t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"')
t.timeit()
Out[6]:
In [10]:
%timeit 1+1
In [ ]: