Wednesday, November 16, 2011

Displaying an image, class, pixel values etc.

Reading the images


A=imread('image_name');
% please modify the image_name to the appropriate name of the  image you are processing.


Images are stored as a matrix in Matlab.
Thus the image is now stored in the matrix A.

Unsigned integers


class(A) % check the class of the variable A.
It says uint8 i.e. unsigned integer with 8 bits.
uint8 is nice but note it is unsigned and integer, which might not be what you like

I like to convert the image to class double, that can store signed and non-integer values.
A=double(A);

What is the size of the image?


[M, N]=size(A) % Gives the size of the image stored in A
[M N]=size(A) % The comma isn't needed between M and N
M is the number of rows and N is the number of columns.

Accessing pixel values


The value A(i, j) gives the value of a particular pixel stored at the row number i and column number j.
A(20, 30) % displays the value of the pixel at the location (20, 30)

This could be little tricky sometimes, especially if one is used to think about (20, 30) as the x and y coordinates.
They are not! In Matlab there is no such thing as the point (0, 0). You see Matlab is MATrix LABoratory. So, everything is stored as matrices, and there is no such thing as a zeroth column or a row. I wish Matlab could handle things like A(0, 0), life would have been simple. But it does not. So we get used to it.


When I want to think about the x and y axis, I think of the origin at the Top-Left corner and the x-axis is going downwards and y is going to the right. But I guess, different peole have different way of thinking about it.




Displaying images


figure;
imshow(A); % imshow is my favourite way of displaying images.
title('Original image')

figure;
imshow(A, [0 255]); 
% [0 255] specifies the range of the grayscale. 255 is the maximum for an 8 bit image,
% For and 8 bit image one can have 2^8 = 256 levels. Thus, you have zero to 255 intensity levels.
title('Original image with [0, 255]')

figure;
imagesc(A);
colormap('Gray');
title('Original image with imagesc');

Help!

Matlab is just like Hogwarts School of Witchcraft and Wizardry.
Like in Hogwarts, help will always be given in Matlab to those who ask for it.
Just type the following in the command window to get help about the command imshow.

help imshow
doc imshow 
% This will open up an HTML document about the command imshow

No comments:

Post a Comment