I am asked to develop an image processing pipeline using OpenCV python to segment a flower from plant images. But I still have problems getting the accurate binary image. I'm given a data set of images of different flower colours to segment, so removing solely certain colours of flowers won't work for all images.
This is my current pipeline code:
def process_image(image_path):
# Read the image
image = cv2.imread(image_path)
# Apply bilateral filter for noise reduction
noise_reduced_image = cv2.bilateralFilter(image, d=9, sigmaColor=75, sigmaSpace=75)
# Convert to grayscale
grayscale_image = cv2.cvtColor(noise_reduced_image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
blurred_image = cv2.GaussianBlur(grayscale_image, (5, 5), 0)
# Threshold the image - this value may need adjustment for your images
ret, thresholded_image = cv2.threshold(blurred_image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Find contours from the binary image
contours, hierarchy = cv2.findContours(thresholded_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Create an empty image for contours which is the same size as the original image
contour_image = np.zeros_like(thresholded_image)
# Draw the contours on the contour image
cv2.drawContours(contour_image, contours, -1, (255), thickness=cv2.FILLED)
# Perform morphological operations to further clean up the image
kernel = np.ones((5, 5), np.uint8)
contour_image = cv2.morphologyEx(contour_image, cv2.MORPH_OPEN, kernel, iterations=7) # Remove noise
dilated_image = cv2.dilate(contour_image, kernel, iterations=1) # Fill in the gaps
final_image = cv2.bitwise_not(dilated_image)
return final_image
Input Image:
Ground truth:




I tried by filtering the yellow color part of the image.
Output: