0% found this document useful (0 votes)
63 views61 pages

DAY Wise Programs

The document contains code snippets demonstrating various image processing techniques in MATLAB, including: 1. Reading, displaying, and manipulating images through functions like imread, imshow, and arithmetic operators. 2. Converting between color spaces and image types (e.g. RGB to grayscale). 3. Applying filters and transforms like Fourier, median, and blurring filters. 4. Simulating noise and restoration with functions such as imnoise and deconvlucy. 5. Compressing images using the discrete cosine transform and block processing. The code shows how to perform common image analysis and enhancement tasks programmatically in MATLAB.

Uploaded by

Rasool Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views61 pages

DAY Wise Programs

The document contains code snippets demonstrating various image processing techniques in MATLAB, including: 1. Reading, displaying, and manipulating images through functions like imread, imshow, and arithmetic operators. 2. Converting between color spaces and image types (e.g. RGB to grayscale). 3. Applying filters and transforms like Fourier, median, and blurring filters. 4. Simulating noise and restoration with functions such as imnoise and deconvlucy. 5. Compressing images using the discrete cosine transform and block processing. The code shows how to perform common image analysis and enhancement tasks programmatically in MATLAB.

Uploaded by

Rasool Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 61

%%%%%%%% -------------DAY 1---------%%%%%%%%

%%%%%%Some basic programs on images%%%%%%

>> zeros(3)
ans =
0

>> ones(3)
ans =
1

>> eye(3)
ans =
1

>> ones(3)*2
ans =
2

>> a = [1 2 3]
a=
1

>> b = [1,2,3]

b=
1

>> c = [1;2;3]
c=
1
2
3

>> d = [1 2;3 4]
d=
1

>> e = [1 2 3;4 5 6;7 8 9]


e=
1

>> magic(3)
ans =
8

>> pascal(3)
ans =
1

>> rand(3,4)
ans =
0.9501

0.4860

0.4565

0.4447

0.2311 0.8913

0.0185

0.6154

0.6068

0.8214

0.7919

0.7621

>> randint(3)
ans =
1

>> randn(3,4)%%Displays +ve and -ve values


ans =
-0.4326

0.2877

1.1892

0.1746

-1.6656 -1.1465 -0.0376 -0.1867


0.1253

1.1909

0.3273

>> A = [1 2 3;4 5 6; 7 8 9]
A=
1

>> B = size(A)

0.7258

B=
3

>> C = length(A)
C=
3

>> A = [11 12 13;14 15 16;17 18 19]


A=
11 12

13

14

15

16

17

18

19

>> B = sum(A)
B=
42

45

48

>> C = sum(B)
C=

135

>> D = diag(A)
D=
11
15
19

>> E = diag(A, -1)


E=
14
18

>> F = det(A)
F=
0

>> G = norm(A)
G=
45.6601
>> H = inv(A)
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.387779e-017.
H=
1.0e+014 *
1.8765 -3.7530
-3.7530

7.5060 -3.7530

1.8765 -3.7530

>> J = A'
J=
11 14

17

12

15

18

13

16

19

>> K = A/2
K=

1.8765

1.8765

5.5000

6.0000

6.5000

7.0000

7.5000

8.0000

8.5000

9.0000

9.5000

>> L = mod(A,4)
L=
3

%%%%%%%%%Matrix Indexing%%%%%%%%%

>> A = [1 2 3;4 5 6; 7 8 9]
A=
1

>> A(3, 2)

ans =
8
>> A(end,end-2)
ans =
7

>> A(1,:)
ans =
1

>> A(2,:)
ans =
4

>> A(:,1)
ans =
1
4
7

>> A(:,3)
ans =
3
6
9

>> A(:,:,:)
ans =
1

>> A(:,2,:)
ans =
2
5
8
>> A(1,:,:)
ans =

>> A(1,2,:)
ans =
2
>> A(0+[1 2],1+[1 2])
ans =
2

%%%%%%%-------- Arithmetic Operators---------%%%%%%%%%%


MATLAB provides these arithmetic operators.

Day 2

%%%%%%%% -------------DAY 2---------%%%%%%%%

%%%%%%%%%------Image Processing------%%%%%%%%%%%

% How to read an image


A=imread (filename);

% To display the image


imshow (A);
imview(A);
X=imread(Path);
imshow(X);

X=imread(Path);
imshow(X);

% Add a constant to an image.


