Dip 04 Updated
Dip 04 Updated
Dip 04 Updated
Experiment 4
Implementation of different types of images and conversion of images
to different formats
1: Indexed Images:
An indexed image consists of a data matrix, X, and a colormap matrix, map. The data matrix can be of
class uint8, uint16, or double. The colormap matrix is an m-by-3 array of class double containing
floating-point values in the range [0,1]. Each row of map specifies the red, green, and blue components
of a single color. An indexed image uses direct mapping of pixel values to colormap values. The color
of each image pixel is determined by using the corresponding value of X as an index into map. The
value 1 points to the first row in map, the value 2 points to the second row, and so on.
A colormap is often stored with an indexed image and is automatically loaded with the image
when you use the imread function. However, you are not limited to using the default colormap--you can
use any colormap that you choose. The figure below illustrates the structure of an indexed image. The
pixels in the image are represented by integers, which are pointers (indices) to color values stored in the
colormap. The following figure depicts an indexed image.
Example 1 :
[X,map] = imread('trees.tif');
imshow(X,map),title('Indexed image')
2: Intensity Images
An intensity image is a data matrix, I, whose values represent intensities within some range. MATLAB
stores an intensity image as a single matrix, with each element of the matrix corresponding to one image
pixel. The matrix can be of class double, uint8, or uint16.While intensity images are rarely saved with a
colormap, MATLAB uses a colormap to display them. The elements in the intensity matrix represent
various intensities, or gray levels, where the intensity 0 usually represents black and the intensity 1, 255,
or 65535 usually represents full intensity, or white.
The figure below depicts an intensity image of class double.
Example 2:
I = imread('rice.png')
imshow(I)
3: Binary Images
In a binary image, each pixel assumes one of only two discrete values. Essentially, these two values
correspond to on and off. A binary image is stored as a logical array of 0's (off pixels) and 1's (on
pixels). The figure below depicts a binary image.
Example 3:
A = imread('cameraman.tif');
B=im2bw(A)
imshow(B)
4: RGB Images
An RGB image, sometimes referred to as a true-color image, is stored in MATLAB as an m-by-n-by-3
data array that defines red, green, and blue color components for each individual pixel. RGB images do
not use a palette. The color of each pixel is determined by the combination of the red, green, and blue
intensities stored in each color plane at the pixel's location. Graphics file formats store RGB images as
24-bit images, where the red, green, and blue components are 8 bits each. This yields a potential of 16
million colors. The precision with which a real-life image can be replicated has led to the commonly
used term true-color image.
An RGB array can be of class double, uint8, or uint16. In an RGB array of class double, each
color component is a value between 0 and 1. A pixel whose color components are (0,0,0) is displayed as
black, and a pixel whose color components are (1,1,1) is displayed as white. The three-color
components for each pixel are stored along the third dimension of the data array.
For example, the red, green, and blue color components of the pixel (10,5) are stored in RGB(10,5,1),
RGB(10,5,2), and RGB(10,5,3), respectively. The following figure depicts an RGB image of class
double.
Example 4:
C = imread('ngc6543a.jpg'); %imread returns a 650-by-600-by-3 array, C.
Example 5 :
RGB = imread('peppers.png');
imshow(RGB)
You can think of an RGB image as a "stack" of three gray-scale images. These gray-scale images are
commonly called the component images.
Example 6:
img = imread('peppers.jpg');
figure, imshow(img)
red = img(:,:,1);
green = img(:,:,2);
blue = img(:,:,3);
a = zeros(size(img, 1), size(img, 2));
% m = size(obj,dim) returns the length of the dimension specified by the scalar dim. For
example, size(obj,1) returns the number of rows.
just_red = cat(3, red, a, a);
figure, imshow(just_red)
just_green = cat(3, a, green, a);
figure, imshow(just_green)
just_blue = cat(3, a, a, blue);
figure, imshow(just_blue)
The following table shows how to convert between the different formats given above.
[r,c]=size(a);
for i=1:1:r
k=1;
for j=c:-1:1
temp=a(i,k);
result(i,k)=a(i,j);
result(i,j)=temp;
k=k+1;
end
end
subplot(1,2,1),imshow(a)
subplot(1,2,2),imshow(result)
LAB TAKS:
TASK 1: Write a MATLAB code that reads a gray scale image given as an input and
generates the following resultant image given below.