Question: % Load an image img = imread ( ' K: / Belg / cg / Turkey . jpg ' ) ; % Display the original
Load an image
img imreadK:BelgcgTurkeyjpg;
Display the original image
figure, imshowimg titleOriginal Image';
Convert the image from RGB to HSV color space for easier color segmentation
hsvimg rgbhsvimg;
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 :sizecolors
colorname colorsi;
huemin colorsi;
huemax colorsi;
satmin colorsi;
valmin colorsi;
Create a binary mask where specific color areas are white
colormask hsvimg:: huemin & hsvimg:: huemax & hsvimg:: satmin & hsvimg:: valmin;
Apply morphological operations to clean up the mask
Remove noise using opening with a square element
colormask imopencolormask, strelrectangle;
Fill holes using closing with a square element
colormask imclosecolormask, strelrectangle;
Segment the color from the original image
colorsegmented img;
colorsegmented~repmatcolormask, ;
Display the mask and the segmented image
subplot sizecolors i;
imshowcolormask titlecolorname Mask';
subplot sizecolors sizecolorsi;
imshowcolorsegmented titlecolorname 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
