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

Image Processing Lab Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 19

Image Processing

Lab manual

Week1 matlab basics

Week2 matlab basics

Week3 reading and writing image as a matrix

Week4 plotting images

Week5 adding noise and removing noise using filters

Week6 image analysis(histogram)

Week7 Image edge detection ( canny, sobel , ...)

Week8 transformation like (discrete wavelet transform)


Lab#1
Mat lab Environment
The graphical interface to the MATLAB workspace

Creating MATLAB variables


variable name = a value (or an expression)

Overwriting variable
>> t = 5;
>> t = t+1
t =6
Error messages
>> x = 10;
>> 5x
??? 5x
|
.Error: Unexpected MATLAB expression

Managing the workspace


clear >>
who >>
clear all >>
Entering multiple statements per line

>> a=7; b=cos(a), c=cosh(a)


b =0.6570
c =548.3170

Getting help
>> help Command
>> lookfor inverse

Mathematical functions

Basic plotting
Example

>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
Adding titles, axis labels, and annotations

Example

>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
>> xlabel('x = 0:2\pi')
>> ylabel('Sine of x')
>> title('Plot of the Sine function')
Notes:
0:pi/100:2*pi yields a vector that:
 starts at 0,
 takes steps (or increments)
of ¼=100,
 stops when 2¼ is reached.

Multiple data sets in one plot


Example
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,'--',x,y2,'-',x,y3,':')
>> xlabel('0 \leq x \leq 2\pi')
>> ylabel('Cosine functions')
>>
legend('2*cos(x)','cos(x)','0.5*cos(x)')
>> title('Typical example of multiple
plots')
>> axis([0 2*pi -3 3])

Specifying line styles and colors

plot(x,y,'style_color_marker')
Lab#2

Matrix generation
Entering a vector
>> v = [1 4 7 10 13]
v=
1 4 7 10 13

All elements from the third through the last elements,


>> v (3,end)
ans =
7 10 13
Entering a matrix

>> A (2,1)
ans =
4
Creating a sub-matrix
Lab#3
Basic functions in matlab
Imread()
img = imread('ImageProcessing_1/BerkeleyTower.png');
>> size(img)

imshow()
To show our image, we the imshow() or imagesc() command. Theimshow() 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.

Using imshow(): Using imagesc():

We can check the RGB values with (x,y) coordinates of a pixel:

1. Select "Data Cursor" icon from the top menu


2. Click the mouse on the image
Notice each pixel is a 3-dimensional vector with values in the range [0,255]. The 3
numbers displayed is the amount of RGB.

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:
subplot(131);
imagesc(img(:,:,1));
title('Red');

subplot(132);
imagesc(img(:,:,2));
title('Green');

subplot(133);
imagesc(img(:,:,3));
title('Blue');

We we issue a command colormap gray, the picture turns into a grayscale:


>> colormap gray

Want to play more? Then, 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
may want to 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.

subplot(211);
imshow(img);
title('Normal RGB');

subplot(212);
blue_img = double(img);
blue_img(:,:,3) = 4*blue_img(:,:,3);
blue_img = uint8(blue_img);
imshow(blue_img);
title('RG 4*B');

 
Lab#4
imwrite()
To save your new blue image, use the imwrite() command:

imwrite(blue_img, 'Blue4_BerkeleyTower.png', 'png');

rgb2gray()
img = imread('ImageProcessing_1/BerkeleyTower.png');
gray = rgb2gray(img);
imshow(gray);

>> size(gray)
ans =
499 748

Now, we have only one value for each pixel not 3 values. In other words,
the rgb2gray() converted RGB images to grayscale by eliminating the hue and saturation
information while retaining the luminance.
Adding noise to image

Description
J = imnoise(I,type) adds noise of a given type to the intensity image I. type is a
string that specifies any of the following types of noise. Note that certain types of noise
support additional parameters. See the related syntax.

Example
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
figure, imshow(I)
figure, imshow(J)

Lab Assignment
Add all types of noise of cameraman picture
Lab#5
Removing noise from image

Remove Salt and Pepper Noise from Image


Read image into workspace and display it.
Example

I = imread('eight.tif');
figure, imshow(I)

Add salt and pepper noise.


J = imnoise(I,'salt & pepper',0.02);

Use a median filter to filter out the noise.


K = medfilt2(J);

Display results, side-by-side.


imshowpair(J,K,'montage')

Lab Assignment
Try to use three other filters to remove noise from image

Lab#6
Image Enhancement

Image enhancement algorithms include deblurring, filtering, and contrast methods.


For more information, 

Here are some useful examples and methods of image enhancement:


 Filtering with morphological operators
 Histogram equalization
 Noise removal using a Wiener filter
 Linear contrast adjustment
 Median filtering
 Unsharp mask filtering
 Contrast-limited adaptive histogram equalization (CLAHE)
 Decorrelation stretch
The following images illustrate a few of these examples :

Correcting non uniform illumination with morphological operators.


Enhancing grayscale images with histogram equalization.

Deblurring images using a Wiener filter.

Enhance Grayscale Images


Using the default settings, compare the effectiveness of the following three techniques:

 imadjust increases the contrast of the image by mapping the values of the input intensity
image to new values such that, by default, 1% of the data is saturated at low and high intensities of the
input data.
 histeq performs histogram equalization. It enhances the contrast of images by transforming
the values in an intensity image so that the histogram of the output image approximately matches a
specified histogram (uniform distribution by default).
 adapthisteq performs contrast-limited adaptive histogram equalization. Unlike histeq, it
operates on small data regions (tiles) rather than the entire image. Each tile's contrast is enhanced so
that the histogram of each output region approximately matches the specified histogram (uniform
distribution by default). The contrast enhancement can be limited in order to avoid amplifying the noise
which might be present in the image.
 pout_imadjust = imadjust(pout);
 pout_histeq = histeq(pout);
 pout_adapthisteq = adapthisteq(pout);

 imshow(pout);
 title('Original');

 figure, imshow(pout_imadjust);
 title('Imadjust');
Lab Assignment
Lab#7
Image Edge Detection
Compare Edge Detection Using Canny and Prewitt Methods
Read a grayscale image into the workspace and display it.
I = imread('circuit.tif');
imshow(I)

Find edges using the Canny method.


BW1 = edge(I,'Canny');

Find edges using the Prewitt method.


BW2 = edge(I,'Prewitt');

Display both results side-by-side.


imshowpair(BW1,BW2,'montage')
Lab#8
Transformation Method (discrete wavelet transform)
image = imread(imagePath);
wavename = 'haar';
[cA,cH,cV,cD] = dwt2(im2double(iamge),wavename);
imshow([cA,cH; cV,cD],'Colormap',gray);

You might also like