Sobel edge detection.
In [1]:
import numpy as np
def sobel(f):
from pconv import pconv
Sx = np.array([[1.,2.,1.],
[0.,0.,0.],
[-1.,-2.,-1.]])
Sy = np.array([[1.,0.,-1.],
[2.,0.,-2.],
[1.,0.,-1.]])
fx = pconv(f, Sx)
fy = pconv(f, Sy)
mag = np.abs(fx + fy*1j)
theta = np.arctan2(fy,fx)
return mag,theta
Computes the edge detection by Sobel. Compute magnitude and angle.
In [1]:
testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python sobel.ipynb
import numpy as np
import sys,os
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
import matplotlib.image as mpimg
In [2]:
if testing:
f = np.array([[0,1,0,0],
[0,0,0,0],
[0,0,0,0]],dtype='uint8')
m,t = ia.sobel(f)
print('m:\n',m)
print('t:\n',t)
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()
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')
nb.nbshow(ia.normalize(m), title='Magnitude of Sobel filtering')
nb.nbshow(ia.normalize(dt), title='Angle of edges with magnitude above 2')
nb.nbshow()
In [ ]: