% Read the image
image = imread('F1.jpg');

% Convert the image to HSV color space to handle color thresholds
hsvImage = rgb2hsv(image);
hue = hsvImage(:,:,1);
saturation = hsvImage(:,:,2);
value = hsvImage(:,:,3);

% Define thresholds for gray pixels (considering some red pixels)
grayHueMin = 0.0; % This range includes both gray and red pixels in gray zones
grayHueMax = .000005;
graySatMax = 0.4; % Maximum saturation for gray areas

% Create a mask for gray and red-in-gray pixels
grayMask = (hue >= grayHueMin & hue <= grayHueMax) & (saturation <= graySatMax);

% Convert the image to grayscale to include another thresholding layer
grayImage = rgb2gray(image);

% Apply another threshold to grayscale image to refine gray areas
% You may need to adjust this value based on the image
grayThreshold = 130; 

% Create a binary mask based on the grayscale threshold
binaryGrayMask = grayImage < grayThreshold;

% Combine the masks to ensure all gray and red-in-gray pixels are considered
finalBinaryMask = grayMask | binaryGrayMask;

% Display the result
imshow(finalBinaryMask);

% Save the binary image
imwrite(finalBinaryMask, 'binary_image_refined.png');
