I want to create binary mask of an image. I am using OpenCV, however the output is not as expected. I am using the following code -
import cv2
def create_adaptive_binary_mask(image_path):
# Read the image
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Apply adaptive thresholding
binary_mask = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Display or save the binary mask
cv2.imshow('Adaptive Binary Mask', binary_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
create_adaptive_binary_mask('1.jpg')
Output Image:
Expected Output Image:
This can be done using flood fill in Python/OpenCV.
Input:
Floodfilled Image:
Morphology Eroded Image:
Final Mask Image: