Matlab Tutorial : Digital Image Processing 2 - RGB & Indexed Color
In this chapter, we'll see how we can get information about an image.
We'll use the following functions / commands:
- imfinfo()
- impixelinfo
- rgb2ind()
- rgb2gray()
- colormap(jet)
I loaded an image (Sun.jpg)
The imfinfo() gives us lots of information. So, when we do:
info = imfinfo('Sun.jpg');
When we double click the variable info on the Workspace pane, we get the details:
Of course, we get the same info if we inquire the variable by typing it into the Command Window:
>> info Filename: [1x77 char] FileModDate: '13-Apr-2014 18:18:38' FileSize: 1767119 Format: 'jpg' FormatVersion: '' Width: 1780 Height: 1292 BitDepth: 24 ColorType: 'truecolor' FormatSignature: '' NumberOfSamples: 3 CodingMethod: 'Huffman' CodingProcess: 'Sequential' Comment: {}
Because the image itself is an array, we can check the values it is storing. For example, if we want to know the values of the pixel at (350, 250):
>> img(350,250,:) ans(:,:,1) = 157 ans(:,:,2) = 70 ans(:,:,3) = 0
That's the intensity of RGB of the pixel.
Also, there is a command impixelinfo. We type the command into the Command Window, and move the mouse on the image. Then, we'll see coordinates and RGB info (i.e. (455, 988) [255 241 12]) are being displayed while the mouse is on the picture. We can copy that info by right-mouse click and use it somewhere if necessary.
This rgb2ind() converts RGB image to indexed image. Indexed image has an advantage in terms of resource usage over RGB color.
Here is the calculation from wiki - Indexed Color:
"Indexed color saves a lot of memory, storage space, and transmission time: using truecolor, each pixel needs 24 bits, or 3 bytes. A typical 640x480 VGA resolution truecolor uncompressed image needs 640x480x3 = 921,600 bytes (900 KiB). Limiting the image colors to 256, every pixel needs only 8 bits, or 1 byte each, so the example image now needs only 640x480x1 = 307,200 bytes (300 KiB), plus 256x3 = 768 additional bytes to store the palette map in itself (assuming RGB), approx. one third of the original size."
Let's convert the RGB image to an indexed image with 32 colors. We'll use the following function:
[IND,map] = rgb2ind(RGB_image,n)
This converts the RGB_image to an indexed image IND using minimum variance quantization. The map contains at most n colors. n must be less than or equal to 65536.
>> [IND,map] = rgb2ind(img,32); >> figure('Name','Indexed image with 32 Colors'); >> imshow(IND);
This will show the image based on the IND values:
When we do mapping:
>> colormap(map)
It looks like we need to scale the index properly. So, we need to use imagesc():
>> [IND,map] = rgb2ind(img,32); >> figure('Name','Indexed image with 32 Colors');
Then, mapping:
>> colormap(map)
We have other built-in colormaps such as jet and spring. We want to map grayscale image to these colormaps:
Here is the grayscale image converted using rgb2gray():
img = imread('Sun.jpg'); gray = rgb2gray(img); imshow(gray);
jet colormap:
colormap(jet);
spring colormap:
colormap(spring);
Matlab provides built-in colormaps as shown in the picture below:
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization