Question: % Load an image img = imread ( ' K: / Belg / cg / Turkey . jpg ' ) ; % Display the original

% Load an image
img = imread('K:/Belg/cg/Turkey.jpg');
% Display the original image
figure, imshow(img), title('Original Image');
% Convert the image from RGB to HSV color space for easier color segmentation
hsv_img = rgb2hsv(img);
figure;
% Define thresholds for the red and green colors in the HSV space
% Adjust these ranges based on your image specifics and the colors you want to segment
colors ={
'red', [0.00.250.250.5]; % Hue range for red, Saturation min, Value min
'green', [0.00.150.250.5]% Hue range for green, Saturation min, Value min
};
% Initialize a figure for displaying multiple outputs
% Loop through each color and process segmentation
for i =1:size(colors,1)
color_name = colors{i,1};
hue_min = colors{i,2}(1);
hue_max = colors{i,2}(2);
sat_min = colors{i,2}(3);
val_min = colors{i,2}(4);
% Create a binary mask where specific color areas are white
color_mask =(hsv_img(:,:,1)>= hue_min & hsv_img(:,:,1)<= hue_max) & (hsv_img(:,:,2)>= sat_min) & (hsv_img(:,:,3)>= val_min);
% Apply morphological operations to clean up the mask
% Remove noise using opening with a square element
color_mask = imopen(color_mask, strel('rectangle',[33]));
% Fill holes using closing with a square element
color_mask = imclose(color_mask, strel('rectangle',[55]));
% Segment the color from the original image
color_segmented = img;
color_segmented(~repmat(color_mask, [1,1,3]))=0;
% Display the mask and the segmented image
subplot(2, size(colors,1), i);
imshow(color_mask), title([color_name ' Mask']);
subplot(2, size(colors,1), size(colors,1)+i);
imshow(color_segmented), title([color_name ' Segmented']);
end
I am gonna send you another code please make like this

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!