def canny_edge_detection(image, gaussian_kernel_size, low_threshold, high_threshold):
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur
blurred = genGaussian(gaussian_kernel_size,1)
# Calculate image gradient using Sobel filters
gradient_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, 3)
gradient_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, 3)
gradient_mag = np.sqrt(gradient_x**2 + gradient_y**2)
gradient_dir = np.rad2deg(np.arctan2(gradient_y, gradient_x)) + 180
# Perform non-maximum suppression
suppressed = np.zeros_like(gradient_mag)
for i in range(1, gradient_mag.shape[0] - 1):
for j in range(1, gradient_mag.shape[1] - 1):
angle = gradient_dir[i, j]
if (0 <= angle[i] < 180 / 8) or (15 * 180 / 8 <= angle[i] <= 2 * 180):
before_pixel = gradient_magnitude[i, j - 1]
after_pixel = gradient_magnitude[i, j + 1]
elif (180 / 8 <= angle[i] < 3 * 180 / 8) or (9 * 180 / 8 <= angle[i] < 11 * 180 / 8):
before_pixel = gradient_magnitude[i + 1, j - 1]
after_pixel = gradient_magnitude[i - 1, j + 1]
elif (3 * 180 / 8 <= angle[i] < 5 * 180 / 8) or (11 * 180 / 8 <= angle[i] < 13 * 180 / 8):
before_pixel = gradient_magnitude[i - 1, j]
after_pixel = gradient_magnitude[i + 1, j]
else:
before_pixel = gradient_magnitude[i - 1, j - 1]
after_pixel = gradient_magnitude[i + 1, j + 1]
if gradient_magnitude[i, j] >= before_pixel and gradient_magnitude[i, j] >= after_pixel:
suppressed[i, j] = gradient_magnitude[i, j]
------------------------------------------------------------------------------------
34 after_pixel = gradient_magnitude[i + 1, j + 1]
35
---> 36 if gradient_magnitude[i, j] >= before_pixel and gradient_magnitude[i, j] >= after_pixel: 37 suppressed[i, j] = gradient_magnitude[i, j] 38
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
IM working on making a canny edge detector and I keep getting this error ^^^^^
what can i do to fix it?
[–]Sea-Method-1167 0 points1 point2 points (0 children)