I want to convert my RGB image into an indexed image with a pre-specified colormap that contains all the colors in my image.
I tried using RGB2IND with the colormap approximation method with the following syntax:
gmap = [0:255;0:255;0:255]'/255; % this is the colormap
rgb1 = zeros(256,256,3); % create the gray-scale RGB image
rgb1(:,:,1) = repmat([0:255]/255,256,1);rgb1(:,:,2) = repmat([0:255]/255,256,1);rgb1(:,:,3) = repmat([0:255]/255,256,1);rgb2 = rgb2ind(rgb1,gmap, 'nodither'); % convert RGB->index with colormap
rgb3 = ind2rgb(rgb2, gmap); % convert index->RGB with colormap
diff = sum(sum(sum(rgb1-rgb3))) % find the difference between original and new image
However, the difference between the original and converted image is not zero, which means the images are different.
Best Answer