Professional Documents
Culture Documents
IMAGE ENHANCEMENT :
1.CONVOLUTION OPERATION INVOLVING IMAGES:
clc;
clear all:
close all;
I = imread('cameraman.tif');
h = [1 0 1; 1 0 1; 1 0 1];
y=conv2(I,h);
figure,subplot(121),imshow(I,'notruesize'),title('original'):
subplot(122),imshow((unit8(y),notruesize),title(image after convolution);
2. MEDIAN FILTERING:
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.03);
K = medfilt2(J);
figure,subplot(311),imshow(I),title('original')
subplot(312),imshow(J),title('original mixed with salt and pepper noise'),
subplot(313),imshow(K),title('median filtered image')
3. IMAGE FILTERING:
clc; clear all; close all;
I = imread('cameraman.tif');
h1=[1 1 1; 1 1 1; 1 1 1]/9;
y1=imfilter(I,h1);
figure, subplot(121),imshow(I,'notruesize'),
title('original');
subplot(122), imshow(uint8(y1),'notruesize'),
title('Image after filtering');
4. CONTRAST ENHANCEMENT OF AN IMAGE:
I = imread('cameraman.tif');
J = histeq(I);
figure,subplot(121),imshow(I)
subplot(122),imshow(J)
5. PROGRAM USING IMADJUST
I = imread('cameraman.tif');
J = imadjust(I); figure, subplot(121),imshow(I)
Workshop on Image and Video Processing- A Practical Approach
7
CB
( : , : , 2)
Y
(:,:,1)
CR
(: , : , 3 )
1. Call function
jpgacbits
2. dcbitsy
2. acbitsy
+
Total = dcbitsy + acbitsy
Function Jpgdcbits
1. Find the level / run length using the
formula Round (log 2 (abs(dc)) + 0.5) + 1)
Bits returned
Function Jpgacbits
0. Arrange the remaining 63 AC
coefficients in the Zig Zag pattern by
calling the function ZigZag
Function ZigZag
Convert 2D ac
coefficients into 1D array
Return
1. Find the level / run length using the
formula Round (log 2 (abs(dc)) + 0.5) + 1)
Bits returned
if k==64
Bits=Bits+double(RLC{1}(1));
break;
end
else
if count==0
RL=count;
level=round(log2(abs(X1(k)))+0.5);
Bits=Bits+double(RLC{RL+1}(level+1));
elseif count>=1 && count<=15
RL=count;
level=round(log2(abs(X1(k)))+0.5);
Bits=Bits+double(RLC{RL+1}(level));
count=0;
else
Bits=Bits+double(RLC{1}(1));
count=count-16;
end
end
k=k+1;
end
case 'C'
codelength=codec;
end
if dc==0
Workshop on Image and Video Processing- A Practical Approach
23
Program 1
%This program illustrates false contouring
clc
clear all
close all
a=imread('tigerpub.jpg');
imshow(a),title('original image')
%using 128 gray levels
figure,imshow(grayslice(a,128),gray(128)),
title('Image with 128 gray level')
%using 64 gray levels
figure,imshow(grayslice(a,64),gray(64)),
title('Image with 64 gray level')
%using 32 gray levels
figure,imshow(grayslice(a,32),gray(32)),
title('Image with 32 gray level')
%using 16 gray levels
figure,imshow(grayslice(a,16),gray(16)),
title('Image with 16 gray level')
%using 8 gray levels
figure,imshow(grayslice(a,8),gray(8)),
title('Image with 8 gray level')
Program 2
% Generating Walsh Basis function
clear all;
close all;
clc;
n=input('Enter the basis matrix dimension: ');
m=n;
for u=0:n-1
for v=0:n-1
for x=0:n-1
for y=0:n-1
powervalue=1;
sn=log2(n);
for i=0:sn-1
a=dec2bin(x,sn);
b=bin2dec(a(sn-i));
c=dec2bin(y,sn);
d=bin2dec(c(sn-i));
e=dec2bin(u,sn);
f=bin2dec(e(i+1));
e=dec2bin(v,sn);
a=bin2dec(e(i+1));
powervalue=powervalue*(-1)^(b*f+d*a);
end
basis{u+1,v+1}(x+1,y+1)=powervalue;
end
end
Program 3
%Averaging filter
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\rose.jpg');
% Addition of noise to the input image
b=imnoise(a,'salt & pepper');
c=imnoise(a,'gaussian');
d=imnoise(a,'speckle');
% Defining 3x3 and 5x5 kernel
h1=1/9*ones(3,3);
h2=1/25*ones(5,5);
% Attempt to recover the image
b1=conv2(b,h1,'same');
b2=conv2(b,h2,'same');
c1=conv2(c,h1,'same');
c2=conv2(c,h2,'same');
d1=conv2(d,h1,'same');
d2=conv2(d,h2,'same');
%Displaying the result
figure,subplot(2,2,1),imshow(a),title('Original Image'),
subplot(2,2,2),imshow(b),title('Salt & Pepper noise'),
subplot(2,2,3),imshow(uint8(b1)),title('3 x 3 Averaging filter'),
subplot(2,2,4),imshow(uint8(b2)),title('5 x 5 Averaging filter')
%...........................
figure,subplot(2,2,1),imshow(a),title('Original Image'),
subplot(2,2,2),imshow(c),title('Gaussian noise'),
subplot(2,2,3),imshow(uint8(c1)),title('3 x 3 Averaging filter'),
subplot(2,2,4),imshow(uint8(c2)),title('5 x 5 Averaging filter'),
%..................
Original Image
3 x 3 Averaging filter
5 x 5 Averaging filter
Program 4
%This code is used to Butterworth highpass filter
close all;
clear all;
clc;
im=imread('d:\work\hibiscus.tif');
fc=40;
n=1;
[co,ro] = size(im);
cx = round(co/2); % find the center of the image
cy = round (ro/2);
imf=fftshift(fft2(im));
H=zeros(co,ro);
for i = 1 : co
for j =1 : ro
d = (i-cx).^2 + (j-cy).^ 2;
if d ~= 0
H(i,j) = 1/(1+((fc*fc/d).^(2*n)));
end;
end;
end;
outf = imf .* H;
out = abs(ifft2(outf));
imshow(im),title('Original Image'),figure,imshow(uint8(out)),title('Highpass Filterd Image')
figure,imshow(H),title('2D View of H'),figure,surf(H),
Program 5
%unsharp masking
clc
clear all
close all
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\babyelephant.jpg');
h=fspecial('unsharp');
b=imfilter(a,h);
imshow(a),title('original image')
figure,imshow(b), title('Unsharp mask')
original image
Unsharp mask
Program 6
Workshop on Image and Video Processing- A Practical Approach
28
Program 7
% Motion Blur
close all;
clear all;
clc;
a = imread(horse.jpg');
a=rgb2gray(a);
H = fspecial('motion',10,25);
MotionBlur_a = imfilter(a,H,'replicate');
imshow(a),title('Original Image');
figure,imshow(MotionBlur_a),title('Motion Blurred Image');
Original Image
Original Image
Degraded Image
Restored Image
Program 9
% This program computes the edges in the image
clear all;
close all;
clc;
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\dove1.jpg');
% a = rgb2gray(a);
b=edge(a,'roberts');
c=edge(a,'sobel');
d=edge(a,'prewitt');
e=edge(a,'log');
f=edge(a,'canny');
imshow(a),title('Original Image')
figure,imshow(b),title('Roberts')
figure,imshow(c),title('Sobel')
figure,imshow(d),title('Prewitt')
figure,imshow(e),title('Log')
figure,imshow(f),title('Canny')
Original Image
Prewitt
Roberts
Log
Sobel
Canny
Program 10
% Boundary detector
close all;
clear all;
clc;
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\morph2.bmp');
b=[0 1 0;1 1 1;0 1 0];
a1=imdilate(a,b);
a2=imerode(a,b);
a3=a-a2;
a4=a1-a;
Dilated image
Eroded image
Third
Property
Second
property
First property
Program 11
% Colour filtering
clc;
close all;
clear all;
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\sunflower2.jpg');
yiq=rgb2ntsc(a);
%Extract the Y component alone
b1=yiq(:,:,1);
h=[-1 -1 -1;-1 8 -1;-1 -1 -1];
%Perform high pass filtering only on Y component
c1=conv2(b1,h,'same');
yiq(:,:,1)=c1;
% Convert YIQ to RGB format
a1=ntsc2rgb(yiq);
figure,imshow(a),title('original image')
figure,imshow(a1),title('High pass filtered image')
Program 12
% Color image segmentation
clc
clear all
close all
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\Tomato2.jpg');
%Conversion of RGB to YCbCr
b=rgb2ycbcr(a);
%Threshold is applied only to Cb component
mask=b(:,:,2)>120;
imshow(a),title('original image')
figure,imshow(mask),title('Segmented image')
original image
Segmented image
Program 13
% Separating RGB components
original image
Red Component
Green Component
Blue Component
Program 14
% Finding Red. Green and Blue missing components
clc
clear all
close all
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\fl1.bmp');
original image
Red Missing!
Green Missing!
Blue Missing!
Program 15
% Wavelet Decomposition
clc
clear all
close all
a=imread('E:\Program Files\MATLAB\R2010b\toolbox\images\imdemos\images\zoneplate.png');
figure,imshow(a);
%First level decomposition
[p q r s]=dwt2(single(a),'db1');
b=[uint8(p), q; r,s];
%Second level decomposition
[p1 q1 r1 s1]=dwt2(p,'db1');
b1=[p1,q1; r1, s1];
b2=[uint8(b1), q; r s];
figure, imshow(b2)
_________________________________________________________
VIDEO PROCESSING- BASIC MATLAB CODES
1.To read a video file
clc;
close all;
vidread=aviread('rhinos.avi');
implay(vidread)
2.To Convert Video To Frames
clc;
clear clc;
clear all;
close all;
3. To read a video file
vidread =mmreader('rhinos.avi');
vidFrames = read(vidread);
vidfrmlocation='F:\vidFrames.jpg';
4. To read particular frame
figure,imshow(vidFrames(:,:,:,50));
numFrames = get(vidread,'numberOfFrames');
for NumFrm =1:5
Frames(:,:,:,NumFrm )= read(vidread,5 ); figure,imshow(Frames(:,:,:,NumFrm ));
end
5.Background Subtraction or Foreground Extraction with noise removal
clc;
close all;
I=imread ('E:\ \');
figure,imshow(I);
I1=imread ('E:\ \');
figure,imshow(I1);
I2=imread ('E:\ \');
forgnd=I-I1;figure,imshow(forgnd);
figure,imshow(im2bw(forgnd));
for i=1:240
for j=1:320
if forgnd(i,j)>(8)
b(i,j)=255;
else
b(i,j)=0;
end
Workshop on Image and Video Processing- A Practical Approach
36
16. Play the video five times with a frame rate of 30 fps.
movie(my_movie, 5, 30)
17. Play only frames 110.
frames = [5 1:10];
movie(my_movie, frames, 30)
18. Convert frame 10 to an image for further processing.
old_img = frame2im(my_movie(10));
19. Blur the image using an averaging filter and display the result.
fn = fspecial(average,7);
new_img = imfilter(old_img, fn);
figure , subplot(1,2,1), imshow(old_img), title(Original Frame);
subplot(1,2,2), imshow(new_img), title(Filtered Frame);
20. Using another frame, create a negative and display the result
old_img2 = frame2im(my_movie(15));
image_neg = imadjust(old_img2, [0 1], [1 0]);
figure
subplot(1,2,1), imshow(old_img2), title(Original Frame);
subplot(1,2,2), imshow(image_neg), title(Filtered Frame);
21.Now that we have processed our image, we can convert it back to frame using the im2frame
function.Convert the images back to frames and save the new frames in the video structure.
my_movie2 = my_movie;
new_frame10 = im2frame(new_img);
new_frame15 = im2frame(image_neg);
my_movie2(10) = new_frame10;
my_movie2(15) = new_frame15;
22. Create a negative of all the frames and reconstruct the frames into a video.
movie_neg = my_movie;
for k = 1:file_info.NumFrames
cur_img = frame2im(movie_neg(k));
new_img = imadjust(cur_img, [0 1], [1 0]);
Workshop on Image and Video Processing- A Practical Approach
39