def isccsym(F):
    import ia898.src as ia
    
    if len(F.shape) == 1: F = F[np.newaxis,np.newaxis,:] 
    if len(F.shape) == 2: F = F[np.newaxis,:,:] 

    n,m,p = F.shape
    
    return(abs(F-np.conjugate(ia.ptrans(F[::-1,::-1,::-1],(1,1,1))))<10E-4).all()
示例#2
0
def conv_media2(f, window):
    H, W = window
    f1 = np.empty(((f.shape[0] + H - 1), (f.shape[1] + W - 1)))
    f1[(H - 1):, (W - 1):] = f
    if H > 1:
        f1[:(H - 1), (W - 1):] = f[(f.shape[0] - (H - 1)):, :]
    if W > 1:
        f1[(H - 1):, :(W - 1)] = f[:, (f.shape[1] - (W - 1)):]
    if (H > 1) & (W > 1):
        f1[:(H - 1), :(W - 1)] = f[(f.shape[0] - (H - 1)):,
                                   (f.shape[1] - (W - 1)):]
    sat = f1.cumsum(1).cumsum(0)
    A = np.zeros_like(sat)
    B = np.zeros_like(sat)
    C = np.zeros_like(sat)
    D = np.zeros_like(sat)
    A = sat
    B[:, W:] = ia.ptrans(sat, (0, W))[:, W:]
    C[H:, :] = ia.ptrans(sat, (H, 0))[H:, :]
    D[H:, W:] = ia.ptrans(sat, (H, W))[H:, W:]
    g = A + D - C - B
    g = g[(H - 1):, (W - 1):]
    return g
def fp_match(f, f1):
    # DFT das imagens
    F = np.fft.fft2(f)
    F1 = np.fft.fft2(f1)
    
    # Idetificação do ângulo de rotação
    F = ia.dftview(F)
    F1 = ia.dftview(F1)
    
    Fp = ia.polar(F,F.shape)
    F1p = ia.polar(F1,F1.shape)
    
    Pc = ia.phasecorr(Fp,F1p)
    peak = np.unravel_index(np.argmax(Pc), Pc.shape)
    ang = (float(peak[1])/Pc.shape[1])*360
    
    #Correção de rotação
    t1 = np.array([
            [1,0,-f1.shape[0]/2.],
            [0,1,-f1.shape[1]/2.],
            [0,0,1]]);

    t2 = np.array([
                [1,0,f1.shape[0]/2.],
                [0,1,f1.shape[1]/2.],
                [0,0,1]]);

    theta = np.radians(ang)
    r1 = np.array([
            [np.cos(theta),-np.sin(theta),0],
            [np.sin(theta),np.cos(theta),0],
            [0,0,1]]);

    T = t2.dot(r1).dot(t1)
    f1c = ia.affine(f1,T,0)
    
    #Máxima correlação eentre f e f1
    g = ia.phasecorr(f,f1c)
    idx = np.argmax(g)
    row,col = np.unravel_index(idx,g.shape)
    t = np.array(f.shape) - np.array((row,col))
    f1rt = ia.ptrans(f1c,-t)
    
    return [g[row,col],f1rt]  
# ## Correlação de fase para estimar o deslocamento e a correlação entre as imagens

# In[15]:

g = ia.phasecorr(f,f1c)

idx = np.argmax(g)
row,col = np.unravel_index(idx,g.shape)
vx = g[row,col]

t = np.array(f.shape) - np.array((row,col))
print('deslocamento:',np.array(f.shape) - np.array((row,col)))
print('correlação:',vx)

f1rt = ia.ptrans(f1c,-t)

nb = ia.nbshow(3)
nb.nbshow(f)
nb.nbshow(f1c)
nb.nbshow(f1rt)
nb.nbshow()


