def canny(F, s): Fx = gD(F, s, 1, 0) Fy = gD(F, s, 0, 1) Fxy = gD(F, s, 1, 1) Fxx = gD(F, s, 2, 0) Fyy = gD(F, s, 0, 2) Fw = np.sqrt(Fx**2 + Fy**2) Fww = (Fx**2 * Fxx) + (2 * Fx * Fy * Fxy) + (Fy**2 * Fyy) canny = np.zeros_like(Fw) height, width = F.shape # For all pixels check if the second derivative of that pixel has changed # sign. If so, draw a pixel. for y in range(width - 2): for x in range(height - 2): if ((Fww[y-1][x-1] > 0 and Fww[y+1][x-1] < 0) or (Fww[y-1][x] > 0 and Fww[y+1][x] < 0) or (Fww[y-1][x+1] > 0 and Fww[y+1][x+1] < 0) or (Fww[y-1][x-1] < 0 and Fww[y+1][x-1] > 0) or (Fww[y-1][x] < 0 and Fww[y+1][x] > 0) or (Fww[y-1][x+1] < 0 and Fww[y+1][x+1] > 0) or (Fww[y-1][x-1] > 0 and Fww[y-1][x+1] < 0) or (Fww[y][x-1] > 0 and Fww[y][x+1] < 0) or (Fww[y+1][x-1] > 0 and Fww[y+1][x+1] < 0) or (Fww[y-1][x-1] < 0 and Fww[y-1][x+1] > 0) or (Fww[y][x-1] < 0 and Fww[y][x+1] > 0) or (Fww[y-1][x-1] < 0 and Fww[y+1][x+1] > 0)): canny[y][x] = 1 return canny
def comparison(): F = plt.imread('../images/cameraman.png') # Values and meshgrid used below. x = np.arange(-128, 128) y = np.arange(-128, 128) Y, X = np.meshgrid(x, y) A = 1 V = 6*np.pi/201 # Fx Fx = A*V*np.cos(V*X) # dFx dFx = gD(F, 1, 1, 0) plt.subplot(131) plt.title("Fx") plt.imshow(Fx, cmap=plt.cm.gray) plt.subplot(132) plt.title("dFx") plt.imshow(dFx, cmap=plt.cm.gray) plt.subplot(133) plt.title("Fx/dFx") plt.imshow(Fx/dFx, cmap=plt.cm.gray) plt.show()