def filtroidealdemo(f,U,V): ''' Inputs: f - input image U - vertical radius V - horizontal radius Output: fh - filtered image ''' F = np.fft.fft2(f) H,W = F.shape Fh = np.zeros_like(F) r,c = np.indices((H,W)) Fh[( ((r-H//2)/U)**2 + ((c-W//2)/V)**2)<1]=1 Fh = ptr.ptransfat(Fh,(H//2,W//2)) fh = np.fft.ifft2(F * Fh) plt.figure(1,figsize=(12,12)) plt.subplot(221) plt.imshow(f,cmap='gray') plt.title("f - imagem original") plt.subplot(222) plt.imshow(ptr.ptransfat(np.log(1+ np.abs(F)),(H//2,W//2)),cmap='gray') plt.title("F - espectro da imagem") plt.subplot(223) plt.imshow(ptr.ptransfat(np.log(1+ np.abs(F*Fh)),(H//2,W//2)),cmap='gray') plt.title("F*Fh - espectro filtrado") plt.subplot(224) plt.imshow(fh.real,cmap='gray') plt.title("fh - imagem filtrada") return fh
def dftrotationproperty(f, scale, angle): ''' Inputs: f - image scale - image scale. It should be less than 1. angle - angle in rads. ''' r, c = f.shape T = np.eye(3) T = T * 0.5 T[0, 2] = r // 8 T[1, 2] = c // 8 T1 = np.array([[1, 0, r // 2], [0, 1, c // 2], [0, 0, 1]]) T2 = np.array([[np.cos(angle), -np.sin(angle), 0], [np.sin(angle), np.cos(angle), 0], [0, 0, 1]]) T3 = np.array([[1, 0, -r // 2], [0, 1, -c // 2], [0, 0, 1]]) Tr = T1.dot(T2.dot(T3)) g = aff2.affine2(f, T) gr = aff2.affine2(g, Tr) F = np.fft.fft2(f) G = np.fft.fft2(g) GR = np.fft.fft2(gr) fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(12, 5)) im = axes[0, 0].imshow(f, cmap="gray") im = axes[0, 1].imshow(g, cmap="gray") im = axes[0, 2].imshow(gr, cmap="gray") im = axes[1, 0].imshow(np.log(np.abs(ptr.ptransfat(F, (r // 2, c // 2))) + 1), cmap='gray') im = axes[1, 1].imshow(np.log(np.abs(ptr.ptransfat(G, (r // 2, c // 2))) + 1), cmap='gray') im = axes[1, 2].imshow( np.log(np.abs(ptr.ptransfat(GR, (r // 2, c // 2))) + 1), cmap='gray') fig.colorbar(im, ax=axes.ravel().tolist())
def dftscaleproperty(froi,scale): Froi = np.fft.fft2(froi) # F is the DFT of f r,c = froi.shape froix = np.zeros(scale*np.array(froi.shape)) # size is 4 times larger froix[::scale,::scale] = froi Froix = np.fft.fft2(froix) # Fx4 is the DFT of fx4 (expanded f) r2,c2 = froix.shape plt.figure(1,figsize=(2,2)) plt.subplot(121) plt.imshow(froi, cmap="gray") plt.title('froi') plt.subplot(122) plt.imshow(np.log(np.abs(ptr.ptransfat(Froi,(r//2,c//2)))+1),cmap='gray') plt.title('Froi') plt.figure(2,figsize=(2*scale,2*scale)) plt.subplot(121) plt.imshow(froix, cmap="gray") plt.title('froix{}'.format(scale)) plt.subplot(122) plt.imshow(np.log(np.abs(ptr.ptransfat(Froix,(r2//2,c2//2)))+1),cmap='gray') plt.title('Froix{}'.format(scale))
def isccsymAlt(F): H, W = F.shape U = (H // 2) V = (W // 2) fq = F[:U + 1, :V + 1] Ft = ptr.ptransfat(F, (-1, -1)) if (H % 2 == 0): Ux = U - 1 else: Ux = U if (W % 2 == 0): Vy = V - 1 else: Vy = V tq = np.conjugate(np.flipud(np.fliplr(Ft[Ux:, Vy:]))) sq = F[1:U + 1, -V:] qq = np.conjugate(np.flipud(np.fliplr(F[-U:, 1:V + 1]))) A1 = ((abs(fq - tq)) < 10E-4).all() A2 = ((abs(sq - qq)) < 10E-4).all() return A1 & A2
# ## Examples # # ### Example 1 # # #### Caso numérico # In[15]: if testing: f = np.arange(25).reshape(5, 5) t1 = (2, 0) t2 = (0, 100003) t3 = (-577, 333) g1 = ptr.ptransfat(f, t1) g2 = ptr.ptransfat(f, t2) g3 = ptr.ptransfat(f, t2) print("Imagem original:\n", f) print("\nTranlação de (2,0):\n", g1) print("\nTranslação de (0,100003):\n", g2) print("\nTranslação de (-577,333):\n", g3) # ### Example 2 # # #### testes com imagens
if testing: #Create image f = np.zeros((256, 256)) H, W = f.shape a, b = (64, 16) f[H // 2 - a:H // 2 + a, W // 2 - b:W // 2 + b] = 255 f[H // 2 - b:H // 2 + b, W // 2 - a:W // 2 + a] = 255 F = np.fft.fft2(f) fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 5)) axes[0].set_title('Imagem original') im = axes[0].imshow(f, cmap="gray") axes[1].set_title('Transformada de Fourier') im = axes[1].imshow(np.log(np.abs(ptr.ptransfat(F, (H // 2, W // 2))) + 1), cmap='gray') fig.colorbar(im, ax=axes.ravel().tolist()) # In[7]: if testing: #rotate image H, W = f.shape angle = np.pi / 4 T1 = np.array([[1, 0, H // 2], [0, 1, W // 2], [0, 0, 1]]) T2 = np.array([[np.cos(angle), -np.sin(angle), 0], [np.sin(angle), np.cos(angle), 0], [0, 0, 1]]) T3 = np.array([[1, 0, -H // 2], [0, 1, -W // 2], [0, 0, 1]])
fig.colorbar(im, ax=axes.ravel().tolist()) # The DFT of the ROI image is taken and its spectrum is displayed # In[6]: if testing: F = np.fft.fft2(froi) # F is the DFT of f r,c = froi.shape fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) axes[0].set_title('froi') axes[1].set_title('spectrum') im = axes[0].imshow(froi, cmap="gray") im = axes[1].imshow(np.log(np.abs(ptr.ptransfat(F,(r//2,c//2)))+1),cmap='gray') fig.colorbar(im, ax=axes.ravel().tolist()) # The image is expanded by 4, but filling the new pixels with 0 # In[7]: if testing: fx4 = np.zeros(4*np.array(froi.shape)) # size is 4 times larger fx4[::4,::4] = froi # filling the expanded image fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) axes[0].set_title('froi') axes[1].set_title('fx4') im = axes[0].imshow(froi, cmap="gray")
sys.path.append(path) import ptrans as ptr # In[36]: Hf = np.zeros_like(F) Hfx = np.zeros_like(F) H, W = F.shape Hf[:H // (4), :W // (4)] = 1 #Ucut = H//(2*4) #frequência de corte em H #Vcut = W//(2*4) #frequência de corte em W #r,c = np.indices((H,W)) #Hfx[( ((r-H//2)/Ucut)**2 + ((c-W//2)/Vcut)**2)<1]=1 #Hfx = ptr.ptransfat(Hfx,(-H//(2),-W//(2))) Hf = ptr.ptransfat(Hf, (-H // (2 * 4), -W // (2 * 4))) Gfh = (F * Hf) gfh = np.fft.ifft2(Gfh) gfh = ia.normalize(gfh.real) gfh4 = gfh[::4, ::4] Gfh4 = np.fft.fft2(gfh4) nb = ia.nbshow(3) nb.nbshow(ia.dftview(F), 'Espectro original') nb.nbshow(ia.dftview(Hf), 'Filtro passa-baixo') nb.nbshow(ia.dftview(Gfh), 'Espectro Filtrado') nb.nbshow(gfh4, 'imagem resultante') nb.nbshow(ia.dftview(Gfh4), 'imagem resultante') nb.nbshow()