How to find contours from a Sobel edge detection image

61 views Asked by At

I am doing this to apply Sobel edge detection to an image

# Sobel edge detection
grad_x = cv2.Sobel(blurred_image, cv2.CV_64F, 1, 0, ksize=3)
grad_y = cv2.Sobel(blurred_image, cv2.CV_64F, 0, 1, ksize=3)
grad_magnitude = np.sqrt(grad_x ** 2 + grad_y ** 2)
sobel_edges_image = cv2.convertScaleAbs(grad_magnitude)

I am able to view it in a window and it looks great, as a next step I am trying to find the contours in the image but I am having trouble with the image format

# Apply thresholding to the Sobel gradient magnitude
_, sobel_binary = cv2.threshold(sobel_edges_image, 0, 255, cv2.THRESH_BINARY)

# Ensure sobel_binary is a single-channel 8-bit image
if len(sobel_binary.shape) != 2 or sobel_binary.dtype != np.uint8:
    raise ValueError("sobel_binary is not in the expected format. It should be a single-channel 8-bit image.")

# Find contours in the binary image
sobel_contours, _ = cv2.findContours(sobel_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Does anyone know how to go about finding contours after sobel edge detection was applied?

0

There are 0 answers