In following code instead of using rgb2lab i have to use srgb2lab, i am getting error?
3 views (last 30 days)
Show older comments
e = imread('peppers.png'); lab_he = rgb2lab(he);
ab = lab_he(:,:,2:3); nrows = size(ab,1); ncols = size(ab,2); ab = reshape(ab,nrows*ncols,2);
nColors = 3; % repeat the clustering 3 times to avoid local minima [cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols); figure,imshow(pixel_labels,[]), title('image labeled by cluster index');
1 Comment
Stephen23
on 14 May 2018
@jayamala pakhare: please show us the complete error message. This means all of the red text.
Answers (1)
Image Analyst
on 14 May 2018
You read the image into "e" instead of "he". This works for me:
Try this improved code:
% Read in original color image.
rgbImage = imread('peppers.png');
subplot(1, 2, 1);
imshow(rgbImage);
title('Original RGB Image', 'FontSize', 18);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Convert to LAB color space.
labImage = rgb2lab(rgbImage);
ab = labImage(:,:,2:3);
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
numberOfClasses = 3; % Tell it we want only 3 color classes.
% Set Replicates = 3 to repeat the clustering 3 times to avoid local minima.
[cluster_indexes, cluster_centers] = kmeans(ab, numberOfClasses, 'distance','sqEuclidean','Replicates',3);
% Reshape back into 2-D image.
pixel_labels = reshape(cluster_indexes,nrows,ncols);
subplot(1, 2, 2);
imshow(pixel_labels,[])
title('Classified Image as labeled by kmeans()', 'FontSize', 18);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!