Images in matlab
-
Upload
ali-alvi -
Category
Engineering
-
view
270 -
download
6
Transcript of Images in matlab
What is Image Data???
In Matlab, an image is a set of color or intensity
data.
Each image is formed by combination of pixels.
Each pixel is a matrix element.
Each pixel has its own intensity of red, blue and
green color.
A high level image is represented by a 3
dimensional array.
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.
Popular Matlab functions associated
with Images:
In Matlab, there are a lot of useful functions. Some
of these are:
imread
imshow
rgb2gray
imhist
imadjust
imb2w
And many more….
Loading an image in Matlab:
For loading an image in Matlab, we use use
“imread” function.
Following is an example of loading an image:
a=imread('skardu.jpeg');
Showing an image in Matlab:
“imshow” function shows the loaded image in
Matlab window.
Following is an example:
a=imread('skardu.jpg');
imshow(a);
Getting image info using Matlab:
We mainly have two types of images that we use
to deal in Matlab. These are
1- RGB images
2- Indexed images
RGB images are stored in 3 matrices. 1st for red,
2nd for green and 3rd for blue.
Each element in matrix stores intensity value of
red, green or blue color for a particular pixel.
In indexed images, each pixel is given an index.
Each index is mapped to RGB value in colormap.
Pixel info of RGB image:
For obtaining pixels info of an rgb image, we have
a built in function “impixelinfo”.
Following syntax is used:
a=imread('rgb.jpg');
imshow(a);
impixelinfo;
On the output screen, we can see the particular
row and column of pixel with its corresponding
rgb value.
We can also find the size of image by using given
command:
>> a=imread('rgb.jpg');
>> impixelinfo
>> size(a)
Output is:
ans =
768 1024 3
Output shows that it is a 768x1024 image with 3
planes.
Converting image from one format to
other:
Matlab can be used to change the format of image.
Following table gives information about image conversion:
As an example we will change format of image.
Converting image to grayscale
image:
“rgb2gray” function converts the image to
grayscale image.
Example is:
a=imread('skardu.jpg');
b=rgb2gray(a);
imshow(b);
Histogram of Image:
“imhist” function is used to obtain histogram of
image.
This function plots a graph that shows how many
pixels are in a specific intensity in grayscale
image.
An example is:
a=imread('skardu.jpg');
b=rgb2gray(a);
imhist(b)
Adjusting the contrast ratio: “imadjust” function adjusts the contrast ratio in image.
Example is:
a=imread('skardu.jpg');
b=rgb2gray(a);
c=imadjust(b);
subplot(1,2,1);
imshow(b);
title('Not Adjusted');
subplot(1,2,2);
imshow(c);
title('Adjusted');
Using Matlab to obtain black and
white image:
“im2bw” is a function in Matlab that turns the
image into black and white image.
Example:
a=imread('skardu.jpg');
b=im2bw(a);
imshow(b);
Resizing the image: Matlab can also be used to resize the image.
By resizing, we mean that we can increase or decrease pixels of image.
Following code is an example:
a=imread('skardu.jpg');
subplot(1,2,1);
imshow(a);
title('original image');
c=imresize(a,1/5);
subplot(1,2,2);
imshow(c);
title('Resized');
Performance issues in Matlab
images:
Matlab is very fast on vector and matrix
operations but
Correspondingly slow with loops.
So,
• Try to avoid loops
• Try to vectorize your code