12 Lab Lapena
12 Lab Lapena
SECTION: 4ECE-A
RATING
EXPERIMENT No. 12
When it comes to images, filtering is a method of enhancing an image. For example, you can
emphasize certain features or remove some unnecessary.
Filtering is a neighbourhood operation which means that the value of any given pixel in the
output image is determined by applying some algorithm to the values of the pixels in the
neighbourhood of the corresponding input pixel.
A pixels neighbourhood is some set of pixels, defined by their locations relative to that pixel.
Linear filtering is filtering in which the value of an output pixel is a linear combination of the
values of the pixels in the input pixels neighbourhood.
Linear filtering of an image is accomplished through an operation called convolution.
In convolution, the value of an output pixel is computed as a weighted sum of neighbouring
pixels. The matrix of weights is called the convolution kernel, also known as the filter.
To load and display an image in MATLAB, we type the following sequence of commands:
>> x = imread(myimage)
directory
>> axis of
1.
To see the distribution of intensities of your image, you can create a histogram by
calling the imhist function. This works only for grayscale images. (If you are using a
colored image convert it to grayscale by using rgb2gray. Improve the contrast of the
image. One way is to call the histeq function to spread the intensity values over the
full range of the image, a process called histogram equalization.
>>myimage2 = histeq(myimage);
For GrayScale:
b=rgb2gray(a);
imshow(b);
For Histogram:
imhist(b);
2.
3.
If we type size(x), we see three numbers, the dimensions of the three dimensional
matrix. In particular,
x is a m x n x 3 matrix, the elements in x(:; :; 1) are the red intensities, x(:; :; 2) are the
green intensities,
and x(:; :; 3) are the blue intensities.
For example, to remove the blue intensities, we can do the following:
For No Blue Image:
noblue = a;
noblue(:,:,3) = zeros(size(a,1),size(a,2));
image(noblue);
The entries in x are unsigned 8-bit integers, whereas the operations for MATLAB are
mainly done with
doubles. Therefore, we need to convert to doubles before we manipulate the images:
>> dx = double(x)
>> y = uint8(dx)
>> image(y)
% convert to double
% convert to unsigned 8-bit integer
% same image as before
We can make the picture bluer (more blue) by multiplying the blue intensities with the
factor 1.5:
For Bluer Picture:
moreblue(:,:,1) = a(:,:,1);
moreblue(:,:,2) = a(:,:,2);
moreblue(:,:,3) = da(:,:,3)*1.5;
image(uint8(moreblue));
Assigning the red, green, and blue intensities to their average, we obtain a gray scale
picture:
For Color Intensity:
gc = (da(:,:,1) + da(:,:,2) + da(:,:,3))/3;
c(:,:,1) = gc;
c(:,:,2) = gc;
c(:,:,3) = gc;
image(uint8(c));
By selection of submatrices and the proper choice of axes, we can zoom the picture.
BLURRING AN IMAGE
We can encrypt a message by multiplying the red, green, and blue intensities with a
random matrix.
To decrypt we then multiply with the inverse of that random matrix. Assuming we loaded
the image into
x, we generate a blurring matrix as follows:
>> blur = randn(size(x,1)); % matrix to blur
>> key = inv(blur); % matrix to reconstruct image
For Blurring:
h = fspecial('gaussian',10,10);
a=imfilter(a,h);
imshow(a)
EXERCISE:
1. Perform the following codes using your own image. Also use other functions (such as
imadjust, im2bw etc.)to manipulate the image. Observe what happens. Include your
observation and images right after the codes.
2. Up to now you have probably only worked with variables of type double in Matlab. A
double is a real number. Images are often stored in diferent types. We will work with
the types double, logical, uint8 and uint16. It is easy to convert between types by
simply using the function of the type's name. For instance, to convert a matrix, A, of
doubles to logicals one would simply type: logical(A); Note that when converting
between types some information may be lost.
Type whos in your Matlab command line and press enter. The properties of all the
variables currently in your workspace will be displayed
This displays the intensity maps for the red, green and blue components as three
grayscale images next to each other in that order.
Change the order of the components in the original image and see what happens
when you display it.
Images can also be stored as uint16 - integer values between 0 and 65535, but this is
rarely done.
Convert the grayscale image to type double. Try to display it using the imshow
command. What happens?
When imshow is used to display an image of type double, it expects all values to be
between 0 and 1. All values higher than 1 are simply displayed as white pixels. How
would you change the image so all the values are between 0 and 1?
All the values between 0 and 1 change the image in black and white (BW =
im2bw)