In [1]:
def pconv(f,h):
import numpy as np
h_ind=np.nonzero(h)
f_ind=np.nonzero(f)
if len(h_ind[0])>len(f_ind[0]):
h, f = f, h
h_ind,f_ind= f_ind,h_ind
gs = np.maximum(np.array(f.shape),np.array(h.shape))
if (f.dtype == 'complex') or (h.dtype == 'complex'):
g = np.zeros(gs,dtype='complex')
else:
g = np.zeros(gs)
f1 = g.copy()
f1[f_ind]=f[f_ind]
if f.ndim == 1:
(W,) = gs
col = np.arange(W)
for cc in h_ind[0]:
g[:] += f1[(col-cc)%W] * h[cc]
elif f.ndim == 2:
H,W = gs
row,col = np.indices(gs)
for rr,cc in np.transpose(h_ind):
g[:] += f1[(row-rr)%H, (col-cc)%W] * h[rr,cc]
else:
Z,H,W = gs
d,row,col = np.indices(gs)
for dd,rr,cc in np.transpose(h_ind):
g[:] += f1[(d-dd)%Z, (row-rr)%H, (col-cc)%W] * h[dd,rr,cc]
return g
In [1]:
testing = (__name__ == '__main__')
if testing:
! jupyter nbconvert --to python pconv.ipynb
import numpy as np
%matplotlib inline
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import sys,os
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
In [2]:
if testing:
f = np.array([0,0,0,1,0,0,0,0,1])
print("f:",f)
h = np.array([1,2,3])
print("h:",h)
g1 = ia.pconv(f,h)
g2 = ia.pconv(h,f)
print("g1:",g1)
print("g2:",g2)
In [4]:
if testing:
f = np.array([[1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0]])
print("Image (f):")
print(f)
h = np.array([[1,2,3],
[4,5,6]])
print("\n Image Kernel (h):")
print(h)
g1 = ia.pconv(f,h)
print("Image Output (g1=f*h):")
print(g1)
g2 = ia.pconv(h,f)
print("Image Output (g2=h*f):")
print(g)
In [5]:
if testing:
f = np.zeros((3,3,3))
#f[0,1,1] = 1
f[1,1,1] = 1
#f[2,1,1] = 1
print("\n Image Original (F): ")
print(f)
h = np.array([[[ 1, 2, 3 ],
[ 3, 4, 5 ],
[ 5, 6, 7 ]],
[[ 8, 9, 10],
[11, 12, 13],
[14, 15, 16]],
[[17, 18, 19],
[20, 21, 22],
[23, 24, 25]]])
print("\n Image Kernel (H): ")
print(h)
result = ia.pconv(f,h)
print("\n Image Output - (G): ")
print(result)
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')