Question: This is first code % Load an image img = imread ( ' image 3 . jpg ' ) ; % Display the original image

This is first code
% Load an image
img = imread('image3.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
ans this is second code
% Load an image
image = imread('K:/Belg/cg/Turkey.jpg');
% Convert the image from RGB to HSV
hsv_image = rgb2hsv(image);
% Define initial HSV range for segmentation (example values for red color)
h_min =0;
s_min =0.5;
v_min =0.5;
h_max =0.1;
s_max =1;
v_max =1;
% Create a mask for the selected color
mask =(hsv_image(:,:,1)>= h_min) & (hsv_image(:,:,1)<= h_max) & ...
(hsv_image(:,:,2)>= s_min) & (hsv_image(:,:,2)<= s_max) & ...
(hsv_image(:,:,3)>= v_min) & (hsv_image(:,:,3)<= v_max);
% Apply dilation to enhance the segmentation result
SE = ones(5); % Define a 5x5 square structuring element
dilated_mask = imdilate(mask, SE);
% Apply the dilated mask to the original image
result = bsxfun(@times, image, cast(dilated_mask, 'like', image));
% Display the original image, mask, and the result
figure;
subplot(1,3,1);
imshow(image);
title('Original Image');
subplot(1,3,2);
imshow(mask);
title('Mask');
subplot(1,3,3);
imshow(result);
title('Result withDilation');
I want you to do that make second code like first code

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!