Beispiel #1
0
def canny(F, s, high_threshold, low_threshold):
    '''Apply a canny edge detector to the image.'''
    blurred = gD(F, s, 0, 0)

    filt = gauss1(1.4, f1_1)
    Gx = convolve1d(blurred, filt, axis=0, mode='nearest')
    Gy = convolve1d(blurred, filt, axis=1, mode='nearest')
    G = zeros(F.shape)
    angle = zeros(F.shape, dtype='int')
    after_nm = zeros(F.shape)
    
    # Finding intensity grade
    for x in xrange(len(G[0])):
        for y in xrange(len(G)):
            G[x][y] = sqrt(Gx[x][y] ** 2 + Gy[x][y] ** 2)
            angle[x][y] = int( \
                    round(arctan2(Gx[x][y], Gy[x][y]) * 4 / pi + 1)) % 4
    
    # Non-maximum suppression
    for x in xrange(len(G[0])):
        for y in xrange(len(G)):
            if angle[x][y] == 0:
                side_one = G[x+1][y] if inImage(G, x+1, y) else 0
                side_two = G[x-1][y] if inImage(G, x-1, y) else 0
            elif angle[x][y] == 1:
                side_one = G[x-1][y-1] if inImage(G, x-1, y-1) else 0
                side_two = G[x+1][y+1] if inImage(G, x+1, y+1) else 0
            elif angle[x][y] == 2:
                side_one = G[x][y+1] if inImage(G, x, y+1) else 0
                side_two = G[x][y-1] if inImage(G, x, y-1) else 0
            elif angle[x][y] == 3:
                side_one = G[x-1][y+1] if inImage(G, x-1, y+1) else 0
                side_two = G[x+1][y-1] if inImage(G, x+1, y-1) else 0
            
            if not (G[x][y] > side_one and G[x][y] > side_two):
                after_nm[x][y] = 0
            else:
                after_nm[x][y] = G[x][y]
    
    # Hysteresis thresholding
    high_threshold *= (after_nm.max() - after_nm.min()) / 255
    low_threshold *= (after_nm.max() - after_nm.min()) / 255
    after_threshold = zeros(after_nm.shape, dtype = int)
    
    def follow_edge(x, y):
        """Follow an edge by recursively checking if the neighbours are in 
        it."""
        after_threshold[x][y] = 1

        for x in xrange(-1, 2):
            for y in xrange(-1, 2):
                if (not x or not y):
                    if after_nm[x][y] >= low_threshold and not after_threshold:
                        follow_edge(x, y)
    
    # Make border pixels zero
    for i in xrange(len(after_nm[0])):
        after_nm[i][0] = 0
        after_nm[i][len(after_nm) - 1] = 0
        
    for i in xrange(len(after_nm) - 1):
        after_nm[0][i] = 0
        after_nm[len(after_nm[0]) - 1][i] = 0
    
    # Follow each line
    for x in xrange(len(after_nm[0])):
        for y in xrange(len(after_nm)):
            if after_nm[x][y] >= high_threshold and not after_threshold[x][y]:
                follow_edge(x, y)
    
    return after_threshold
Beispiel #2
0
from sys import argv, exit
from gaussian_functions import gD

if len(argv) != 2:
    print "Usage: python time.py s"
    exit(1)
           
s = float(argv[1])

Image = imread('cameraman.png')
Images = zeros((6, Image.shape[0], Image.shape[1]))
labels = ['F', 'Fx', 'Fy', 'Fxx', 'Fxy', 'Fyy']
Images[0] = Image
j = 0

Images[1] = gD(Image, s, 1, 0)
Images[2] = gD(Image, s, 0, 1)
Images[3] = gD(Image, s, 2, 0)
Images[4] = gD(Image, s, 1, 1)
Images[5] = gD(Image, s, 0, 2)

figure()
for i in xrange(1, 10):
    if i in [1, 4, 5, 7, 8, 9]:
        subplot(330 + i)
        axis('off')
        imshow(Images[j], cmap='gray')
        title(labels[j])

        j += 1
Beispiel #3
0
s = float(argv[1])
method = argv[2]
show_out = int(argv[3])
Image = imread('cameraman.png')

if method == '2D':
    Gs = gauss(s)
    G = convolve(Image, Gs, mode='nearest')
elif method == '1D':
    Gsx = gauss1(s, f1)
    G2 = convolve1d(Image, Gsx, axis=0, mode='nearest')
    G2 = convolve1d(G2, Gsx, axis=1, mode='nearest')
elif method == 'gD':
    iorder = 2
    jorder = 2
    result = gD(Image, s, iorder, jorder)
else:
    print "Invalid method"
    exit(1)

if show_out == 1:
    if method == '2D':
        fig = figure()
        X = arange(0, Gs.shape[0])
        Y = arange(0, Gs.shape[1])

        # create meshgrid
        X, Y = meshgrid(X, Y)

        # matplot lib version < 1.0 uses Axes3D
        ax = Axes3D(fig)