Reduce number of colors in matlab using rgb2ind

后端 未结 2 602

I am doing some image processing and I needed to reduce the number of colors of an image. I found that rgb2ind could do that and wrote the following snippet:

<
2条回答
  •  时光取名叫无心
    2021-01-21 14:56

    It may perceptually look like there is more than 6 colours, but there is truly 6 colours. If you take a look at your map variable, it will be a 6 x 3 matrix. Each row contains a colour that you want to quantize your image to.

    To double check, convert this image into a grayscale image, then do a histogram of this image. If rgb2ind worked, you should only see 6 spikes in the histogram.

    BTW, to be able to reconstruct your problem, you used the peppers.png image that is built-in to MATLAB's system path. As such, this is what I did to describe what I'm talking about:

    RGB = imread('peppers.png');
    
    %// Your code
    [X,map] = rgb2ind(RGB,6,'nodither');
    X = rgb2ind(RGB, map);
    rgb=ind2rgb(X,map);
    rgb_uint8=uint8(rgb*255+0.5);
    imshow(rgb_uint8);
    
    %// My code - Double check colour distribution
    figure;
    imhist(rgb2gray(rgb_uint8));
    axis tight;
    

    This is the figure I get:

    enter image description here

    As you can see, there are 6 spikes in our histogram. If there are truly 6 unique colours when you ran your code, then there should be an equivalent of 6 equivalent grayscale intensities when you convert the image into grayscale, and the histogram above verifies our findings.

    As such, you are quantizing your image to 6 colours, but it doesn't look like it due to quantization noise of your image.

提交回复
热议问题