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
image
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
rgb
hsv
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',
;
Hue range for red, Saturation min, Value min
'green',
Hue range for green, Saturation min, Value min
;
Initialize a figure for displaying multiple outputs
Loop through each color and process segmentation
for i
:size
colors
color
name
colors
i
;
hue
min
colors
i
;
hue
max
colors
i
;
sat
min
colors
i
;
val
min
colors
i
;
Create a binary mask where specific color areas are white
color
mask
hsv
img
:
:
hue
min & hsv
img
:
:
hue
max
&
hsv
img
:
:
sat
min
&
hsv
img
:
:
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
;
Fill holes using closing with a square element
color
mask
imclose
color
mask, strel
rectangle
;
Segment the color from the original image
color
segmented
img;
color
segmented
~repmat
color
mask,
;
Display the mask and the segmented image
subplot
size
colors
i
;
imshow
color
mask
title
color
name
Mask
;
subplot
size
colors
size
colors
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
rgb
hsv
image
;
Define initial HSV range for segmentation
example values for red color
h
min
;
s
min
;
v
min
;
h
max
;
s
max
;
v
max
;
Create a mask for the selected color
mask
hsv
image
:
:
h
min
&
hsv
image
:
:
h
max
&
hsv
image
:
:
s
min
&
