imagematlabimage-processingcomputer-vision

How to convert a RGB image to a two dimensional image in Matlab


I have a RGB image called imrgb, the size of which is 320*512*3. I want to know how to convert it to a two dimensional image?
I saw some colorful images, but they are still two dimensional instead of three dimensional. Can anyone tell me how to convert it to a two-dimensional image but still looks colorful?

enter image description here


Solution

  • If I understood right you can store the image data as a 2D array and use a colormap to assign it colors, displaying it as a "colorful" image. In this case the colormap will be a Nx3 array where each row corresponds to an index present (or not) in the image data. So if the colormap is a 256x3 array, for example, the image will be made of indices ranging from 0 to 255.

    For example, let's consider the peppers.png demo image that ships with Matlab. Let's read and use the rgb2ind function to store the image data in variable X and its associated colormap in variable map.

    a = imread('peppers.png');
    [X,map] = rgb2ind(a,256);
    

    If we ask the size of X, we get:

    ans =
    
       384   512
    

    So a 2D variable, even though the original image (a) was 3D (rgb).

    If we wish to display this 2D data as a colorful we need to provide a suitable colormap. For instance, we can use the original colormap (stored in map) in order to get the following:

    image(X)
    colormap(map)
    axis off
    

    enter image description here

    or we can use any other colormap of our choice.

    For example:

    image(X)
    colormap(jet(200)) %// The size can be different than the original colormap
    axis off
    

    yielding:

    enter image description here

    So as you see the image looks 3D but the data is actually 2D.

    Is this what you meant?