Skip to content

Commit 01f8911

Browse files
authored
Add files via upload
1 parent 06fddb0 commit 01f8911

16 files changed

+1508
-0
lines changed

Color-Based_Segmentation(multi).m

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
%acquire images
2+
source1 = imread("p1.jpg");
3+
source2 = imread("p2.jpg");
4+
source3 = imread("p3.jpg");
5+
subplot(1,3,1), imshow(source1);
6+
title("Parthenon 1");
7+
subplot(1,3,2), imshow(source2);
8+
title("Parthenon 2");
9+
subplot(1,3,3), imshow(source3);
10+
title("Parthenon 3");
11+
12+
%Calculate Sample Colors in L*a*b* Color Space for Each Region
13+
%{
14+
You can see six major colors in the image: the background color, red, green, purple, yellow, and magenta.
15+
The L*a*b* colorspace (also known as CIELAB or CIE L*a*b*) enables you to quantify these visual differences.
16+
17+
The L*a*b* color space is derived from the CIE XYZ tristimulus values. The L*a*b* space consists of a luminosity 'L*'
18+
or brightness layer, chromaticity layer 'a*' indicating where color falls along the red-green axis, and chromaticity layer 'b*'
19+
indicating where the color falls along the blue-yellow axis.
20+
21+
Your approach is to choose a small sample region for each color and to calculate each sample region's average color in 'a*b*' space.
22+
You will use these color markers to classify each pixel.
23+
%}
24+
25+
load regioncoordinates;
26+
27+
nColors = 6;
28+
sample_regions1 = false([size(source1,1) size(source1,2) nColors]);
29+
sample_regions2 = false([size(source2,1) size(source2,2) nColors]);
30+
sample_regions3 = false([size(source3,1) size(source3,2) nColors]);
31+
32+
for count = 1:nColors
33+
sample_regions(:,:,count) = roipoly(source,region_coordinates(:,1,count), ...
34+
region_coordinates(:,2,count));
35+
end
36+
37+
imshow(sample_regions(:,:,2))
38+
title('Sample Region for Red')
39+
40+
%Convert your source RGB image into an L*a*b* image using rgb2lab
41+
labImage = rgb2lab(source);
42+
43+
%Calculate the mean 'a*' and 'b*' value for each area that you extracted with roipoly.
44+
%These values serve as your color markers in 'a*b*' space
45+
AImage = labImage(:, :, 2);
46+
BImage = labImage(:, :, 3);
47+
48+
color_markers = zeros([nColors, 2]);
49+
50+
for count = 1:nColors
51+
color_markers(count,1) = mean2(AImage(sample_regions(:,:,count)));
52+
color_markers(count,2) = mean2(BImage(sample_regions(:,:,count)));
53+
end
54+
55+
%Example the average color of the red sample region in 'a*b*' space is:
56+
fprintf('[%0.3f,%0.3f] \n',color_markers(2,1),color_markers(2,2));
57+
58+
%Classify Each Pixel Using the Nearest Neighbor Rule
59+
%{
60+
Each color marker now has an 'a*' and a 'b*' value. You can classify each pixel in the lab_fabric image by calculating the Euclidean distance
61+
between that pixel and each color marker. The smallest distance will tell you that the pixel most closely matches that color marker.
62+
For example, if the distance between a pixel and the red color marker is the smallest, then the pixel would be labeled as a red pixel.
63+
64+
Create an array that contains your color labels, i.e., 0 = background, 1 = red, 2 = green, 3 = purple, 4 = magenta, and 5 = yellow.
65+
%}
66+
color_labels = 0:nColors-1;
67+
68+
%Initialize matrices to be used in the nearest neighbor classification
69+
AImage = double(AImage);
70+
BImage = double(BImage);
71+
distance = zeros([size(AImage), nColors]);
72+
73+
%Perform classification
74+
for count = 1:nColors
75+
distance(:,:,count) = ( (AImage - color_markers(count,1)).^2 + ...
76+
(BImage - color_markers(count,2)).^2 ).^0.5;
77+
end
78+
79+
[~,label] = min(distance,[],3);
80+
label = color_labels(label);
81+
clear distance;
82+
83+
%Display Results of Nearest Neighbor Classification
84+
%{
85+
The label matrix contains a color label for each pixel in the source image. Use the label matrix to separate objects in the original
86+
source image by color. Display the five segmented colors as a montage. Also display the background pixels in the image that are not
87+
classified as a color.
88+
%}
89+
rgb_label = repmat(label,[1 1 3]);
90+
segmented_images = zeros([size(source), nColors],'uint8');
91+
92+
for count = 1:nColors
93+
color = source;
94+
color(rgb_label ~= color_labels(count)) = 0;
95+
segmented_images(:,:,:,count) = color;
96+
end
97+
98+
montage({segmented_images(:,:,:,2),segmented_images(:,:,:,3) ...
99+
segmented_images(:,:,:,4),segmented_images(:,:,:,5) ...
100+
segmented_images(:,:,:,6),segmented_images(:,:,:,1)});
101+
title("Montage of Red, Green, Purple, Magenta, and Yellow Objects, and Background")
102+
103+
%Display 'a*' and 'b*' Values of the Labeled Colors
104+
%{
105+
You can see how well the nearest neighbor classification separated the different color populations by plotting the 'a*' and 'b*' values
106+
of pixels that were classified into separate colors. For display purposes, label each point with its color label.
107+
%}
108+
purple = [119/255 73/255 152/255];
109+
plot_labels = {'k', 'r', 'g', purple, 'm', 'y'};
110+
111+
figure
112+
for count = 1:nColors
113+
plot(AImage(label==count-1),BImage(label==count-1),'.','MarkerEdgeColor', ...
114+
plot_labels{count}, 'MarkerFaceColor', plot_labels{count});
115+
hold on;
116+
end
117+
118+
title('Scatterplot of the segmented pixels in ''a*b*'' space');
119+
xlabel('''a*'' values');
120+
ylabel('''b*'' values');
121+
122+
%--------------------------------------------------------------------------%
123+
% Read the image and convert to L*a*b* color space
124+
I = imread('vegetables.jpg');
125+
Ilab = rgb2lab(I);
126+
% Extract a* and b* channels and reshape
127+
ab = double(Ilab(:,:,2:3));
128+
nrows = size(ab,1);
129+
ncols = size(ab,2);
130+
ab = reshape(ab,nrows*ncols,2);
131+
% Segmentation usign k-means
132+
nColors = 4;
133+
[cluster_idx, cluster_center] = kmeans(ab,nColors,...
134+
'distance', 'sqEuclidean', ...
135+
'Replicates', 3);
136+
% Show the result
137+
pixel_labels = reshape(cluster_idx,nrows,ncols);
138+
imshow(pixel_labels,[]), title('image labeled by cluster index')

Color-Based_Segmentation(single).m

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
%acquire image
2+
source = imread("vegetables.jpg");
3+
imshow(source);
4+
title("Vegetables");
5+
6+
%Calculate Sample Colors in L*a*b* Color Space for Each Region
7+
%{
8+
You can see six major colors in the image: the background color, red, green, purple, yellow, and magenta.
9+
The L*a*b* colorspace (also known as CIELAB or CIE L*a*b*) enables you to quantify these visual differences.
10+
11+
The L*a*b* color space is derived from the CIE XYZ tristimulus values. The L*a*b* space consists of a luminosity 'L*'
12+
or brightness layer, chromaticity layer 'a*' indicating where color falls along the red-green axis, and chromaticity layer 'b*'
13+
indicating where the color falls along the blue-yellow axis.
14+
15+
Your approach is to choose a small sample region for each color and to calculate each sample region's average color in 'a*b*' space.
16+
You will use these color markers to classify each pixel.
17+
%}
18+
19+
load regioncoordinates;
20+
21+
nColors = 6;
22+
sample_regions = false([size(source,1) size(source,2) nColors]);
23+
24+
for count = 1:nColors
25+
sample_regions(:,:,count) = roipoly(source,region_coordinates(:,1,count), ...
26+
region_coordinates(:,2,count));
27+
end
28+
29+
imshow(sample_regions(:,:,2))
30+
title('Sample Region for Red')
31+
32+
%Convert your source RGB image into an L*a*b* image using rgb2lab
33+
labImage = rgb2lab(source);
34+
35+
%Calculate the mean 'a*' and 'b*' value for each area that you extracted with roipoly.
36+
%These values serve as your color markers in 'a*b*' space
37+
AImage = labImage(:, :, 2);
38+
BImage = labImage(:, :, 3);
39+
40+
color_markers = zeros([nColors, 2]);
41+
42+
for count = 1:nColors
43+
color_markers(count,1) = mean2(AImage(sample_regions(:,:,count)));
44+
color_markers(count,2) = mean2(BImage(sample_regions(:,:,count)));
45+
end
46+
47+
%Example the average color of the red sample region in 'a*b*' space is:
48+
fprintf('[%0.3f,%0.3f] \n',color_markers(2,1),color_markers(2,2));
49+
50+
%Classify Each Pixel Using the Nearest Neighbor Rule
51+
%{
52+
Each color marker now has an 'a*' and a 'b*' value. You can classify each pixel in the lab_fabric image by calculating the Euclidean distance
53+
between that pixel and each color marker. The smallest distance will tell you that the pixel most closely matches that color marker.
54+
For example, if the distance between a pixel and the red color marker is the smallest, then the pixel would be labeled as a red pixel.
55+
56+
Create an array that contains your color labels, i.e., 0 = background, 1 = red, 2 = green, 3 = purple, 4 = magenta, and 5 = yellow.
57+
%}
58+
color_labels = 0:nColors-1;
59+
60+
%Initialize matrices to be used in the nearest neighbor classification
61+
AImage = double(AImage);
62+
BImage = double(BImage);
63+
distance = zeros([size(AImage), nColors]);
64+
65+
%Perform classification
66+
for count = 1:nColors
67+
distance(:,:,count) = ( (AImage - color_markers(count,1)).^2 + ...
68+
(BImage - color_markers(count,2)).^2 ).^0.5;
69+
end
70+
71+
[~,label] = min(distance,[],3);
72+
label = color_labels(label);
73+
clear distance;
74+
75+
%Display Results of Nearest Neighbor Classification
76+
%{
77+
The label matrix contains a color label for each pixel in the source image. Use the label matrix to separate objects in the original
78+
source image by color. Display the five segmented colors as a montage. Also display the background pixels in the image that are not
79+
classified as a color.
80+
%}
81+
rgb_label = repmat(label,[1 1 3]);
82+
segmented_images = zeros([size(source), nColors],'uint8');
83+
84+
for count = 1:nColors
85+
color = source;
86+
color(rgb_label ~= color_labels(count)) = 0;
87+
segmented_images(:,:,:,count) = color;
88+
end
89+
90+
montage({segmented_images(:,:,:,2),segmented_images(:,:,:,3) ...
91+
segmented_images(:,:,:,4),segmented_images(:,:,:,5) ...
92+
segmented_images(:,:,:,6),segmented_images(:,:,:,1)});
93+
title("Montage of Red, Green, Purple, Magenta, and Yellow Objects, and Background")
94+
95+
%Display 'a*' and 'b*' Values of the Labeled Colors
96+
%{
97+
You can see how well the nearest neighbor classification separated the different color populations by plotting the 'a*' and 'b*' values
98+
of pixels that were classified into separate colors. For display purposes, label each point with its color label.
99+
%}
100+
purple = [119/255 73/255 152/255];
101+
plot_labels = {'k', 'r', 'g', purple, 'm', 'y'};
102+
103+
figure
104+
for count = 1:nColors
105+
plot(AImage(label==count-1),BImage(label==count-1),'.','MarkerEdgeColor', ...
106+
plot_labels{count}, 'MarkerFaceColor', plot_labels{count});
107+
hold on;
108+
end
109+
110+
title('Scatterplot of the segmented pixels in ''a*b*'' space');
111+
xlabel('''a*'' values');
112+
ylabel('''b*'' values');
113+
%--------------------------------------------------------------------------%
114+
% Read the image and convert to L*a*b* color space
115+
I = imread('vegetables.jpg');
116+
Ilab = rgb2lab(I);
117+
% Extract a* and b* channels and reshape
118+
ab = double(Ilab(:,:,2:3));
119+
nrows = size(ab,1);
120+
ncols = size(ab,2);
121+
ab = reshape(ab,nrows*ncols,2);
122+
% Segmentation usign k-means
123+
nColors = 6;
124+
[cluster_idx, cluster_center] = kmeans(ab,nColors,...
125+
'distance', 'sqEuclidean', ...
126+
'Replicates', 3);
127+
% Show the result
128+
pixel_labels = reshape(cluster_idx,nrows,ncols);
129+
imshow(pixel_labels,[]), title('image labeled by cluster index')

Color_Space_Lab-Representation.m

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
%acquire image
2+
source = imread("vegetables.jpg");
3+
4+
%Convert your source RGB image into an L*a*b* image using rgb2lab
5+
labImage = rgb2lab(source);
6+
7+
%Get each channel 'L*', 'a*' and 'b*' for the L*a*b* image
8+
LImage = labImage(:, :, 1);
9+
AImage = labImage(:, :, 2);
10+
BImage = labImage(:, :, 3);
11+
12+
%Show the L*a*b* image
13+
subplot(4, 2, 1.5);
14+
imshow(labImage);
15+
title('L*a*b* Image', 'FontSize', 15);
16+
17+
%Show each of the channels individually
18+
%and by scaling the display based on the range of pixel values
19+
subplot(4, 2, 3);
20+
imshow(LImage);
21+
title('L channel Image', 'FontSize', 15);
22+
subplot(4, 2, 4);
23+
imshow(LImage, []);
24+
title('L channel scaled Image', 'FontSize', 15);
25+
subplot(4, 2, 5);
26+
imshow(AImage);
27+
title('A channel Image', 'FontSize', 15);
28+
subplot(4, 2, 6);
29+
imshow(AImage, []);
30+
title('A channel scaled Image', 'FontSize', 15);
31+
subplot(4, 2, 7);
32+
imshow(BImage);
33+
title('B channel Image', 'FontSize', 15);
34+
subplot(4, 2, 8);
35+
imshow(BImage, []);
36+
title('B channel scaled Image', 'FontSize', 15);

0 commit comments

Comments
 (0)