Function iasobel

Synopse

Sobel edge detection.

  • mag,theta = iasobel(f)
    • mag,theta: Image.
    • f: Image. input image
 1 def iasobel(f):
 2     import numpy as np
 3     from ia636 import iaimginfo, iapconv
 4 
 5     wx = np.array([[1.,2.,1.],
 6                    [0.,0.,0.],
 7                    [-1.,-2.,-1.]])
 8     wy = np.array([[1.,0.,-1.],
 9                    [2.,0.,-2.],
10                    [1.,0.,-1.]])
11     gx = iapconv(f, wx)
12     gy = iapconv(f, wy)
13     mag = np.abs(gx + gy*1j)
14     theta = np.arctan2(gy,gx)
15     return mag,theta

Description

Computes the edge detection by Sobel. Compute magnitude and angle.

Examples

Numerical Example

1 import ia636 as ia
2 
3 f = array([[0,0,0,0],
4            [0,1,0,0],
5            [0,0,0,0]],dtype=uint8)
6 m,t = ia.iasobel(f)
7 print 'm=',m
8 print 't=',t
m= [[ 1.41421356  2.          1.41421356  0.        ]
 [ 2.          0.          2.          0.        ]
 [ 1.41421356  2.          1.41421356  0.        ]]
t= [[ 0.78539816  0.         -0.78539816  0.        ]
 [ 1.57079633  0.         -1.57079633  0.        ]
 [ 2.35619449  3.14159265 -2.35619449  0.        ]]

Example 1

1 f = adreadgray('cameraman.pgm')
2 (g,a) = ia.iasobel(f)
3 adshow(ia.ianormalize(g),title='sobel')
4 adshow(ia.ianormalize(log(g+1)),title='log of sobel')

sobel

log of sobel

Example 2

Display angle.

1 f = ia.iacircle([200,300], 90, [100,150])
2 m,t = ia.iasobel(f)
3 adshow(ia.ianormalize(m), title='magnitude of Sobel filtering')
4 t[m<2] = 0
5 adshow(ia.ianormalize(t), 'angle of edges with magnitude above 2')
6 h = ia.iahistogram(ia.ianormalize(t[m>=2]).astype('uint8'))
7 adshow(ia.iaplot(h),'histogram of oriented gradient')

magnitude of Sobel filtering

angle of edges with magnitude above 2

histogram of oriented gradient

Equation

See Also