|
| 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') |
0 commit comments