OpenCV: Strange Artifacts in Image Segmentation Using Watershed Algorithm on Noisy Images
I'm trying to figure out I'm integrating two systems and I can't seem to get I'm working on a project and hit a roadblock....... I've searched everywhere and can't find a clear answer... Quick question that's been bugging me - I'm working on an image segmentation project using OpenCV's watershed algorithm, but I'm encountering strange artifacts in the output, especially when the input images have a lot of noise... I am using OpenCV version 4.5.1 and Python 3.8. The input images are pre-processed using GaussianBlur to reduce noise, but even then, the segmented output sometimes has unexpected boundaries and regions that shouldn't exist. Here's the code snippet that I'm currently using for the segmentation: ```python import cv2 import numpy as np # Load the image image = cv2.imread('noisy_image.jpg') # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply GaussianBlur gray_blurred = cv2.GaussianBlur(gray, (5, 5), 0) # Thresholding to get the foreground mask _, thresh = cv2.threshold(gray_blurred, 127, 255, cv2.THRESH_BINARY) # Find the sure background area kernel = np.ones((3, 3), np.uint8) background = cv2.dilate(thresh, kernel, iterations=3) # Find the sure foreground area foreground = cv2.erode(thresh, kernel, iterations=2) # Compute the unknown region unknown = cv2.subtract(background, foreground) # Label markers ret, markers = cv2.connectedComponents(foreground.astype(np.uint8)) markers = markers + 1 # Increment all labels by 1 markers[unknown == 255] = 0 # Mark the unknown region with 0 # Apply the watershed algorithm cv2.watershed(image, markers) image[markers == -1] = [255, 0, 0] # Mark the boundaries in red cv2.imshow('Segmented Image', image) cv2.waitKey(0) cv2.destroyAllWindows() ``` Despite these steps, I still see some unwanted segments where the watershed boundaries appear overly sensitive to noise in certain regions. I've tried adjusting the GaussianBlur parameters and changing the threshold value, but the output remains inconsistent. Has anyone faced a similar issue with the watershed algorithm on noisy images? Any suggestions on best practices or alternative approaches to improve the segmentation quality would be greatly appreciated! Is there a better approach? Thanks in advance! My development environment is macOS. I'm using Python 3.9 in this project. I'm open to any suggestions. Is there a better approach? I've been using Python for about a year now. I'd really appreciate any guidance on this. Thanks in advance!