Tuesday, March 15, 2016

How to read and display Gray-scale and Color images in MATLAB ?

To Read an Image:

imread() : imread function reads the pixel values from an image file and returns a matrix of all the pixel values. This function returned the image data in the variable w, which is a 291 x 240 element array of uint8.
>>w=imread(‘cameraman.tif’);

To Display an Image:
imshow(): To show our image, use imshow() or imagesc() command. The imshow() command shows an image in standard 8-bit format, like it would appear in a web browser. The imagesc() command displays the image on scaled axes with the min value as black and the max value as white.
>>h=imshow(w);


We can check the pixel values at x and y coordinates of a pixel as
  1. Select "Data Cursor" icon from the top menu
  2. Click the mouse on the image as in the Fig 2.
Note: R G B values displayed in the figure have the same value as it is a gray scale image. The index will give the intensity value.


Imfinfo: Image Information gives the information of an image.
>>info=imfinfo(‘cameraman.tif’);







Impixelinfo: Querying a pixel info

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 (200, 200):
Type the command impixelinfo into the command Window, and move the mouse on the image. Then, we'll see coordinates and gray value being displayed while the mouse is on the picture.


RGB Images:
>> a=imread('autumn.tif');
>>size(a);
>>206 345 3
It is 206 x 348 matrix with 3 RGB channels
a(:,:,1) will give the matrix for R, a(:,:,2) will give the matrix for green, a(:,:,3) will give the matrix for blue.

The imagesc() command displays the image on scaled axes with the min value as black and the max value as white. Display the image using imshow() and imagesc(). Check the RBG values with (x,y) coordinates of a pixel using data cursor icon from the top menu.

Display of Color Images :

Image using imshow()

Image using imagesc()
A useful function for obtaining RGB values or grayscale values is impixel and the function with  three output arguments, returns the coordinates of the selected pixels.
Actually, a color image is a combined image of 3 grayscale images. So, we can display the individual RGB components of the image using the following script:
RGB = imread('peppers.png');
subplot(221);
imagesc(RGB);
title('Original');
subplot(222);
imagesc(RGB(:,:,1));
title('Red');
subplot(223);
imagesc(RGB(:,:,2));
title('Green');
subplot(224);
imagesc(RGB(:,:,3));
title('Blue');

Displaying RGB components of Image:
Output of above code

Output of below code
The following code will display the image in red, green and blue.

rgb = imread('peppers.png');
r = rgb;
r(:,:,2:3) = 0; % The red component without the other components
g = rgb;
g(:,:,1:2:3) = 0; % The green component without the other components
b = rgb;
b(:,:,1:2) = 0; % The blue component without the other components
subplot(221);
imshow(rgb);
title('Original');
subplot(222);
imshow(r);
title('Red');
subplot(223);
imshow(g);
title('Green');
subplot(224);
imshow(b);
title('Blue');
Let's make a new image that has more blue by quadrupling the component. The standard image format is uint8 (8-bit integer), which may not like arithmetic operation and could give us errors if we try mathematical operations. So, we do image processing by casting our image matrix to double format before we do our math. Then we cast back to uint8 format at the end, before displaying the image.

RGB = imread('peppers.png');
imshow(RGB);
title('Normal RGB');
blue_RGB=double(RGB);
blue_RGB(:,:,3)=4*blue_RGB(:,:,3);
blue_RGB(:,:,3)=4*blue_RGB(:,:,3);
figure,imshow(uint8(blue_RGB));
title('Modified blue component');




Writing in files
An image matrix may be written to an image file with imwrite() function. Its general form is
Imwrite(X,map,’filename’,’fmt’) which writes the image stored in matrix X with color map map to file filename with format fmt.

a=imread('autumn.tif');
imwrite(a,'D:\test\autumn.png','png');
% Mention the path and write in the folder you have created

Exercise:

Pick the grayscale image cameraman.tif. Using imwrite write it to files of type JPEG, PNG, BMP format. What are the sizes of those files?

img = imread('cameraman.tif');
imwrite(img,'cameraman.jpg','jpg');                        % Store it in JPEG format
imwrite(img,'cameraman.png','png');                     % Store it in PNG format
imwrite(img,'cameraman.bmp','bmp');                   % Store it in JPEG format


Results:
Size of tif image: 63.7KB
Size of JPEG image: 10.4KB
Size of PNG image: 37.3KB
Size of BMP image: 65KB


RESULTS & CONCLUSIONS

The usage of imread(), imshow() and imfinfo() was learnt and they were implemented on MATLAB
Different channels of an RGB image were extracted and displayed. Each channel of an image was displayed by making all the other components 0.
The intensity of individual channels was varied and displayed
Indexing of images was studied to display an image as a colormap matrix
The usage of imwrite() was studied. It was seen that JPEG file type consumes the least size when compare to all other file types

No comments:

Post a Comment