# ## Corrigir a translação e sobrepor as duas imagens para verificar se a implementação está funcionando
# 
# Existem várias formas de sobrepor as duas imagens para uma melhor visualização:
# - ponderar cada uma
# - combinação no canal de cores, Red, Green, por exemplo
# - combinação geométria (linhas pares e impares ou na forma de xadrez)
# - ou exclusivo abs(f-f1)
def dftshift(f):
    import ia898.src as ia
    return ia.ptrans(f, np.array(f.shape) // 2)
    print('Is this function symmetric?')
    print(ia.isccsym(np.fft.fft2(np.random.rand(100,100)))) # dimension variation
    print(ia.isccsym(np.fft.fft2(np.random.rand(101,100))))
    print(ia.isccsym(np.fft.fft2(np.random.rand(101,101))))


# ### Image Example: circular filter

# In[14]:


if testing:
    img = mpimg.imread('../data/cameraman.tif')
    F = ia.dft(img)
    imgc = 1 * ia.circle(img.shape, 50, [img.shape[0]/2, img.shape[1]/2])  
    imgct = ia.ptrans(imgc, np.array(imgc.shape)//2)
    ia.adshow(ia.normalize(imgct),'circular filter')
    res = F * imgct
    ia.adshow(ia.dftview(res))
    print('Is this filter symmetric?', ia.isccsym(res))


# ### Image Example 2: retangular filter

# In[17]:


if False: # testing:
    mquadra = ia.rectangle(img.shape, [50,50], [img.shape[0]/2, img.shape[1]/2])
    ia.adshow(mquadra,'RETANGULO')
    mquadra = ia.ptrans(mquadra, array(mquadra.shape)/2)
    print('teste 1 d')
    get_ipython().run_line_magic('timeit', 'ptrans(l,3)')
    get_ipython().run_line_magic('timeit', 'ia.ptrans(l,3)')
    print('teste 2 d')
    get_ipython().run_line_magic('timeit', 'ptrans(x,[1,2])')
    get_ipython().run_line_magic('timeit', 'ia.ptrans(x,[1,2])')
    print('teste 3 d')
    get_ipython().run_line_magic('timeit', 'ptrans(x3d,[1,2,1])')
    get_ipython().run_line_magic('timeit', 'ia.ptrans(x3d,[1,2,1])')

# In[50]:

if testing:
    f = mpimg.imread('/home/lotufo/ia898/data/cameraman.tif')
    g1 = ptrans(f, np.array(f.shape) // 3)
    g2 = ia.ptrans(f, np.array(f.shape) // 3)
    nb = ia.nbshow(2)
    nb.nbshow(g1, title='ptrans')
    nb.nbshow(g2, title='ia.ptrans')
    nb.nbshow()
    print('teste')
    get_ipython().run_line_magic('timeit', 'ptrans(f,np.array(f.shape)//3)')
    get_ipython().run_line_magic('timeit', 'ia.ptrans(f,np.array(f.shape)//3)')

# In[34]:


def dftshift(f):
    return ptrans(f, np.array(f.shape) // 2)

示例#8
0
    get_ipython().run_line_magic('matplotlib', 'inline')
    import matplotlib.image as mpimg

# ### Example 1

# Show that the point of maximum correlation for two equal images is the origin.
#
#

# In[4]:

if testing:
    # 2D example
    f1 = mpimg.imread("../data/cameraman.tif")
    noise = np.random.rand(f1.shape[0], f1.shape[1])
    f2 = ia.normalize(ia.ptrans(f1, (-1, 50)) + 300 * noise)
    g1 = ia.phasecorr(f1, f2)
    i = np.argmax(g1)
    row, col = np.unravel_index(i, g1.shape)
    v = g1[row, col]
    print(np.array(f1.shape) - np.array((row, col)))

# In[ ]:

if testing:
    print('max at:(%d, %d)' % (row, col))

    ia.adshow(ia.normalize(f1), "input image")
    ia.adshow(ia.normalize(f2), "input image")
    ia.adshow(ia.normalize(g1),
              "Correlation peak at (%d,%d) with %d" % (row, col, v))
# ### Example 3
# Compare with dft

# In[10]:

if testing:
    f = mpimg.imread('../data/cameraman.tif')

    nb = ia.nbshow(3)
    nb.nbshow(f, 'Imagem original')
    F1 = np.fft.fft2(f)
    F2 = ia.dct(f)
    nb.nbshow(
        ia.normalize(
            np.log(
                np.abs(ia.ptrans(F1, (f.shape[0] // 2, f.shape[1] // 2)) +
                       1))), 'DFT')
    nb.nbshow(ia.normalize(np.log(abs(F2) + 1)), 'DCT')
    nb.nbshow()

    print('Tempo de execução DFT:')
    get_ipython().run_line_magic('timeit', 'F1 = np.fft.fft2(f)')
    print('\nTempo de execução DCT:')
    get_ipython().run_line_magic('timeit', 'F2 = ia.dct(f)')

# ### Example 4
# Compare with scipy function

# In[11]:

if testing:
    import ia898.src as ia


# ### Example 1
# 
# Numeric examples in 2D and 3D.

# In[3]:


if testing:
    # 2D example
    f = np.arange(15).reshape(3,5)

    print("Original 2D image:\n",f,"\n\n")
    print("Image translated by (0,0):\n",ia.ptrans(f, (0,0)).astype(int),"\n\n")
    print("Image translated by (0,1):\n",ia.ptrans(f, (0,1)).astype(int),"\n\n")
    print("Image translated by (-1,2):\n",ia.ptrans(f, (-1,2)).astype(int),"\n\n")


# In[4]:


if testing:
    # 3D example
    f1 = np.arange(60).reshape(3,4,5)

    print("Original 3D image:\n",f1,"\n\n")
    print("Image translated by (0,0,0):\n",ia.ptrans(f1, (0,0,0)).astype(int),"\n\n")
    print("Image translated by (0,1,0):\n",ia.ptrans(f1, (0,1,0)).astype(int),"\n\n")
    print("Image translated by (-1,3,2):\n",ia.ptrans(f1, (-1,3,2)).astype(int),"\n\n")