Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views12 pages

Dip 04 Updated

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 12

DIGITAL IMAGE PROCESSING

Experiment 4
Implementation of different types of images and conversion of images
to different formats

CLO 1: Construct the experiments/projects of varying complexities

CLO 2: Use modern tools and languages.

CLO 3: Demonstrate an original solution of problem under discussion.

CLO 4: Work individually as well as in teams.


1: Images in MATLAB and the Image Processing Toolbox
The basic data structure in MATLAB is the array, an ordered set of real or complex elements. This
object is naturally suited to the representation of images, real-valued ordered sets of color or
intensity data. MATLAB stores most images as two-dimensional arrays (i.e., matrices), in which
each element of the matrix corresponds to a single pixel in the displayed image. (Pixel is derived
from picture element and usually denotes a single dot on a computer display)
For example, an image composed of 200 rows and 300 columns of different colored dots would
be stored in MATLAB as a 200-by-300 matrix. Some images, such as RGB, require a three-
dimensional array, where the first plane in the third dimension represents the red pixel intensities,
the second plane represents the green pixel intensities, and the third plane represents the blue pixel
intensities. This convention makes working with images in MATLAB similar to working with any
other type of matrix data, and makes the full power of MATLAB available for image processing
applications.
For example, you can select a single pixel from an image matrix using normal matrix
subscripting. I(2,15) .This command returns the value of the pixel at row 2, column 15 of the image
I
1.1 Working with Images in MATLAB
Images are most commonly stored in MATLAB using the logical, uint8, uint16 and double data
types. You can perform many standard MATLAB array manipulations on uint8 and uint16 image
data, including Basic arithmetic operations, such as addition, subtraction, and multiplication.
Indexing, including logical indexing Reshaping, reordering, and concatenating Reading from and
writing to MAT-files Using relational operators Certain MATLAB functions, including the find, all,
any, conv2, convn, fft2, fftn, and sum functions, accept uint8 or uint16 data but return data in
double-precision format.
1.2 Storage Classes in the Toolbox.
By default, MATLAB stores most data in arrays of class double. The data in these arrays is stored as
double-precision (64-bit) floating-point numbers. All MATLAB functions work with these arrays.
For image processing, however, this data representation is not always ideal. The number of pixels in
an image can be very large; for example, a 1000-by-1000 image has a million pixels. Since each
pixel is represented by at least one array element, this image would require about 8 megabytes of
memory. To reduce memory requirements, MATLAB supports storing image data in arrays as 8-bit
or 16-bit unsigned integers, class uint8 and uint16. These arrays require one eighth or one fourth as
much memory as double arrays.

1.3 Types of Images in the Toolbox

The Image Processing Toolbox supports four basic types of images:


1. Indexed images
2. Intensity images
3. Binary images
4. RGB images

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.

Image(C) %Display the image.

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.

imshow(RGB(:,:,1)),title('Red component image')

imshow(RGB(:,:,2)),title('Green component image')


imshow(RGB(:,:,3)),title('Blue component image')

2- How to Seprate the 3 Channels of an RGB Image


% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
Each one of those arrays is just a simple monochrome gray scale array, and will appear as gray scale if
you use imshow(). If you want to see it appear in the color of the color channel that it represents then
you'd have to either use cat() to make an RGB image out of it:
z = zeros(size(redChannel));
redAppearingImage = cat(3, redChannel, z, z);
imshow(redAppearingImage);

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)

3: How to convert between different formats

The following table shows how to convert between the different formats given above.

Operation: Matlab command:

Convert between intensity/indexed/RGB format to binary format. dither()

Convert between intensity format to indexed format. gray2ind()

Convert between indexed format to intensity format. ind2gray()

Convert between indexed format to RGB format. ind2rgb()

Convert a regular matrix to intensity format by scaling. mat2gray()

Convert between RGB format to intensity format. rgb2gray()

Convert between RGB format to indexed format. rgb2ind()


4: How to convert between double and uint8
When you store an image, you should store it as a uint8 image since this requires far less memory than
double. When you are processing an image (that is performing mathematical operations on an image)
you should convert it into a double. Converting back and forth between these classes is easy.
I=im2double(I);
converts an image named I from uint8 to double.
I=im2uint8(I);
converts an image named I from double to uint8.

5: MIRROR IMAGE GENERATION


The following program produces mirror image of the image passed to it and also displays both the
original and mirror image

Example 7: temp =a(i,k) result(i,k)

a=imread('pout.tif'); a(i,k) a(i,j)

[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)

ORIGINAL IMAGE MIRROR IMAGE

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.

Your output should be like the one given below


Task 2: Read an RGB image and separate it into red, green and blue channels. Display
the original image, the R, G, B channels images and then combine the three channels to
make a RGB image and show it.

You might also like