def detector(src, size, sigma, threshold1, threshold2): #Step 1: Filter out noise by Gaussian filtering g_filter = ip.getGaussianKernel(size, sigma) img_smoothed = ip.convolve(src, g_filter) #Step 2: Find gradient intensity and directions: gradient_intensity, gradient_direction = calculateGradient(img_smoothed) #Step 3: Non-max Suppresion (thinning): nonmax_suppressed = nonmax_suppr(gradient_intensity, gradient_direction) #Step 4 and 5: Double Thresholding and Edge Tracking lnkd, thresholded = hysteresis(nonmax_suppressed, gradient_direction, threshold1, threshold2) return thresholded
def calculateGradient(image): operator_y, operator_x = ip.getGradientOperator(ip.SOBEL) intensity_y = ip.convolve(image, operator_y, True) intensity_x = ip.convolve(image, operator_x, True) #I_xy=sqrt(I_x^2+I_y^2) can be approximated as I_xy=abs(I_x)+abs(I_y) #approximation: gradient_strength = np.abs(intensity_y) + np.abs(intensity_x) gradient_intensity = np.hypot(intensity_y, intensity_x) #gradient_intensity = np.abs(intensity_y)+np.abs(intensity_x) gradient_direction = np.arctan2(intensity_y, intensity_x) #round directions to 0, 45, 90, 135 degrees DIRECTION QUANTIZATION gradient_direction = (np.round(gradient_direction*180/np.pi/45)*45) for y in range(gradient_direction.shape[0]): for x in range(gradient_direction.shape[1]): if gradient_direction[y,x] == 180: gradient_direction[y,x]=0 if gradient_direction[y,x] < 0: gradient_direction[y,x]+=180 return gradient_intensity, gradient_direction
img1 = imread('data/I1.jpg') img2 = imread('data/I2.jpg') #convert to grayscale, color features are irrelevant for edge detection img1 = ip.color2gray(img1) img2 = ip.color2gray(img2) img2x = np.copy(img2) img2y = np.copy(img2) #get operators roberts_y, roberts_x = ip.getGradientOperator(ip.ROBERTS) prewitt_y, prewitt_x = ip.getGradientOperator(ip.PREWITT) sobel_y, sobel_x = ip.getGradientOperator(ip.SOBEL) #convolve with operators img1_roberts_x = ip.convolve(img1, roberts_x, no_abs=True) img1_roberts_y = ip.convolve(img1, roberts_y, no_abs=True) img1_prewitt_x = ip.convolve(img1, prewitt_x, no_abs=True) img1_prewitt_y = ip.convolve(img1, prewitt_y, no_abs=True) img1_sobel_x = ip.convolve(img1, sobel_x, no_abs=True) img1_sobel_y = ip.convolve(img1, sobel_y, no_abs=True) #get gradient strength and directions img1_roberts_Gs, img1_roberts_Gd = ip.getGsGd( ip.convolve(img1, roberts_y, no_abs=True), ip.convolve(img1, roberts_x, no_abs=True), ip.ROBERTS) img1_prewitt_Gs, img1_prewitt_Gd = ip.getGsGd(img1_prewitt_y, img1_prewitt_x, ip.PREWITT) img1_sobel_Gs, img1_sobel_Gd = ip.getGsGd(img1_sobel_y, img1_sobel_x, ip.SOBEL)