I = imread('rice.png');
J = imadd(I,50);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)

OUTPUT:

% Subtract a constant value from an image:


I = imread('rice.png');
Iq = imsubtract(I,50);
imview(I),imview(Iq)
OUTPUT:

% Scale an image by a constant factor:


I = imread('moon.tif');
J = immultiply(I,0.5);

subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)
OUTPUT:

% Divide by a constant of the rice image.


I = imread('rice.png');
J = imdivide(I,2);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)

OUTPUT:

% Adjust a low-contrast grayscale image.


I = imread('pout.tif');
J = imadjust(I);
imview(I), imview(J)
OUTPUT:

% A low-contrast image with its histogram.


I = imread('pout.tif');
imshow(I)
figure, imhist(I,64)

OUTPUT:

5000

4000

3000

2000

1000

0
0

50

100

150

200

%Entropy
%Entropy is used to characterize the texture of the input image.
I = imread('circuit.tif');
J = entropy(I)
OUTPUT:
J = 6.9439

%A high-contrast image with its histogram.


I = imread('cameraman.tif');
figure; imshow(I); imhist(I,64);
OUTPUT:

250

4000
3500
3000
2500
2000
1500
1000
500
0
0

50

100

150

200

250

%Add two images together and specifies an output class.


I = imread('rice.png');
J = imread('cameraman.tif');
K = imadd(I,J,'uint16');
imshow(K,[])

OUTPUT:

% To display the r, g, b components of an color image


a=imread('football.jpg');
subplot(2,2,1),imshow(a);title('rgb image');

b=a(:,:,1);
subplot(2,2,2),imshow(b);title('r component image');
c=a(:,:,2);
subplot (2,2,3),imshow(c);title('g component image');
d=a(:,:,3);
subplot(2,2,4),imshow(d);title('b component image');
rgb image

r component image

g component image

b component image

2)
a=imread('peppers.png');
subplot(2,2,1),imshow(a);title('rgb image');

b=rgb2gray(a);

subplot(2,2,2),imshow(b);title('gray scale image ');

c=rgb2ind(a,234);%tolerance value between (0 to 255)


subplot(2,2,3),imshow(c);title('indexed image');

d=im2bw(a);
subplot(2,2,4),imshow(d);title('binary image');

e=rgb2ntsc(a);figure,
subplot(2,2,1),imshow(e);title('national television systems commitee
image');

rgb image

gray scale image

indexed image

binary image

% To convert an indexed image to the gray scale image


[a,map]=imread('trees.tif');
imshow(a,map);

f=ind2gray(a,map);figure,
subplot(2,2,2),imshow(f);title('gray scale image');
original image

a=imread('peppers.png');
subplot(2,2,1),imshow(a);title('rgb image');

b=rgb2ycbcr(a);
subplot(2,2,2),imshow(b);title('ycbcr image ');

c= ycbcr2rgb(b);
subplot(2,2,3),imshow(c);title('rgb image');

gray scale image

rgb image

ycbcr image

indexed image

% To rotate an image 35 counterclockwise and use bilinear


interpolation.
I = imread('circuit.tif');
J = imrotate(I,35,'bilinear');
imshow(I)
figure, imshow(J)

OUTPUT:

% Crop an image
clc;
close all;
clear all;
I = imread('circuit.tif');
I2 = imcrop(I,[75 68 130 112]);
imview(I), imview(I2)

OUTPUT:

% To increases the size of the image


I = imread('circuit.tif');
J = imresize(I,1.25);
imshow(I)
figure, imshow(J)
OUTPUT:

IMAGE TRANSFORMS
Image transforms

Fourier transform

a=imread('peppers.png');
subplot(2,2,1),imshow(a);title('original image');
b=fft(a);
subplot(2,2,2),imshow(b);title('transformed image');
c=ifft(b);
subplot(2,2,3),imshow(c,[]);title('restored image');
original image

transformed image

restored image

% Create geometric transformation structure


I = imread('cameraman.tif');

tform = maketform('affine',[1 0 0; .5 1 0; 0 0 1]);


J = imtransform(I,tform);
imshow(I), figure, imshow(J)
OUTPUT:

DAY 3

%%%%%%%% -------------DAY 3---------%%%%%%%%


Image enhancement

Image enhancement

% Add gaussian noise to an image


I = imread('eight.tif');
J = imnoise(I, 'gaussian');

imshow(I);
figure, imshow(J);
OUTPUT:

% Add salt & pepper noise to an image


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

% Filter the noisy image with an averaging filter and display the
results.
I = imread('eight.tif');
imshow(I)
J = imnoise(I,'salt & pepper',0.02);
figure, imshow(J)
K = filter2(fspecial('average',3),J)/255;
figure, imshow(K)

OUTPUT:

% Median filters to filter the noisy image and display the results.
I = imread('eight.tif');
imshow(I)
J = imnoise(I,'salt & pepper',0.02);
figure, imshow(J)
L = medfilt2(J,[3 3]);
figure, imshow(L)
OUTPUT:

% Filtering of images, either by correlation or convolution


I = imread('coins.png');
h = ones(5,5) / 25;
I2 = imfilter(I,h);
imshow(I), title('Original Image');
figure, imshow(I2), title('Filtered Image')

OUTPUT:

Original Image

Filtered Image

% Blurred image.
I = imread('cameraman.tif');
subplot(2,2,1);
imshow(I); title('Original Image');

H = fspecial('motion',20,45);
MotinBlur = imfilter(I,H,'replicate');
subplot(2,2,2);
imshow(MotionBlur); title('Motion Blurred Image');

H = fspecial('disk',10);
blurred = imfilter(I,H,'replicate');
subplot(2,2,3);
imshow(blurred); title('Blurred Image');

H = fspecial('Unsharp');
sharpeneded = imfilter(I,H,'replicate');

subplot(2,2,4);
imshow(sharpened); title('Sharpened Image');
OUTPUT:
Original Image

Motion Blurred Image

Blurred Image

Sharpened Image

DAY 4
Image
restoration

%To blur an image using a function psf.

I = imread('peppers.png');
I = I(10+[1:256],222+[1:256],:);
figure;imshow(I);title('Original Image');
LEN = 31;
THETA = 11;
PSF = fspecial('motion',LEN,THETA);
Blurred = imfilter(I,PSF,'circular','conv');
figure; imshow(Blurred);title('Blurred Image');

OUTPUT:
Original Image

%Deblurring with the Wiener Filter:


I = imread('peppers.png');

Blurred Image

I = I(10+[1:256],222+[1:256],:);
figure;imshow(I);title('Original Image');
LEN = 31;
THETA = 11;
PSF = fspecial('motion',LEN,THETA);
Blurred = imfilter(I,PSF,'circular','conv');
figure; imshow(Blurred);title('Blurred Image');
wnr1 = deconvwnr(Blurred,PSF);
figure;imshow(wnr1);
title('Restored, True PSF');

OUTPUT:
Original Image

Blurred Image

Restored, True PSF

% Deblurring with a Regularized Filter


I = imread('tissue.png');
I = I(125+[1:256],1:256,:);
figure; imshow(I); title('Original Image');
PSF = fspecial('gaussian',11,5);
Blurred = imfilter(I,PSF,'conv');
V = .02;
BlurredNoisy = imnoise(Blurred,'gaussian',0,V);
figure;imshow(BlurredNoisy);title('Blurred and Noisy Image');
NP = V*prod(size(I));
[reg1 LAGRA] = deconvreg(BlurredNoisy,PSF,NP);
figure,imshow(reg1),title('Restored Image');

Original Image

Blurred and Noisy Image

Restored Image

Restoration algorithms
% deblurring with the lucy richardson filter
I = imread('football.jpg');
subplot(2,2,1),imshow(I);title('Original Image');
PSF = fspecial('gaussian',5,5);
% Create a simulated blur in the image and add noise.
Blurred = imfilter(I,PSF,'circular','conv');
V = .02;
BlurredNoisy = imnoise(Blurred,'gaussian',0,V);
subplot(2,2,2),imshow(BlurredNoisy);title('Blurred and Noisy ');
luc1 = deconvlucy(BlurredNoisy,PSF,5);
subplot(2,2,3); imshow(luc1);
title('Restored Image');

Original Image

Restored Image

Blurred and Noisy

DAY 5
COMPRESSIO
N
%%%%%%%%%%%%%%%%day 5 %%%%%%%%%%
Compression

a = im2double(imread('cameraman.tif'));
imshow(a);title('original image');
v = dctmtx(size(a,1));
dct = v*a*v';

figure, imshow(dct);title('dct image');


original image

dct image

I = imread('coins.png');
imshow(I);title('original image');
I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
imshow(I), figure, imshow(I2);title('dct compressed image');

OUTPUT:

dct compressed image

% Un sharp contrast enhancement filter


I = imread('cameraman.tif');
subplot(2,2,1);
imshow(I); title('Original Image');
H = fspecial('unsharp');
sharpened = imfilter(I,H,'replicate');
subplot(2,2,4);
imshow(sharpened); title('Sharpened Image');
OUTPUT:

Wavelets

a=imread('cameraman.tif');
subplot(3,2,1),imshow(a);title('original image');
[ca,ch,cv,cd]=dwt2(a,'haar');
subplot(3,2,2),imshow(mat2gray(ca));title('approximation coefficents
image');
subplot(3,2,3),imshow(ch);title('horizontal coefficients image');
subplot(3,2,4),imshow(cv);title('vertical coefficients image');
subplot(3,2,5),imshow(cd);title('diagnol coefficients image');
c=idwt2(ca,ch,cv,cd,'haar');
subplot(3,2,6),imshow(mat2gray(c));title('reconstructed image');

original image

approximation coefficents image

horizontal coefficients image

vertical coefficients image

diagnol coefficients image

reconstructed image

DAY 6

%%%%%%%%%%day 6 %%%%%%%%%%

segmentation
% Edge-detection method that edge provides is the Sobel and Canny
method
I = imread('coins.png');
imshow(I)
BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
imshow(BW1);
figure, imshow(BW2);
OUTPUT:

% edge-detection method that edge provides is the prewitt and


Canny method
z= imread('cameraman.tif');
BW1 = edge(z,'prewitt');

BW2 = edge(z,'canny');
imshow(BW1);title('prewitt image');
figure, imshow(BW2);title('canny image');
OUTPUT:
prewitt image

Segmentation
% applying the watershed transform
a=imread('football.jpg');
imshow(a);title('original image');
b=watershed(a);
figure,imshow(b);title('transformed image');

canny image

original image

transformed image

%bwmorph means Morphological operations on binary images


% Morphological operations are create connectivity array,
% reconstruction morphologically etc.

%remove-removes interior pixels.


BW = imread('circles.png');
imshow(BW);
BW2 = bwmorph(BW,'remove');
figure, imshow(BW2);
OUTPUT:

%SKELETONIZATION
%To reduce all objects in an image to lines, without changing the essential
structure of the image is known as skeletonization.
BW1 = imread('circbw.tif');
BW2 = bwmorph(BW1,'skel',Inf);
imshow(BW1);
figure, imshow(BW2)
OUTPUT:

%Matrix to grayscale image convertion


%filter2- 2-D digital filter
%fspecial- Create predefined 2-D filter
%mat2gray- Convert matrix to grayscale image
I = imread('rice.png');
J = filter2(fspecial('sobel'),I);
K = mat2gray(J);
imshow(I), figure, imshow(K)
OUTPUT:

%Reverse black and white in a binary image.


%imcomplement- Complement image
bw = imread('text.png');
bw2 = imcomplement(bw);
subplot(1,2,1),imshow(bw)
subplot(1,2,2),imshow(bw2)
OUTPUT:

% To plot the border on the image.


I=imread(cameraman.tif);
imshow(I)
BW = im2bw(I);
imshow(BW)
dim = size(BW)
col = round(dim(2)/2)-90;
row = min(find(BW(:,col)))

boundary = bwtraceboundary(BW,[row, col],'N');


imshow(I)
hold on;
plot(boundary(:,2),boundary(:,1),'g','LineWidth',10);
OUTPUT:

%To rotate an image 35 counterclockwise and use bilinear interpolation.


I = imread('circuit.tif');
J = imrotate(I,35,'bilinear');
imshow(I)
figure, imshow(J)
OUTPUT:

%creating a mask to an image.


I = imread('cameraman.tif');
BW = imread('text.png');
mask = BW(1:256,1:256);
f = inline('imadjust(x,[],[],0.3)');
I2 = roifilt2(I,mask,f);
imshow (I2)

OUTPUT:

%Edgetapering
original = imread('cameraman.tif');
PSF = fspecial('gaussian',60,10);
edgesTapered = edgetaper(original,PSF);
figure, imshow(original,[]);
figure, imshow(edgesTapered,[]);
OUTPUT:

You might also like