2. Convert image to binary image, based on threshold
BW = im2bw(I, level) converts the grayscale image I to a binary image. The
output image BW replaces all pixels in the input image with luminance greater than
level with the value 1 (white) and replaces all other pixels with the value 0 (black).
>>b= im2bw(a,0.89)
>> imshow(b);
Convert an RGB image to a grayscale image
>>c=rgb2gray(a)
>> imshow(c);
Display histogram of image data
>> imhist(c)
3. Contrast enhancement
imadjust
Adjust image intensity values or colormap
Syntax: J = imadjust(I)
Creating the binary of an image using graythresh
Thresholding can be performed by using the graythresh
Global image threshold using Otsu's method
Syntax
level = graythresh(I)
level = graythresh(I) computes a global threshold (level)
that can be used to convert an intensity image to a binary image
with im2bw. level is a normalized intensity value that lies in the
range [0, 1].
I=rgb2gray(a)
level = graythresh(I);
BW = im2bw(I,level);
imshow(BW)
5. 0
100
200
300
400
500
600
700
800
900
1000
0 50 100 150 200 250
Spatial Transformations
To resize an image, use the imresize function. When you resize an image, you specify the image to be
resized and the magnification factor. To enlarge an image, specify a magnification factor greater than 1. To
reduce an image, specify a magnification factor between 0 and 1.
For example, the command below reduces the size of an image by 0.75 times.
>> I = rgb2gray(a); >> J = imresize(I,.75);
>> imshow(I) >>imshow(J)
6. You can specify the size of the output image by passing a vector that contains the number of rows and columns in
the output image.
If the specified size does not produce the same aspect ratio as the input image, the
output image will be distorted.
This example creates an output image with 100 rows and 150 columns.
>>J = imresize(I,[100 150]);
To use bilinear
>>Y = imresize(X,[100 150],'bilinear')
Using the imcrop function
imcrop is used to extract a rectangular portion of an image,
You can specify the crop region interactively using the mouse
>>J = imcrop(I);
or
You can specify the crop rectangle as a four-element position vector, [xmin
ymin width height] i.e, [60 40 100 90]
>>J = imcrop(I,[60 40 100 90]);
7. To rotate an image
Use the imrotate function. We specify rotation angle, in degrees.
If you specify a positive rotation angle, imrotate rotates the image counterclockwise;
if you specify a negative rotation angle, imrotate rotates the image clockwise.
To rotate the image 35° counterclockwise using bilinear interpolation
>>J = imrotate(I,35,'bilinear');
>>imshow(J)
imrotate uses nearest-neighbor
interpolation by default to determine the
value of pixels in the output image, but
you can specify other interpolation
methods.
To perfrom Translation to (40,40) from origin
>> xform = [ 1 0 0
0 1 0
40 40 1 ]
xform =
1 0 0
0 1 0
40 40 1
>> tform_translate =
maketform('affine',xform);
>> k= imtransform(I, tform_translate);
>>[cb_trans xdata ydata]= imtransform(I,
tform_translate);
>> cb_trans2 = imtransform(I,
tform_translate,...
'XData', [1 (size(I,2)+ xform(3,1))],...
'YData', [1 (size(I,1)+ xform(3,2))]);
>> imshow(cb_trans2)
Image Arithmetic
8. Creating the negative of an image using imcomplement
>>J=imcomplement(I)
>> imshow(J)
Imadd
Add two images or add constant to image
c=imresize(a,[300,300])
d=imresize(b,[300,300])
imshow(c)
K=imadd(c,d)
imshow(K)
OUTPUT:
9. imsubtract
Subtract one image from another or subtract constant from image
K=imsubtract(c,d)
imshow(K)
imdivide
Divides two images or divides image by constant
K=imdivide(c,d)
imshow(K)
immultiply
Multiply two images or multiply image by constant
K=immultiply(c,d)
imshow(K)
imabsdiff
absolute difference of two images
K=immultiply(c,d)
imshow(K)
To display an image in the background and another image on foreground.
>> b = imresize(a,[300 300]);
>> d = imresize(c,[300 300]);
>> e = imlincomb(.5,b,.5,d);
11. >> J = imnoise(I,'gaussian',0,0.025);
>> imshow(I)
>> imshow(J)
Output after applying Gaussian noise.
Remove the noise, using the wiener2 function.
K = wiener2(J,[5 5]);
imshow(K)
Removing noise using Median filter
K=medfilt2(J,[3 3])
imshow(K)
12. Removing noise using Gaussian filter
1. Add Salt and pepper noise
J=imnoise(I,'salt & pepper',.02)
imshow(J)
2. Creating Gaussian filter
h = fspecial(type,parameters) creates a two-
dimensional filter h of the specified type. fspecial
returns h as a correlation kernel, which is the
appropriate form to use with imfilter. type is a string
having one of these values.
h=fspecial('gaussian',[3 3],.7)
h =
0.0113 0.0838 0.0113
0.0838 0.6193 0.0838
0.0113 0.0838 0.0113
3. Filter noisy image with Gaussian filter
L=imfilter(J,h)
imshow(L)
Removing noise using 2 dimensionl order-statistics filter
L=ordfilt2(J,5,ones(3,3))
imshow(L)
13.
14. -1
-0.5
0
0.5
1
-1
0
1
0
0.5
1
Fx
F
y
Magnitude
Removing noise using Averaging filter
1.Create kernel for average filter with low pass characterisitics
k=[ 1 1 1
1 1 1
1 1 1]/9
k =
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
2. Plot frequency response characteristics
freqz2(k)
3.Apply averaging filter
Filter M=imfilter(J,k)
imshow(M)
15. Find the edges of an image using the Prewitt , Sobel and Canny methods.
BW1 = edge(I,'prewitt');
BW2 = edge(I,'canny');
Imshow(BW2)
BW2 = edge(I,'sobel');
Imshow(BW2)
Visit my blog:
enthusiaststudent.blogspot.in