def suppression(img, D, t): """ Step 3: Non-maximum suppression(code contains threshold) Args: img: Numpy ndarray of image to be processed (gradient-intensed image) D: Numpy ndarray of gradient directions for each pixel in img Returns: ... """ M, N = img.shape Z = np.zeros((M, N), dtype=np.int32) DZ = np.zeros((M, N), dtype=np.int32) for i in range(1, M - 1): for j in range(1, N - 1): # find neighbour pixels to visit from the gradient directions if abs(img[i, j]) > t: where = round_angle(D[i, j]) try: if where == 0: if (img[i, j] >= img[i, j - 1]) and (img[i, j] >= img[i, j + 1]): Z[i, j] = img[i, j] DZ[i, j] = D[i, j] elif where == 90: if (img[i, j] >= img[i - 1, j]) and (img[i, j] >= img[i + 1, j]): Z[i, j] = img[i, j] DZ[i, j] = D[i, j] elif where == 135: if (img[i, j] >= img[i - 1, j + 1]) and ( img[i, j] >= img[i + 1, j - 1]): Z[i, j] = img[i, j] DZ[i, j] = D[i, j] elif where == 45: if (img[i, j] >= img[i + 1, j + 1]) and ( img[i, j] >= img[i - 1, j - 1]): Z[i, j] = img[i, j] DZ[i, j] = D[i, j] except IndexError as e: """ Todo: Deal with pixels at the image boundaries. """ pass return Z, DZ
def sharp_curve_remove(img, D, min_pixel, t): """ Step 3: sharp_curve_remove Args: img: Numpy ndarray of image to be processed (gradient-intensed image) D: Numpy ndarray of gradient directions for each pixel in img n: Num of minimal pixels in a line(with same gradient direction) min_pixel: t: Minimal gradient of edge Returns: ... """ M, N = img.shape Z = np.zeros((M, N), dtype=np.int32) for i in range(M): for j in range(N): # find neighbour pixels to visit from the gradient directions # find 10 pixels without too much curve if abs(img[i, j]) > t: where = round_angle(D[i, j]) try: if where == 0: max_length = 1 m = i - 1 while max_length < min_pixel: if m >= 0 and round_angle(D[m, j]) == 0: m = m - 1 max_length = max_length + 1 else: break if max_length >= min_pixel: while max_length >= 0: Z[m, j] = img[m, j] m = m + 1 max_length = max_length - 1 elif where == 90: max_length = 1 m = j - 1 while max_length < min_pixel: if m >= 0 and round_angle(D[i, m]) == 90: m = m - 1 max_length = max_length + 1 else: break if max_length >= min_pixel: while max_length >= 0: Z[i, m] = img[i, m] m = m + 1 max_length = max_length - 1 elif where == 135: max_length = 1 m = i - 1 n = j - 1 while max_length < min_pixel: if m >= 0 and n >= 0 and round_angle(D[m, n]) == 135: m = m - 1 n = n - 1 max_length = max_length + 1 else: break if max_length >= min_pixel: while max_length >= 0: Z[m, m] = img[m, n] m = m + 1 n = n + 1 max_length = max_length - 1 elif where == 45: max_length = 1 m = i - 1 n = j + 1 while max_length < min_pixel: if m > 0 and n < N - 1 and round_angle(D[m, m]) == 45: m = m - 1 n = n + 1 max_length = max_length + 1 else: break if max_length >= min_pixel: while max_length >= 0: Z[m, n] = img[m, n] m = m + 1 n = n - 1 max_length = max_length - 1 except IndexError as e: """ Todo: Deal with pixels at the image boundaries. """ pass return Z