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]
FskiResz1 = np.fft.fft2(skimage_resize1) FskiResz2 = np.fft.fft2(skimage_resize2) FsciResz1 = np.fft.fft2(scipy_imresize1) FsciResz2 = np.fft.fft2(scipy_imresize2) FsciZoom1 = np.fft.fft2(scipy_zoom1) FsciZoom2 = np.fft.fft2(scipy_zoom2) # In[12]: # Plotando espectros nb = ia.nbshow(1) #nb.nbshow(f, 'Imagem original') nb.nbshow(ia.dftview(F), 'Espectro original') nb.nbshow() nb = ia.nbshow(2) #nb.nbshow(ia.normalize(skimage_rescale1),'skiResc x%s'%(scale1)) #nb.nbshow(ia.normalize(skimage_resize1),'skiResz x%s'%(scale1)) #nb.nbshow(ia.normalize(scipy_imresize1),'sciResz x%s'%(scale1)) #nb.nbshow(ia.normalize(scipy_zoom1),'sciZoom x%s'%(scale1)) nb.nbshow(ia.dftview(FskiResc1), 'skimage_rescale x%s' % (scale1)) nb.nbshow(ia.dftview(FskiResz1), 'skimage_resize x%s' % (scale1)) nb.nbshow(ia.dftview(FsciResz1), 'scipy_imresize x%s' % (scale1)) nb.nbshow(ia.dftview(FsciZoom1), 'scipy_Zoom x%s' % (scale1)) nb.nbshow()
sys.path.append(ia898path) import ia898.src as ia # ### Example 1 # In[2]: if testing: import matplotlib.image as mpimg import numpy.fft as FFT f = mpimg.imread('../data/cameraman.tif') ia.adshow(f, "Original 2D image - Cameraman") F = FFT.fft2(f) Fv = ia.dftview(F) ia.adshow(Fv, "Cameraman DFT optical spectrum") # ## Equation # # # $$ \begin{matrix} # Gaux &=& \log(|F_{xc,yc}| + 1)\\xc &=& \lfloor W/2 \rfloor \\yc &=& \lfloor H/2 \rfloor\\ G &=& Gaux|_0^{255} # \end{matrix} $$ # In[3]: if testing: print('testing dftview')
# In[4]: if False: #testing: import matplotlib.image as mpimg f = mpimg.imread('../data/cameraman.tif') F = ia.dft(f) print(F.shape) H = ia.circle(F.shape, 50, [F.shape[0] / 2, F.shape[1] / 2]) H = ia.normalize(H, [0, 1]) FH = F * ia.idftshift(H) print(ia.isdftsym(FH)) g = ia.idft(FH) ia.adshow(f) ia.adshow(ia.dftview(F)) ia.adshow(ia.normalize(H, [0, 255])) ia.adshow(ia.dftview(FH)) ia.adshow(ia.normalize(abs(g))) # ## Equation # # $$ \begin{matrix} # f(x) &=& \frac{1}{N}\sum_{u=0}^{N-1}F(u)\exp(j2\pi\frac{ux}{N}) \\ & & 0 \leq x < N, 0 \leq u < N \\ \mathbf{f} &=& \frac{1}{\sqrt{N}}(A_N)^* \mathbf{F} # \end{matrix} $$ # $$ \begin{matrix} # f(x,y) &=& \frac{1}{NM}\sum_{u=0}^{N-1}\sum_{v=0}^{M-1}F(u,v)\exp(j2\pi(\frac{ux}{N} + \frac{vy}{M})) \\ & & (0,0) \leq (x,y) < (N,M), (0,0) \leq (u,v) < (N,M) \\ # \mathbf{f} &=& \frac{1}{\sqrt{NM}} (A_N)^* \mathbf{F} (A_M)^* # \end{matrix} $$
import matplotlib.image as mpimg ia898path = os.path.abspath('../../') if ia898path not in sys.path: sys.path.append(ia898path) import ia898.src as ia # ### Example 1 # In[6]: if testing: f = ia.circle([120, 150], 6, [60, 75]) F = ia.dft(f) Fs = ia.dftshift(F) ia.adshow(ia.dftview(F)) ia.adshow(ia.dftview(Fs)) # In[7]: if testing: F = np.array([[10 + 6j, 20 + 5j, 30 + 4j], [40 + 3j, 50 + 2j, 60 + 1j]]) Fs = ia.dftshift(F) print('Fs=\n', Fs) # ## Equation # # $$ \begin{matrix} # HS &=& H_{xo,yo} \\xo &=& \lfloor W/2 \rfloor \\yo &=& \lfloor H/2 \rfloor # \end{matrix} $$
# ## Identificação do ângulo de rotação # ### Calcular a transformada polar da visualização da DFT # In[12]: F = np.fft.fft2(f) F1 = np.fft.fft2(f1) #nb = ia.nbshow(2) #nb.nbshow(ia.dftview(F),'F') #nb.nbshow(ia.dftview(F1),'F1') #nb.nbshow() F = ia.dftview(F) F1 = ia.dftview(F1) #H,W = F.shape #U,V = (120,120) #F = F[H//2-U:H//2+U,W//2-V:W//2+V] #F1 = F1[H//2-U:H//2+U,W//2-V:W//2+V] nb = ia.nbshow(2) nb.nbshow(F,'F') nb.nbshow(F1,'F1') nb.nbshow() Fp = ia.polar(F,F.shape) F1p = ia.polar(F1,F1.shape)
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) ia.adshow(ia.normalize(mquadra),'retangular filter') mfiltrada = F * mquadra
g = imrsz.imresize(f, scale) g2 = scipy.misc.imresize(f,scale) print('f shape:', f.shape) print('imresize shape:', g.shape) print('scipy.imresize:', g2.shape) nb = ia.nbshow(3) nb.nbshow(f, 'Imagem original') nb.nbshow(g, 'imresize' ) nb.nbshow(g2, 'scipy.imresize') nb.nbshow() nb = ia.nbshow(4) nb.nbshow(ia.dftview(np.fft.fft2(f)) , 'Espectro original') nb.nbshow(ia.dftview(np.fft.fft2(g)), 'imresize') nb.nbshow(ia.dftview(np.fft.fft2(g2)), 'scipy.imresize') nb.nbshow() # In[ ]: if testing: # Testing integer scale = 50 g = imrsz.imresize(f, scale) g2 = scipy.misc.imresize(f,scale)
F = ia.dft(f) # proposed dft F1 = np.fft.fftn(f) # numpy dft print('ia.dft:', '\n', F.round(2), '\n') print('fft.fftn:', '\n', F1.round(2), '\n') print('Equal Results? (max error)', abs(F1 - F).max()) # ### Image example: 2d circle # In[5]: if testing: f = ia.circle([256, 256], 10, [129, 129]) ia.adshow(f) F = ia.dft(f) Fv = ia.dftview(F) ia.adshow(Fv) # ### Image example: 3d square # In[6]: if False: #testing: f = np.zeros((25, 30, 40)) f[10:15, 20:26, 21:27] = 1 F = ia.dft(f) ia.adshow(ia.normalize(ia.mosaic(f, 5)), 'Original Image') ia.adshow(ia.mosaic(ia.dftview(F), 5), 'Fourier Transformation') # ### Comparison with other implementations
import sys,os ia898path = os.path.abspath('../../') if ia898path not in sys.path: sys.path.append(ia898path) import ia898.src as ia # ### Example 1 # In[2]: if testing: H2_10 = ia.bwlp([100,100],2,2) # cutoff period: 2 pixels, order: 10 ia.adshow(ia.dftview(H2_10)) # In[3]: if testing: H4_1 = ia.bwlp([100,100],4,1) # cutoff period: 4, order: 1 ia.adshow(ia.dftview(H4_1)) # In[4]: if testing: H8_100 = ia.bwlp([100,100],8,100) # cutoff period: 8, order: 100
# Será necessário a função `comb` para poder fazer esta demonstração. # # Será necessário também ver outras propriedades da convolução e do teorema da convolução para poder fazer esta demonstração. Recomendo ler livros ou textos sobre esta explicação da interpretação no domínio da frequência da redução via interpolação. # In[15]: c = ia.comb((f.shape), (4, 4), (0, 0)) ia.adshow(ia.normalize(c)) # In[16]: C = np.fft.fft2(c) F = np.fft.fft2(f) G2 = ia.pconv(F, C) nb = ia.nbshow(3) nb.nbshow(ia.dftview(F), 'Espectro da imagem original') nb.nbshow(ia.dftview(C), 'Espectro do trêm de impulsos') nb.nbshow(ia.dftview(G2), 'Espectro resultante da convolução') nb.nbshow() # In[17]: g2 = np.fft.ifft2(G2) gReal = ia.normalize(g2.real) gMinified = gReal[::4, ::4] nb.nbshow(gReal, 'Imagem resultante') nb.nbshow(gMinified, 'Imagem reduzida') nb.nbshow() # In[18]: