Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Submitted by
Name: Akankhya Behera
Roll Number: 114CS0093
Day -1
Date: 03-08-2017
1. The aim of this experiment is to synthesize and display an image given a 2-Dimensional
function. Synthesize the following image of size 512 X 512 and display it.
3
I(x , y) = 255cos[2 (50 + 25)] , 0 x , y 511
Source code:
I=zeros(512);
for c = 0:511
for d = 0:511
I(c+1,d+1) = 255*cos(2*pi*((c/50)+(d/25)));
end
end
imshow(I);
Output:
2. Perform Zoom-in and Zoom-out operation using nearest neighborhood and bilinear
interpolation technique for `cameraman.tif' test image of size 256 X 256.
Source code:
im=imread('cameraman.tif');
I=zeros(512);
for c=1:256
for d=1:256
I(c,(2*d)-1)=im(c,d);
I(c,(2*d))=im(c,d);
end
end
I1=zeros(512);
for c=1:256
for d=1:512
I1((2*c)-1,d)=I(c,d);
I1((2*c),d)=I(c,d);
end
end
im3=mat2gray(I1);
imshow(im3);
%imshow(im),figure,imshow(im3);
I2=zeros(256);
for c=1:256
for d=1:256
4
I2(c,d)=im3((2*c),(2*d));
end
end
I3=mat2gray(I2);
%imshow(I3);
im=imread('cameraman.tif');
I=zeros(511);
for c=1:256
for d=1:256
I((2*c)-1,(2*d)-1)=im(c,d);
%I((2*c),(2*d))=im((2*c),d);
end
end
for c=1:2:511
for d=1:255
I(c,(2*d))=(I(c,(2*d)-1)+I(c,(2*d)+1))/2;
%I((2*c),(2*d))=im((2*c),d);
end
end
for c=2:2:510
for d=1:511
I(c,d)=(I(c-1,d)+I(c+1,d))/2;
%I((2*c),(2*d))=im((2*c),d);
end
end
im3=mat2gray(I);
imshow(im3);
%imshow(im),figure,imshow(im3);
I2=zeros(511);
for c=1:256
for d=1:511
I2(c,d)=im3((2*c)-1,d);
end
end
I4=zeros(256);
for c=1:256
for d=1:256
I4(c,d)=I2(c,(2*d)-1);
end
end
im4=mat2gray(I4);
%imshow(im4);
Output:
Original picture
5
Zoom Out
Nearest neighbour Bilinear Interpolation
Zoom In
Nearest neighbour Bilinear Interpolation
Day -2
Date: 10-08-2017
6
1. Perform negative, logarithm, and power law transformation on `'pout.tif' and `tire.tif' test
images.
Source code:
im=imread('pout.tif');
for c=1:291
for d=1:240
im(c,d)=255-im(c,d);
end
end
%imshow(im);
I=imread('tire.tif');
[M,N]=size(I);
c=1;
for i=1:M
for j=1:N
m=double(I(i,j));
I(i,j)=c.*(log2(1+m));
end
end
7
I1=mat2gray(I);
%imshow(I1);
I2=imread('pout.tif');
[M,N]=size(I2);
gamma=1;
c=0.5;
for i=1:M
for j=1:N
I2(i,j)=c.*(I2(i,j).^gamma);
end
end
I3=mat2gray(I2);
imshow(I3);
Output:
Gamma
2. Perform contrast stretching on `tire.tif' image with the help of piecewise linear function.
8
Source code:
im=imread('tire.tif');
[M,N]=size(im);
for i=1:M
for j=1:N
if im(i,j)>=0 && im(i,j)<100
im(i,j)=(im(i,j))/2;
elseif im(i,j)>=100 && im(i,j)<150
im(i,j)=(3*im(i,j))-250;
else
im(i,j)=(2550-(11*im(i,j)))/21;
end
end
end
I2=mat2gray(im);
imshow(I2);
Output:
9
G1=bitget(F,1);
G2=bitget(F,2);
G3=bitget(F,3);
G4=bitget(F,4);
G5=bitget(F,5);
G6=bitget(F,6);
G7=bitget(F,7);
G8=bitget(F,8);
figure(1);
subplot(4,4,[1 8]);imshow(F);title('Original 8 bit image');
subplot(4,4,9);imshow(logical(G1));title('Bitplane 1');
subplot(4,4,10);imshow(logical(G2));title('Bitplane 2');
subplot(4,4,11);imshow(logical(G3));title('Bitplane 3');
subplot(4,4,12);imshow(logical(G4));title('Bitplane 4');
subplot(4,4,13);imshow(logical(G5));title('Bitplane 5');
subplot(4,4,14);imshow(logical(G6));title('Bitplane 6');
subplot(4,4,15);imshow(logical(G7));title('Bitplane 7');
subplot(4,4,16);imshow(logical(G8));title('Bitplane 8');
Output:
10
4. Load an image `tire.tif' and plot the histogram. Perform histogram equalization and compare its
output with the output obtained using `histeq' inbuilt function.
Source code:
im=imread('tire.tif');
nlev=256;
g=histeq(im,nlev);
imshow(g);
figure,imshow(im);
[m,n]=size(im);
numofpixels=m*n;
him=uint8(zeros(m,n));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
for i=1:m
for j=1:n
11
value=im(i,j);
freq(value+1)=freq(value+1)+1;
probf(value+1)=freq(value+1)/numofpixels;
end
end
sum=0;
no_bins=256;
for i=1:size(probf)
sum=sum+freq(i);
cum(i)=sum;
probc(i)=cum(i)/numofpixels;
output(i)=round(probc(i)*no_bins);
end
for i=1:m
for j=1:n
him(i,j)=output(im(i,j)+1);
end
end
figure,imshow(him);
title('Histogram equalization');
Output:
12
I=imread('art8.jpg');
I=im2bw(I);
B=strel('square',3);
A=I;
%Find a non-zero element's position.
p=find(A==1);
p=p(1);
Label=zeros([size(A,1) size(A,2)]);
N=0;
while(~isempty(p))
N=N+1;%Label for each component
p=p(1);
X=false([size(A,1) size(A,2)]);
X(p)=1;
Y=A&imdilate(X,B);
while(~isequal(X,Y))
X=Y;
Y=A&imdilate(X,B);
end
13
Pos=find(Y==1);
A(Pos)=0;
%Label the components
Label(Pos)=N;
p=find(A==1);
end
imtool(Label);
end
RGBIm(:,:,1)=R;
RGBIm(:,:,2)=G;
RGBIm(:,:,3)=B;
RGBIm=uint8(RGBIm);
figure,imshow(RGBIm);title('Labelled Components');
Output:
14
Day -3
Date: 17-08-2017
15
1. Load an input image `eight.tif' and perform image averaging and weighted averaging operation
for different masks.
Source code:
Image Averaging
I=imread('eight.tif');
mI=zeros(size(I) + 2);
B=zeros(size(I));
n=input('define mask size ');
window=zeros(n);
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-(n-1)
for j=1:size(mI,2)-(n-1)
sum=0;
num=0;
for x=1:n
for y=1:n
window(x,y)=mI(i+x-1,j+y-1);
sum=sum+window(x,y);
end
end
B(i,j)=round(sum/(n*n));
end
16
end
B=uint8(B);
imshow(B);
figure,imshow(I);
Weighted Averaging
I=imread('eight.tif');
mI=zeros(size(I) + 2);
B=zeros(size(I));
window=zeros(3);
mask=[1 2 1;2 4 2;1 2 1];
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-2
for j=1:size(mI,2)-2
sum=0;
num=0;
for x=1:3
for y=1:3
window(x,y)=mI(i+x-1,j+y-1)*mask(x,y);
sum=sum+window(x,y);
end
end
B(i,j)=round(sum/16);
end
end
B=uint8(B);
%imshow(B);
figure,imshow(B);
Output:
17
2. Consider an input image `eight.tif' and add Salt and Pepper Noise of desired density p using
imnoise inbuilt function .Perform mean, median, max,and min filtering on the noisy image of
varying mask size (3 X 3, 5 X 5, 7 X 7 and 9 X9) and record their PSNR values.
Source code:
Median filtering
I2=imread('eight.tif');
I=imnoise(I2,'Salt & Pepper',0.1);
%imshow(I2);
mI=zeros(size(I) + 2);
B=zeros(size(I));
%n=input('define mask size ');
window=zeros(9,1);
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-2
for j=1:size(mI,2)-2
18
sum=0;
inc=1;
num=0;
for x=1:3
for y=1:3
window(inc)=mI(i+x-1,j+y-1);
inc=inc+1;
end
end
med=sort(window);
B(i,j)=med(5);
end
end
B=uint8(B);
imshow(B);
figure,imshow(I);
Mean Filtering
I2=imread('eight.tif');
I=imnoise(I2,'Salt & Pepper',0.1);
%imshow(I2);
mI=zeros(size(I) + 2);
B=zeros(size(I));
%n=input('define mask size ');
window=zeros(9,1);
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-2
for j=1:size(mI,2)-2
sum=0;
inc=1;
num=0;
for x=1:3
for y=1:3
window(inc)=mI(i+x-1,j+y-1);
sum=sum+window(inc);
inc=inc+1;
end
end
%med=sort(window);
B(i,j)=round(sum/9);
end
end
B=uint8(B);
imshow(B);
figure,imshow(I);
Max Filtering
19
I2=imread('eight.tif');
I=imnoise(I2,'Salt & Pepper',0.1);
%imshow(I2);
mI=zeros(size(I) + 2);
B=zeros(size(I));
%n=input('define mask size ');
window=zeros(9,1);
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-2
for j=1:size(mI,2)-2
sum=0;
inc=1;
num=0;
for x=1:3
for y=1:3
window(inc)=mI(i+x-1,j+y-1);
sum=sum+window(inc);
inc=inc+1;
end
end
maxm=max(window);
B(i,j)=maxm;
end
end
B=uint8(B);
imshow(B);
figure,imshow(I);
Min Filtering
I2=imread('eight.tif');
I=imnoise(I2,'Salt & Pepper',0.1);
%imshow(I2);
mI=zeros(size(I) + 2);
B=zeros(size(I));
%n=input('define mask size ');
window=zeros(9,1);
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-2
for j=1:size(mI,2)-2
20
sum=0;
inc=1;
num=0;
for x=1:3
for y=1:3
window(inc)=mI(i+x-1,j+y-1);
sum=sum+window(inc);
inc=inc+1;
end
end
minm=min(window);
B(i,j)=minm;
end
end
B=uint8(B);
imshow(B);
figure,imshow(I);
Output:
21
3. Load an image `cameraman.tif' and apply the following masks and observe the output
0 1 0 1 1 1
[1 4 1] [1 8 1]
0 1 0 1 1 1
Source code:
First Matrix
I=imread('cameraman.tif');
mask=[0 -1 0;-1 4 -1;0 -1 0];
mI=zeros(size(I) + 2);
B=zeros(size(I));
window=zeros(3);
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-2
for j=1:size(mI,2)-2
sum=0;
num=0;
for x=1:3
for y=1:3
window(x,y)=mI(i+x-1,j+y-1)*mask(x,y);
sum=sum+window(x,y);
end
end
B(i,j)=sum;
end
end
B=uint8(B);
%imshow(B);
figure,imshow(B);
22
Second matrix
I=imread('cameraman.tif');
mask=[-1 -1 -1;-1 8 -1;-1 -1 -1];
mI=zeros(size(I) + 2);
B=zeros(size(I));
window=zeros(3);
for x=1:size(I,1)
for y=1:size(I,2)
mI(x+1,y+1)=I(x,y);
end
end
for i=1:size(mI,1)-2
for j=1:size(mI,2)-2
sum=0;
num=0;
for x=1:3
for y=1:3
window(x,y)=mI(i+x-1,j+y-1)*mask(x,y);
sum=sum+window(x,y);
end
end
B(i,j)=sum;
end
end
B=uint8(B);
%imshow(B);
figure,imshow(B);
Output:
23
4. Perform histogram specification for a given input image and a desired/specified image.
Source Code
Output:
24
Day -4
Date: 24-08-2017
[r1,c1]=size(img);
[r2,c2]=size(h);
h0r=c1+c2-1;
h0=zeros(h0r,c2);
a=zeros(h0r*(r1+r2-1),r2*c2);
for j=1:r1
h0=zeros(h0r,c2);
h0(1:c1,1)=img(j,1:c1);
for i=2:c2
h0(2:h0r,i)=h0(1:h0r-1,i-1);
end
a(((j-1)*h0r+1):j*h0r,1:c2)=h0(1:h0r,1:c2);
end
for i=2:r2
a((i-1)*h0r+1:h0r*(r1+r2-1),(c2*(i-1))+1:i*c2)=a((i-2)*h0r+1:h0r*(r1+r2-
2),(c2*(i-2))+1:(i-1)*c2);
end
a
h1=h';
h1=h1(:);
c=a*h1;
m =1;
b=zeros(r1+r2-1,c1+c2-1);
for i=1:r1+r2-1
for j=1:c1+c2-1
b(i,j) = c(m);
m=m+1;
end
end
b
Output:
imgg=[1,2;3 4];
[imgX,imgY]=size(imgg);
[hX,hY]=size(hh);
img=zeros(imgX,imgY);
h=zeros(hX,hY);
for i=1:imgX
for j=1:imgY
img(i,j)=imgg(i,j);
end
end
for i=1:hX
for j=1:hY
h(i,j)=hh(i,j);
end
end
[r1,c1]=size(img);
%h0=zeros(c1,c1);
a=zeros(r1*c1,r1*c1);
for j=1:r1
h0=zeros(c1,c1);
h0(1:c1,1)=img(j,1:c1);
for i=2:c1
h0(1,i)=h0(c1,i-1);
h0(2:c1,i)=h0(1:c1-1,i-1);
end
a(((j-1)*c1+1):j*c1,1:c1)=h0(1:c1,1:c1);
end
for i=2:r1
for j=2:c1
a(1:c1,(c1*(i-1))+1:i*c1)=a(c1*(r1-1)+1:r1*c1,(c1*(i-2))+1:(i-1)*c1);
a((i-1)*r1+1:r1*c1,(c1*(j-1))+1:j*c1)=a((i-2)*r1+1:r1*(c1-1),(c1*(j-
2))+1:(j-1)*c1);
end
end
%a=a.';
a
h1=h';
h1=h1(:);
c=a*h1;
m =1;
b=zeros(r1,c1);
for i=1:r1
for j=1:c1
b(i,j) = c(m);
m=m+1;
end
end
b
Output:
27
[r1,c1]=size(img);
%h0=zeros(c1,c1);
a=zeros(r1*c1,r1*c1);
for j=1:r1
h0=zeros(c1,c1);
h0(1:c1,1)=img(j,1:c1);
for i=2:c1
h0(1,i)=h0(c1,i-1);
h0(2:c1,i)=h0(1:c1-1,i-1);
end
a(((j-1)*c1+1):j*c1,1:c1)=h0(1:c1,1:c1);
end
for i=2:r1
for j=2:c1
a(1:c1,(c1*(i-1))+1:i*c1)=a(c1*(r1-1)+1:r1*c1,(c1*(i-2))+1:(i-1)*c1);
a((i-1)*r1+1:r1*c1,(c1*(j-1))+1:j*c1)=a((i-2)*r1+1:r1*(c1-1),(c1*(j-
2))+1:(j-1)*c1);
end
end
%a=a.';
a
28
h1=h';
h1=h1(:);
c=a*h1;
m =1;
b=zeros(r1,c1);
for i=1:r1
for j=1:c1
b(i,j) = c(m);
m=m+1;
end
end
b
Output:
29
Day -5
Date: 31-08-2017
1. Plot the real and imaginary component of the basis images separately in case of DFT for a
specified size (e.g., 8 X 8,16 X16)
Source Code
N=8;
K=8;
AR=zeros(N,K);
AI=zeros(N,K);
for i=1:N
for j=1:K
AR(i,j)=cos((2*pi*(i-1)*(j-1))/N);
AI(i,j)=sin((2*pi*(i-1)*(j-1))/N);
end
end
A=AR - 1j.*AI;
30
A=conj(A');
count=1;
for i=1:N
for j=1:K
OP=(A(1:N,i)*(A(1:N,j)'));
subplot(8,8,count);
count=count+1;
imshow(real(OP));
end
end
figure,
count=1;
for i=1:N
for j=1:K
OP=(A(1:N,i)*(A(1:N,j)'));
subplot(8,8,count);
count=count+1;
imshow(imag(OP));
end
end
Output:
31
2. Plot the basis images of Discrete Cosine Transform (DCT) for a given size (e.g., 8 X 8, 16 X 16).
Source Code
clc;
clear all;
m=4;
n=m;
alpha2=ones(1,n)*sqrt(2/n);
alpha2(1)=sqrt(1/n);
alpha1=ones(1,m)*sqrt(2/m);
alpha1(1)=sqrt(1/m);
for u=0:m-1
for v=0:n-1
for x=0:m-1
for y=0:n-1
alpha1=sqrt(2/n);
alpha2=sqrt(2/n);
a{u+1,v+1}(x+1,y+1)=alpha1*alpha2*cos((2*x+1)*u*pi/(2*n))*cos((2*y+1)*v*pi/(2*n))
;
end
end
end
end
for u=1
for v=1
for x=0:m-1
for y=0:n-1
alpha1=sqrt(1/n);
alpha2=sqrt(1/n);
a{u,v}(x+1,y+1)=alpha1*alpha2*cos((2*x+1)*(u-
1)*pi/(2*n))*cos((2*y+1)*(v-1)*pi/(2*n));
end
end
end
end
mag=a;
%figure,
count=1;
for i=1:m
for j=1:n
subplot(m,n,count);
imshow(mag{i,j});
count=count+1;
end
end
32
Output:
[x,y]=size(Img);
subplot(1,3,1);
imshow(Img);
Img=double(Img);
A=zeros(x,y);
for i=1:x
for j=1:y
A(i,j)=exp((-1*1j*2*pi*j*i)/x);
end
end
%output=zeros(80,80);
Output=A*Img*A';
Output=fftshift(Output);
Output2 = zeros(x,y);
for u = 1:x
for v = 1:y
Output2(u,v) = sqrt((real(Output(u,v))^2+imag(Output(u,v))^2));
end
end
33
%Output2=fftshift(Output2);
subplot(1,3,2);
imshow(mat2gray(log10(1+Output2)))
Output:
4. Load two input images, f1(x; y) and f2(x; y) and determine the results based on linearity property
of 2D-DFT using the following equation.
F[af1(x; y) + bf2(x; y)] = aF1(u; v) + bF2(u; v)
Source Code
clc;
clear all;
a=imread('cameraman.tif');
b=imread('moon.tif');
b=imresize(b,[256,256]);
ffta=fft2(double(a));
fftb=fft2(double(b));
m=2;
n=3;
A=(m.*ffta)+(n.*fftb);
B=fft2(double(((m.*a)+ (n.*b))));
subplot(1,2,1);
imshow(uint8(ifft2(A)));
subplot(1,2,2);
imshow(uint8(ifft2(B)))
Output:
34
5. Load two images `moon.tif' and `cameraman.tif'. Exchange the phase angle between two
images and reconstruct the images.
Source Code
clc;
clear all;
a=imread('cameraman.tif');
b=imread('moon.tif');
b=imresize(b,[256,256]);
ffta=fft2(double(a));
fftb=fft2(double(b));
mag_a=abs(ffta);
ph_a=angle(ffta);
mag_b=abs(fftb);
ph_b=angle(fftb);
newfft_a=mag_a.*(exp(i*ph_b));
newfft_b=mag_b.*(exp(i*ph_a));
subplot(1,2,1);
imshow(uint8(ifft2(newfft_a)));
subplot(1,2,2);
imshow(uint8(ifft2(newfft_b)));
Output:
35
Day -6
Date: 07-09-2017
1. Consider an input image `cameraman.tif' and display its DCT coeficients. Perform image
reconstruction using the high energy coeficients of DCT.
Source Code
im=imread('cameraman.tif');
A=dct2(im);
%imshow(log(abs(A)),[])
A(abs(A)<20)=0;
B=idct2(A);
subplot(1,2,1);
imshow(im);
subplot(1,2,2);
imshow(mat2gray(B));
Output:
36
2. Load an input image `lena.jpg' and perform image reconstruction using KL-transform.
Source Code
I=imread('cameraman.tif');
I=im2double(I);
m=1;
for i=1:8:256
for j=1:8:256
for x=0:7
for y=0:7
img(x+1,y+1)=I(i+x,j+y);
end
end
k=0;
for l=1:8
img_expect{k+1}=img(:,l)*img(:,l)';
k=k+1;
end
imgexp=zeros(8:8);
for l=1:8
imgexp=imgexp+(1/8)*img_expect{l};%expectation of E[xx']
end
img_mean=zeros(8,1);
for l=1:8
img_mean=img_mean+(1/8)*img(:,l);
end
img_mean_trans=img_mean*img_mean';
img_covariance=imgexp - img_mean_trans;
[v{m},d{m}]=eig(img_covariance);
temp=v{m};
m=m+1;
for l=1:8
v{m-1}(:,l)=temp(:,8-(l-1));
end
for l=1:8
trans_img1(:,l)=v{m-1}*img(:,l);
end
for x=0:7
for y=0:7
transformed_img(i+x,j+y)=trans_img1(x+1,y+1);
end
37
end
mask=[1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 ];
trans_img=trans_img1.*mask;
for l=1:8
inv_trans_img(:,l)=v{m-1}'*trans_img(:,l);
end
for x=0:7
for y=0:7
inv_transformed_img(i+x,j+y)=inv_trans_img(x+1,y+1);
end
end
end
end
subplot(1,2,2);
imshow(transformed_img);
subplot(1,2,1);
imshow(inv_transformed_img);
Output:
3. Plot the the basis images of Walsh transform for a specified size (e.g., 8X8, 16X16).
Source Code
clear all;
close all;
clc;
n=8;
m=n;
for u=0:n-1
for v=0:n-1
for x=0:n-1
for y=0:n-1
38
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
end
end
mag=basis;
figure(1);
k=1;
for i=1:m
for j=1:n
subplot(m,n,k)
imshow(mag{i,j})
k=k+1;
end
end
Output:
39
4. Plot the basis images of Hadamard transform for a given size (e.g., 8X8, 16X16).
Source Code
clear;
N = 8;
G = zeros(N);
n = log2(N);
for x = 0:N-1
for u = 0:N-1
temp = 1;
for i = 0:n-1
temp = temp * (-1)^(find_b(i,x,n)*find_p(i,u,n));
end
G(x+1,u+1) = temp;
end
end
for i=1:N
for j=1:N
img = G(:,i) * G(:,j)';
subplot(N,N,(i-1)*N+j),imshow(img,[]);
end
end
Output
40
Day -7
Date: 05-10-2017
1. Perform Salt and pepper noise reduction using adaptive median filtering. Compare its result with the
standard median filtering with respect to PSNR and SSIM values.
Source Code
clear all;
img=imread('cameraman.tif');
s1=size(img);
nimg=imnoise(img,'salt & pepper', 0.05);
subplot(1,2,1);
imshow(nimg);
img1=nimg;
img2=nimg;
img3=nimg;
img4=nimg;
for i=6:s1(1)-6
for j=6:s1(2)-6
img1(i,j)=A(img,1,i,j);
end
end
subplot(1,2,2);
imshow(img1);
p1=psnr1(nimg,img1)
function f=A(img,ws,i,j)
t=[];
41
for k=-ws:ws
for l=-ws:ws
t=[t,img(i+k,j+l)];
end
end
a1=median(t)-min(t);
a2=median(t)-max(t);
if a1>0 && a2<0
f=B(img(i,j),t);
elseif ws+1<=5
f=A(img,ws+1,i,j);
else
f=img(i,j);
end
end
function m=B(x,t)
b1=x-min(t);
b2=x-max(t);
if b1>0 && b2<0
m=x;
else
m=median(t);
end
end
function m=mean1(x)
m=0;
x=double(x);
for i=1:9
m=m+x(i);
end
m=m/9;
end
function m=median1(x)
x=sort(x);
m=x(5);
end
function m=max1(x)
x=sort(x);
m=x(9);
end
function m=min1(x)
x=sort(x);
m=x(1);
end
function p=psnr1(a,b)
[m,n]=size(a);
x=0;
for i=1:m
for j=1:n
x=x+double(a(i,j)-b(i,j))*double(a(i,j)-b(i,j));
end
end
x=x/(m*n);
42
p=10*log10(255*255/x);
end
Output
2. Perform Salt and pepper noise reduction using weighted median filtering and its adaptive variant.
Compare its result with the standard median filtering with respect to PSNR values.
Source Code
clear all;
img=imread('eight.tif');
s1=size(img);
nimg=imnoise(img,'salt & pepper', 0.02);
subplot(1,3,1);
imshow(nimg);
title('noised image')
img1=nimg;
img2=nimg;
for i=2:s1(1)-1
for j=2:s1(2)-1
t=[img(i-1,j-1),img(i-1,j),img(i-1,j+1),img(i,j-
1),img(i,j),img(i,j+1),img(i+1,j-1),img(i+1,j),img(i+1,j+1)];
img1(i,j)=wmedian1(t);
img2(i,j)=median1(t);
end
end
subplot(1,3,2);
imshow(img1);
title('weighted median');
subplot(1,3,3);
imshow(img2);
title('median');
p1=psnr1(nimg,img1)
p2=psnr1(nimg,img2)
function m=wmedian1(x)
wt=[0.2,0.15,0.05,0.05,0.05,0.15,0.2,0.05,0.1];
[x,I]=sort(x);
43
wt2=[];
for i=1:9
wt2=[wt2,wt(I(i))];
end
tot=0;
j=-1;
for i=1:9
tot=tot+wt2(i);
if tot>0.45
j=i;
break;
end
end
m=x(j);
end
function m=median1(x)
x=sort(x);
m=x(5);
end
function p=psnr1(a,b)
[m,n]=size(a);
x=0;
for i=1:m
for j=1:n
x=x+double(a(i,j)-b(i,j))*double(a(i,j)-b(i,j));
end
end
x=x/(m*n);
p=10*log10(255*255/x);
end
Output
3. Perform Salt and pepper noise reduction using center weighted median filtering and its adaptive
variant.
Source Code
clear all;
img=imread('cameraman.tif');
sz=size(img);
nsimg=imnoise(img,'salt & pepper', 0.02);
44
subplot(1,3,1);
imshow(nsimg);
title('Noisy image')
img1=nsimg;
img2=nsimg;
for i=2:sz(1)-1
for j=2:sz(2)-1
t=[img(i-1,j-1),img(i-1,j),img(i-1,j+1),img(i,j-
1),img(i,j),img(i,j+1),img(i+1,j-1),img(i+1,j),img(i+1,j+1)];
img1(i,j)=wmedian1(t);
img2(i,j)=median1(t);
end
end
subplot(1,3,2);
imshow(img1);
title('weighted median');
subplot(1,3,3);
imshow(img2);
title('median');
p1=psnr1(nsimg,img1)
p2=psnr1(nsimg,img2)
function m=wmedian1(x)
wt=[0.05,0.05,0.05,0.05,0.6,0.05,0.05,0.05,0.05];
[x,I]=sort(x);
wt2=[];
for i=1:9
wt2=[wt2,wt(I(i))];
end
tot=0;
j=-1;
for i=1:9
tot=tot+wt2(i);
if tot>0.45
j=i;
break;
end
end
m=x(j);
end
function m=median1(x)
x=sort(x);
m=x(5);
end
function p=psnr1(a,b)
[m,n]=size(a);
x=0;
for i=1:m
for j=1:n
x=x+double(a(i,j)-b(i,j))*double(a(i,j)-b(i,j));
end
45
end
x=x/(m*n);
p=10*log10(255*255/x);
end
Output
Gaussian noise Rayleigh noise Erlang (Gamma) noise Exponential noise Uniform noise
Impulse (salt & pepper) noise
Source Code
z=0:255
mu=100
de=10
a=80
b=150
for i=1:256
y(i)=1/(sqrt(2.0*pi)*de)*exp(-(z(i)-mu)*(z(i)-mu)/(2*de*de));
end
plot(z,y)
title('gaussian'),figure
for i=1:256
if(z(i)>=a)
y(i)=2/b*(z(i)-a)*(exp(-(z(i)-a)*(z(i)-a)/b));
else
y(i)=0;
end
end
plot(z,y)
title('rayleigh'),figure
a=1
b=5
for i=1:256
if(z(i)>=0)
y(i)=(power(a,b)*power(z(i),b-1)*(z(i)-a)*exp(-
a*z(i)))/(factorial(b-1));
else
y(i)=0;
end
end
plot(z,y)
title('gamma'),figure
46
a=.05
for i=1:256
y(i)=a*exp(-a*z(i));
end
plot(z,y)
title('exponential'),figure
a=20
b=50
for i=1:256
if z(i)>=a && z(i)<=b
y(i)=1/(b-a);
else
y(i)=0;
end
end
plot(z,y)
title('uniform'),figure
a=50
b=150
for i=1:256
if z(i)==a || z(i)==b
y(i)=0.25;
else
y(i)=0;
end
end
plot(z,y)
title('salt and pepper')
Output
47
Day -8
Date: 12-10-2017
3 Perform Huffman coding for an input matrix and calculate Lavg, entropy
and efficiency
clc;
x=imread('cameraman.tif');
x=double(x);
[M,N]=size(x);
t=zeros(M,N);
k=fft2(x);
for i=1:M
for j=1:N
H(i,j)=((1/(pi*(i*0.001+j*0.1)))*sin(pi*(i*0.001+j*0.1))*exp(-
1j*pi*(i*0.001+j*0.1)));
end
end
G=k.*H;
b=ifft2(G);
y=b;
imshow(uint8(b))
l=fft2(y);
l=l./H;
l=ifft2(l);
figure,
imshow(uint8(l))
Output
48
2.
Source Code
clc;
x=imread('cameraman.tif');
x=double(x);
[M,N]=size(x);
t=zeros(M,N);
k=fft2(x);
for i=1:M
for j=1:N
H(i,j)=exp(-0.0001*((i*i+j*j)^(5/6)));
end
end
G=k.*H;
b=ifft2(G);
y=b;
imshow(uint8(b))
l=fft2(y);
l=l./H;
l=ifft2(l);
figure,
imshow(uint8(l))
Output
Output
3.Perform Huffman coding for an input matrix and calculate Lavg, entropy and efficiency
Sourcecode
#include <stdio.h>
#include <string.h>
/* priority queue */
void qinsert(node n)
{
int j, i = qend++;
while ((j = i / 2)) {
if (q[j]->freq <= n->freq) break;
q[i] = q[j], i = j;
}
q[i] = n;
50
node qremove()
{
int i, l;
node n = q[i = 1];
build_code(q[1], c, 0);
}
if (n->c) putchar(n->c), n = t;
51
putchar('\n');
if (t != n) printf("garbage input\n");
}
int main(void)
{
int i;
const char *str = "this is an example for huffman encoding";
char buf[1024];
init(str);
for (i = 0; i < 128; i++)
if (code[i]) printf("'%c': %s\n", i, code[i]);
encode(str, buf);
printf("encoded: %s\n", buf);
printf("decoded: ");
decode(buf, q[1]);
return 0;
}
Output
52
Day -9
Date: 26-10-2017
Input symbol: a1 a2 a3 a4
Probability : 0.2 0.2 0.4 0.2
2 Write a program to perform LZW coding for a given sequence
1. Perform arithmetic coding for any input sequence (e.g. a1a2a3a3a4) based on the following data and do the
reverse operation for verification.
Input symbol: a1 a2 a3 a4
function[Tag_bits]=Arithmetic_enc(sym,p,seq)
Fx=zeros(1,length(sym));
for i=1:length(sym)
if i==1
Fx(i)=p(i);
else
Fx(i)=Fx(i-1)+p(i);
end
end
L=0;U=1;
53
Tag_bits=zeros(1,0);
for i=1:length(seq)
j=find(seq(i)==sym);
if(j==1)
L_new=L;
else
L_new=L+(U-L)*Fx(j-1);
end
U_new=L+(U-L)*Fx(j);
L=L_new;
U=U_new;
Tag_bits=[Tag_bits,'0'];
L=2*L;
U=2*U;
else
Tag_bits=[Tag_bits,'1'];
L=2*(L-0.5);
U=2*(U-0.5);
end
end
end
tag=(L+U)/2;
bits=zeros(1,0);
if(2*tag>1)
tag=2*tag-1;
bits=[bits,'1'];
else
tag=2*tag;
bits=[bits,'0'];
end
while(bin2dec(bits)/2^length(bits)<L)
if(2*tag>1)
tag=2*tag-1;
bits=[bits,'1'];
54
else
tag=2*tag;
bits=[bits,'0'];
end
end
Tag_bits=[Tag_bits,bits];
Tag_bits=[Tag_bits,dec2bin(0,16-rem(length(Tag_bits),16))];
disp(bin2dec(Tag_bits)/2^length(Tag_bits));
Tag_bits=[Tag_bits,dec2bin(length(seq),16)];
else
end
Output:
Decoding:
function[]=Arithmetic_dec(sym,p,tagword)
Fx=zeros(1,length(sym));
for i=1:length(sym)
if i==1
Fx(i)=p(i);
else
Fx(i)=Fx(i-1)+p(i);
55
end
end
seq_len=bin2dec(tagword(length(tagword)-15:length(tagword)));
L=0;U=1;
tagvalue=bin2dec(tagword)/2^length(tagword);
for i=1:seq_len
for j=1:length(sym)
if(Fx(j)>(tagvalue-L)/(U-L))
disp(j);
if j==1
L_new=L;
else
L_new=L+(U-L)*Fx(j-1);
end
U_new=L+(U-L)*Fx(j);
break;
end
end
L=L_new;
U=U_new;
end
end
vector = uint16(vector(:)');
table = cell(1,256);
table{index} = uint16(index-1);
end
% initialize output
output = vector;
% main loop
outputindex = 1;
startindex = 1;
for index=2:length(vector),
element = vector(index);
substr = vector(startindex:(index-1));
if isempty(code),
output(outputindex) = getcodefor(substr,table);
outputindex = outputindex+1;
startindex = index;
else,
% go on looping
end
end
substr = vector(startindex:index);
output(outputindex) = getcodefor(substr,table);
output((outputindex+1):end) = [];
code = uint16([]);
if length(substr)==1,
code = substr;
for index=257:length(table),
if isequal(substr,table{index}),
57
break
end
end
end
code = uint16(code-1);
str = 'ABABBABCABABBA';
[packed,table]=norm2lzw(uint8(str));
Output:
Day -10
Date: 2-11-2017
Use Harr basis wavelet function as the filter. The filter h[n], which generates
the scaling function, is an FIR lowpass filter; the filter h[n], which generates
the wavelet terms is a highpass filter. For exact reconstruction the filters
must satisfy the relationship h[n]=(1)nh[n] The filters that correspond to
the Haar wavelet are simple two-point filters defined by
h[n] = {0.707, n = 0;
0.707, n = 1;
0, otherwise}
I=imread('pout.tif');
I=imresize(I,[200 200]);
I=fliplr(I);
u=[0.707 -0.707];
v=[0.707 0.707];
59
col=zeros(200,1);
col1=zeros(100,1);
D=zeros(100,100);
V=zeros(100,100);
H=zeros(100,100);
X=zeros(100,100);
lowcol=zeros(100,200);
hicol=zeros(100,200);
lowcol1=zeros(100,200);
hicol1=zeros(100,200);
row=zeros(1,200);
row1=zeros(1,100);
row2=zeros(1,100);
D1=zeros(100,200);
V1=zeros(100,200);
t=1;
%ccol=conv()
for i=1:200
for j=1:200
col(j,1)=I(j,i);
end
G=conv(col,v);
Q=conv(col,u);
F=downsample(G,2);
E=downsample(Q,2);
for k=1:100
lowcol(k,t)=F(k,1);
hicol(k,t)=E(k,1);
end
t=t+1;
end
t=1;
for i=1:100
for j=1:200
row(1,j)=hicol(i,j);
end
G=conv(row,v);
60
Q=conv(row,u);
F=downsample(G,2);
E=downsample(Q,2);
for k=1:100
V(t,k)=F(1,k);
D(t,k)=E(1,k);
end
t=t+1;
end
t=1;
for i=1:100
for j=1:200
row(1,j)=lowcol(i,j);
end
G=conv(row,v);
Q=conv(row,u);
F=downsample(G,2);
E=downsample(Q,2);
for k=1:100
X(t,k)=F(1,k);
H(t,k)=E(1,k);
end
t=t+1;
end
figure
imshow(mat2gray(V))
figure
imshow(mat2gray(D))
figure
imshow(mat2gray(X))
figure
imshow(mat2gray(H))
t=1;
for i=1:100
61
for j=1:100
row1(1,j)=D(i,j);
row2(1,j)=V(i,j);
end
F1=upsample(row1,2);
E1=upsample(row2,2);
G1=conv(F1,u);
Q1=conv(E1,v);
J1=G1+Q1;
for k=1:200
D1(t,k)=J1(1,k);
end
t=t+1;
end
for i=1:200
for j=1:100
col1(j,1)=D1(j,i);
end
F1=upsample(col1,2);
for k=1:200
end
end
figure
imshow(mat2gray(F1))
Output:
62
Day -11
Date: 9-11-2017
63
1. Jpeg compression.
function[res]=jpeg_compression(im)
[m n]=size(im)
q_mtx = [[16 11 10 16 24 40 51 61];
[12 12 14 19 26 58 60 55];
[14 13 16 24 40 57 69 56];
[14 17 22 29 51 87 80 62];
[18 22 37 56 68 109 103 77];
[24 35 55 64 81 104 113 92];
[49 64 78 87 103 121 120 101];
[72 92 95 98 112 100 103 99]];
res=zeros(m,n);
i=1;
for k=1:8:m-7
for l=1:8:n-7
subblock(:,:,i)=im(k:k+7,l:l+7);
subblock(:,:,i)=dct2(subblock(:,:,i))./q_mtx;
temp=subblock(1,6,i);
for i2=1:8
for j2=1:8
if (i2+j2>6)
subblock(i2,j2,i)=0;
end
end
end
subblock(1,6,i)=temp;
subblock(:,:,i)= subblock(:,:,i).*q_mtx;
subblock(:,:,i)=idct2(subblock(:,:,i));
res(k:k+7,l:l+7)=subblock(:,:,i);
i=i+1;
end
end
end
close all;
im=imread('onion.png')
64
im=im2double(im);
for i=1:3
im(:,:,i)=jpeg_compression(im(:,:,i));
end
imshow(im)
65
Output:
function[res,reshist]=histogrameq(im)
his=imhist(im);
[m n]=size(im);
size(his);
nj(1)=his(1);
for i=2:256
nj(i,1)=nj(i-1,1)+his(i,1);
end
nj=nj./(m*n);
nj=nj.*255;
res=[]
for i=1:m
for j=1:n
res(i,j)=nj(im(i,j)+1);
end
end
reshist=imhist(res);
end
Output:
66
function[res]=smoothing(im)
m3 = [1 1 1;1 1 1;1 1 1]
[m n]=size(im);
im=double(im);
res=im;
for i=2:m-1
for j=2:n-1
res(i,j)= m3(1,1)*im(i-1,j-1)+m3(1,2)*im(i-1,j)+m3(1,3)*im(i-
1,j+1)+m3(2,1)*im(i,j-1)+m3(2,2)*im(i,j)+m3(2,3)*im(i,j+1)+m3(3,1)*im(i+1,j-
1)+m3(3,2)*im(i+1,j)+m3(3,3)*im(i+1,j+1);
end
end
res=uint8(res/9);
end
Output:
67