temp = zeros((repeat)) if method == "2D": for i in xrange(1, 20): for j in xrange(0, repeat): start = time() Gs = gauss(i) G = convolve(Image, Gs, mode="nearest") stop = time() temp[j] = stop - start timings[i] = average(temp) elif method == "1D": for i in xrange(1, 20): for j in xrange(0, repeat): start = time() Gsx = gauss1(i, f1) G2 = convolve1d(Image, Gsx, axis=0, mode="nearest") G2 = convolve1d(G2, Gsx, axis=1, mode="nearest") stop = time() temp[j] = stop - start timings[i] = average(temp) else: print "Invalid method" exit(1) figure() plot(timings, label=method) xlabel("s") ylabel("Time(s)") legend() show()
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
if len(argv) != 4: print "Usage: python gauss.py s '1D'|'2D'|'gD' 1|0 (1 shows output \ images, 0 does not)" exit(1) 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])