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 # In[7]: if testing: import matplotlib.image as mpimg f = mpimg.imread('../data/cameraman.tif') get_ipython().magic('time F1 = ia.dft(f)') get_ipython().magic('time F2 = np.fft.fft2(f)') print('Max difference is:', np.abs(F1 - F2).max()) # ## Equation
if testing: import numpy as np import sys, os ea979path = os.path.abspath('../../') if ea979path not in sys.path: sys.path.append(ea979path) import ea979.src as ia # ### Example 1 # In[2]: if testing: f = np.array([100., 500., 1000.]) g1 = ia.normalize(f, [0, 255]) print(g1) # In[3]: if testing: g2 = ia.normalize(f, [-1, 1]) print(g2) # In[4]: if testing: g3 = ia.normalize(f, [0, 1]) print(g3) # In[5]:
print('m:\n', m) print('t:\n', t) # ### Image examples # ### Example 1. # In[3]: if testing: f = mpimg.imread('../data/cameraman.tif') (g, a) = ia.sobel(f) nb = ia.nbshow(2) nb.nbshow(ia.normalize(g), title='Sobel') nb.nbshow(ia.normalize(np.log(g + 1)), title='Log of sobel') nb.nbshow() # ### Example 2. # In[4]: if testing: f = ia.circle([200, 300], 90, [100, 150]) m, t = ia.sobel(f) dt = np.select([m > 2], [t]) nb = ia.nbshow(3) nb.nbshow(f, title='Image f')
# In[2]: if testing: get_ipython().magic('matplotlib inline') import matplotlib.pyplot as plt import matplotlib.image as mpimg import sys, os ea979path = os.path.abspath('../../') if ea979path not in sys.path: sys.path.append(ea979path) import ea979.src as ia r, c = np.indices((256, 256)) f = ((r - 129)**2 + (c - 129)**2 < 10**2) * 255 ia.adshow(ia.normalize(f), 'Imagem original') F = ia.dct(f) ia.adshow(ia.normalize(np.log(abs(F) + 1)), 'DCT') # ### Example 3 # Compare with dft # In[3]: if testing: f = mpimg.imread('../data/cameraman.tif') nb = ia.nbshow(3) nb.nbshow(f, 'Imagem original')
def dftview(F): import ea979.src as ia FM = ia.dftshift(np.log(np.abs(F) + 1)) return ia.normalize(FM).astype(np.uint8)
if testing: F = ia.ramp([5,7], 3, [4,10]) print(F) F = ia.ramp((1,5,7),(0,3,0), [0,0,4,10,0,0]) print(F) F = ia.ramp([1,5,7],[3,0,0], [4,10,0,0,0,0]) print(F) # - **Image example** # In[4]: if testing: F = ia.ramp([1,200,300], [0,10,0], [0,0,0,255,0,128]) ia.adshow(ia.normalize(F.reshape(200,300))) # In[6]: if testing: F = ia.ramp([200,300], 10, [0,255]) ia.adshow(ia.normalize(F)) # In[7]: if testing: F = ia.ramp([1,200,300], [10,0,0], [0,255,0,0,0,0]) ia.adshow(ia.normalize(F.reshape(200,300)))
if testing: f = np.arange(24).reshape(4, 6) F = ia.dft(f) g = ia.idft(F) print(np.round(g.real)) # 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} $$
print("\nVisualiza propriedade A*A'= I:\n", B) # ### Example 2 # In[2]: if testing: get_ipython().magic('matplotlib inline') import matplotlib.pyplot as plt import matplotlib.image as mpimg import sys,os ea979path = os.path.abspath('../../') if ea979path not in sys.path: sys.path.append(ea979path) import ea979.src as ia A = ia.dctmatrix(128) ia.adshow(ia.normalize(A,[0,255]),'DCT 128x128') # In[ ]: # In[ ]:
a = ia.conv(f, h) print(a) # ### Example 4 # In[5]: if testing: get_ipython().magic('matplotlib inline') import matplotlib.pyplot as plt import matplotlib.image as mpimg f = mpimg.imread('../data/cameraman.tif') h = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) g = ia.conv(f, h) gn = ia.normalize(g, [0, 255]) ia.adshow(f, title='input') ia.adshow(gn, title='filtered') # ## Limitations # # Both image and kernel are internally converted to double. # ## Equation # # $$ \begin{matrix} # (f \ast h)(r,c) &=& \sum_{i=0}^{H-1} \sum_{j=0}^{W-1} f_{e}(i,j) h_{e}(r-i, c-j) \\ # f_{e}(r,c) &=& \left\{ \begin{array}{llcl} f(r,c), & 0 \leq r \leq H_{f}-1 & and & 0 \leq r \leq W_f-1 \\ # 0, & H_f \leq r \leq H-1 & or & W_f \leq c \leq W-1 \end{array}\right.\\ # h_{e}(r,c) &=& \left\{ \begin{array}{llcl} f(r,c), & 0 \leq r \leq H_{h}-1 & and & 0 \leq r \leq W_h-1 \\ # 0, & H_h \leq r \leq H-1 & or & W_h \leq c \leq W-1 \end{array}\right.\\
print('image dimensions = ', s) print('center of function = ', mu) print('spread factor =', sigma) print('Laplacian of Gaussian image : \n', F.round(2)) # #### Generating a image 2D 128x128, centered at 64x64 and sigma 4: # In[5]: if testing: s, mu, sigma = [128, 128], [64, 64], 4 F = ia.log(s, mu, sigma) print('image dimensions = ', s) print('center of function = ', mu) print('spread factor =', sigma) ia.adshow(ia.normalize(F), 'Laplacian of Gaussian') # #### Generating a image 2D 256x256, centered at 128x128 and sigma 20 # In[6]: if testing: s, mu, sigma = [256, 256], [128, 128], 20 F = ia.log(s, mu, sigma) print('image dimensions = ', s) print('center of function = ', mu) print('spread factor =', sigma) ia.adshow(ia.normalize(F), 'Laplacian of Gaussian') # ## Measuring time:
ea979path = os.path.abspath('../../') if ea979path not in sys.path: sys.path.append(ea979path) import ea979.src as ia get_ipython().magic('matplotlib inline') import matplotlib.pyplot as plt # ### Example 1 - Numeric 2-dimensional # In[2]: if testing: f = ia.gaussian((8, 4), np.transpose([[3, 1]]), [[1, 0], [0, 1]]) print('f=\n', np.array2string(f, precision=4, suppress_small=1)) g = ia.normalize(f, [0, 255]).astype(np.uint8) print('g=\n', g) # ## Example 2 - one dimensional signal # In[3]: # note that for 1-D case, the tuple has extra , # and the covariance matrix must be 2-D if testing: f = ia.gaussian((100, ), 50, [[10 * 10]]) g = ia.normalize(f, [0, 1]) plt.plot(g) plt.show() # ### Example 3 - two-dimensional image
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) ia.adshow(ia.normalize(mquadra), 'retangular filter') mfiltrada = F * mquadra
import matplotlib.image as mpimg get_ipython().magic('matplotlib inline') # ### Example 1 # In[3]: if testing: f = mpimg.imread('../data/cameraman.tif') F = ia.hadamard(f) g = ia.ihadamard(F) print(sum(sum(abs(f.astype(float) - g.astype(float))))) nb = ia.nbshow(3) nb.nbshow(f, 'Original') nb.nbshow(ia.normalize(F), 'hadamard of f') nb.nbshow(ia.normalize(g), 'ihadamard of F') nb.nbshow() # ## Measuring time: # In[4]: if testing: f = mpimg.imread('../data/cameraman.tif') F = ia.hadamard(f) print('Computational time is:') get_ipython().magic('timeit ia.ihadamard(F)') # In[ ]:
import ea979.src as ia # ### Example 1 # In[2]: if testing: F = ia.rectangle([7, 9], [3, 2], [3, 4]) print(F) # - **Example 2** # In[3]: if testing: F = ia.rectangle([200, 300], [90, 120], [70, 120]) ia.adshow(ia.normalize(F)) # ## Equation # # \begin{equation} # g(x,y)=\begin{cases} # 1, & \text{if } x_\text{min} \leq x < x_\text{max} \text{ and } y_\text{min} \leq y < y_\text{max}.\\ # 0, & \text{otherwise}. # \end{cases} # \end{equation} # ## Contributions # # Lucas de Vasconcellos Teixeira, 1st semester 2017
print("\n Image Output - (G): ") print(result) # ## Example with Image 2D # In[6]: if testing: f = mpimg.imread('../data/cameraman.tif') ia.adshow(f, title='a) - Original Image') h = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]) g = ia.pconv(f, h) print("\nPrewitt´s Mask") print(h) gn = ia.normalize(g, [0, 255]) ia.adshow(gn, title='b) Prewitt´s Mask filtering') ia.adshow(ia.normalize(abs(g)), title='c) absolute of Prewitt´s Mask filtering') # ## Equation # # $$ f(i) = f(i + kN), h(i)=h(i+kN)$$ # # $$ mod(i,N) = i - N \lfloor \frac{i}{N} \rfloor $$ # # $$ (f \ast_W h) (col) = \sum_{cc=0}^{W-1} f(mod(col-cc,W)) h(cc)$$ # # $$ (f \ast_{(H,W)} h) (row,col) = \sum_{rr=0}^{H-1} \sum_{cc=0}^{W-1} f(mod(row-rr,H), mod(col-cc,W)) h(rr,cc)$$ #
get_ipython().system(' jupyter nbconvert --to python cos.ipynb') import numpy as np import sys, os ea979path = os.path.abspath('../../') if ea979path not in sys.path: sys.path.append(ea979path) import ea979.src as ia # ### Example 1 # In[2]: if testing: f = ia.cos([128, 256], 100, np.pi / 3, 0) ia.adshow(ia.normalize(f, [0, 255])) # ## Equation # # $$ \begin{matrix} # f(r,c) & = & cos( 2\pi (\frac{1}{T_r}r + \frac{1}{T_c}c) + \phi) \\ # f(r,c) & = & cos( 2\pi (\frac{v}{H}r + \frac{u}{W}c) + \phi) \\ # T_r & = & \frac{T}{sin(\theta)} \\ # T_c & = & \frac{T}{cos(\theta)} \\ # u & = & \frac{W}{T_c} \\ # v & = & \frac{H}{T_r} # \end{matrix} $$ # # - $\theta$ is the direction of the cosine wave. # - $T$ is the wave period, in number of pixels. # - $T_r$ and $T_c$ are the period or wave length in the vertical and horizontal directions, respectively, in number of pixels.
s = ia.sat(f) print('f (input):\n', f) print('s (output):\n', s) a = ia.satarea(s, (0, 0), (3, 8)) print('area:', a) # ### Image example # # In[3]: if testing: f = mpimg.imread('../data/lenina.pgm')[::2, ::2] nb = ia.nbshow(2) nb.nbshow(f, 'Original Image') nb.nbshow(ia.normalize(ia.sat(f)), 'Integral Image') nb.nbshow() # ### Calculating a rectangle area with SAT (Summed Area Table) # In[4]: if testing: f = mpimg.imread('../data/lenina.pgm')[::2, ::2] H, W = f.shape s = ia.sat(f) a0 = ia.satarea(s, (0, 0), (H - 1, W - 1)) atopleft = ia.satarea(s, (0, 0), (H // 2 - 1, W // 2 - 1)) abotleft = ia.satarea(s, (H // 2, 0), (H - 1, W // 2 - 1)) atopright = ia.satarea(s, (0, W // 2), (H // 2 - 1, W - 1)) abotright = ia.satarea(s, (H // 2, W // 2), (H - 1, W - 1))
# ## Examples # In[1]: testing = (__name__ == "__main__") if testing: get_ipython().system(' jupyter nbconvert --to python isolines.ipynb') import numpy as np import sys, os ea979path = os.path.abspath('../../') if ea979path not in sys.path: sys.path.append(ea979path) import ea979.src as ia # ### Example 1 # In[2]: if testing: f = ia.normalize(ia.bwlp([150, 150], 4, 1), [0, 255]) f = f.astype('uint8') g = ia.isolines(f, 10, 3) g = g.astype('uint8') ia.adshow(f) ia.adshow(g) # In[ ]:
if testing: get_ipython().system(' jupyter nbconvert --to python hadamard.ipynb') import numpy as np import sys, os import matplotlib.image as mpimg ea979path = os.path.abspath('../../') if ea979path not in sys.path: sys.path.append(ea979path) import ea979.src as ia # In[3]: if testing: f = mpimg.imread('../data/cameraman.tif') F = ia.hadamard(f) nb = ia.nbshow(2) nb.nbshow(f) nb.nbshow(ia.normalize(np.log(abs(F) + 1))) nb.nbshow() # ## Measuring time: # In[ ]: if testing: f = mpimg.imread('../data/cameraman.tif') print('Computational time is:') get_ipython().magic('timeit ia.hadamard(f)') # In[ ]:
# ### Example 1 # In[3]: if testing: get_ipython().magic('matplotlib inline') import matplotlib.pyplot as plt import matplotlib.image as mpimg f = mpimg.imread('../data/cameraman.tif') g07 = ia.logfilter(f, 0.7) nb = ia.nbshow(3) nb.nbshow(f, 'Imagem original') nb.nbshow(ia.normalize(g07), 'LoG filter') nb.nbshow(g07 > 0, 'positive values') nb.nbshow() # ### Example 2 # In[4]: if testing: g5 = ia.logfilter(f, 5) g10 = ia.logfilter(f, 10) nb = ia.nbshow(2, 2) nb.nbshow(ia.normalize(g5), 'sigma=5') nb.nbshow(g5 > 0, 'positive, sigma=5') nb.nbshow(ia.normalize(g10), 'sigma=10')
if testing: f = mpimg.imread('../data/cameraman.tif') g = ia.gshow(f, f>230) nb = ia.nbshow(2) nb.nbshow(f,'Original image') nb.nbshow(g,'Pixels with values above 230 are shown in red') nb.nbshow() # In[4]: if testing: f = ia.gaussian((256,256), np.transpose([[128,128]]), [[50*50,0],[0,80*80]]) fn = ia.normalize(f, [0,255]) f1 = ia.gshow(fn, fn > 20, fn > 30, fn > 40, fn > 50, fn > 60, fn > 70) nb = ia.nbshow(2) nb.nbshow(fn,'Original image') nb.nbshow(f1,'Overlaid image') nb.nbshow() # ## Reference # - [Function iagshow](http://adessowiki.fee.unicamp.br/adesso/wiki/ia636/iagshow/view/) # ## Contributions
if testing: np.set_printoptions(suppress=True, precision=4) A = ia.haarmatrix(4) print('Visualiza matriz haar 4x4:\n',A) B = np.dot(A,np.transpose(A)) print("\nVisualiza propriedade A*A'= I:\n", B) # ### Example 2 # In[3]: if testing: A = ia.haarmatrix(128) ia.adshow(ia.normalize(A),'Haar matrix 128x128') # ### Example 3 # In[4]: if testing: f = mpimg.imread('../data/cameraman.tif') A = ia.haarmatrix(f.shape[0]) B = ia.haarmatrix(f.shape[1]) F = np.dot(np.dot(A, f), np.transpose(B)) nb = ia.nbshow(2) nb.nbshow(f,'Imagem original')
get_ipython().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))