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
3
.
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
2
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',
[
0
.
0
0
.
2
5
0
.
2
5
0
.
5
]
;
%
Hue range for red, Saturation min, Value min
'green',
[
0
.
0
0
.
1
5
0
.
2
5
0
.
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
'
,
[
3
3
]
)
)
;
%
Fill holes using closing with a square element
color
_
mask
=
imclose
(
color
_
mask, strel
(
'
rectangle
'
,
[
5
5
]
)
)
;
%
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
=
rgb
2
hsv
(
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
)
&

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!