%page=235
%DFT and IDFT
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(2,2,1);
imshow(imageA);
title('Image A ');
imageA=double(imageA);
%-1 *
for x=1:M
for y=1:N
imageA(x,y)=imageA(x,y)*(-1)^(x+y);
end
end
%Perform 2D FFTs
h=waitbar(0,'Calculating DFT...');
for u=1:M
for v=1:N
sum=0.0;
for x=1:M
for y=1:N
sum=sum+double(imageA(x,y))*(exp(-j*2*pi*(u*x/M+v*y/N)));
end
end
F(u,v)=sum;
end
waitbar(u/M);
end
close(h);
subplot(2,2,2);
imshow(abs(F),[24 100000])
title('Image A DFT Magnitude');
subplot(2,2,3);
imshow(angle((F)),[-pi pi])
title('Image A DFT Phase');
%Define Filter function h
h=zeros(M,N);
d0=input('Enter the value of D0=');
n=input('Enter the order of n=');
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1/(1+(d/d0)^(2*n));
end
end
%Step 3
for u=1:M
for v=1:N
glo(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
h=waitbar(0,'Calculating IDFT...');
for u=1:M
for v=1:N
suml=0.0;
for x=1:M
for y=1:N
suml=suml+double(glo(x,y))*(exp(j*2*pi*(u*x/M+v*y/N)));
end
end
f(u,v)=suml/(M*N);
end
waitbar(u/M);
end
close(h);
%-1 *
for x=1:M
for y=1:N
f(x,y)=f(x,y)*(-1)^(x+y);
end
end
%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');
UVA Solution
Programming Contest Problems and Solutions
Sunday, April 10, 2016
Matlab: Butterworth_HighPass_Filter
%page=235
%DFT and IDFT
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(2,2,1);
imshow(imageA);
title('Image A ');
imageA=double(imageA);
%-1 *
for x=1:M
for y=1:N
imageA(x,y)=imageA(x,y)*(-1)^(x+y);
end
end
%Perform 2D FFTs
h=waitbar(0,'Calculating DFT...');
for u=1:M
for v=1:N
sum=0.0;
for x=1:M
for y=1:N
sum=sum+double(imageA(x,y))*(exp(-j*2*pi*(u*x/M+v*y/N)));
end
end
F(u,v)=sum;
end
waitbar(u/M);
end
close(h);
subplot(2,2,2);
imshow(abs(F),[24 100000])
title('Image A DFT Magnitude');
subplot(2,2,3);
imshow(angle((F)),[-pi pi])
title('Image A DFT Phase');
%Define Filter function h
h=zeros(M,N);
d0=input('Enter the value of D0=');
n=input('Enter the order of n=');
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1/(1+(d0/d)^(2*n));
end
end
%Step 3
for u=1:M
for v=1:N
glo(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
h=waitbar(0,'Calculating IDFT...');
for u=1:M
for v=1:N
suml=0.0;
for x=1:M
for y=1:N
suml=suml+double(glo(x,y))*(exp(j*2*pi*(u*x/M+v*y/N)));
end
end
f(u,v)=suml/(M*N);
end
waitbar(u/M);
end
close(h);
%-1 *
for x=1:M
for y=1:N
f(x,y)=f(x,y)*(-1)^(x+y);
end
end
%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');
%DFT and IDFT
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(2,2,1);
imshow(imageA);
title('Image A ');
imageA=double(imageA);
%-1 *
for x=1:M
for y=1:N
imageA(x,y)=imageA(x,y)*(-1)^(x+y);
end
end
%Perform 2D FFTs
h=waitbar(0,'Calculating DFT...');
for u=1:M
for v=1:N
sum=0.0;
for x=1:M
for y=1:N
sum=sum+double(imageA(x,y))*(exp(-j*2*pi*(u*x/M+v*y/N)));
end
end
F(u,v)=sum;
end
waitbar(u/M);
end
close(h);
subplot(2,2,2);
imshow(abs(F),[24 100000])
title('Image A DFT Magnitude');
subplot(2,2,3);
imshow(angle((F)),[-pi pi])
title('Image A DFT Phase');
%Define Filter function h
h=zeros(M,N);
d0=input('Enter the value of D0=');
n=input('Enter the order of n=');
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1/(1+(d0/d)^(2*n));
end
end
%Step 3
for u=1:M
for v=1:N
glo(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
h=waitbar(0,'Calculating IDFT...');
for u=1:M
for v=1:N
suml=0.0;
for x=1:M
for y=1:N
suml=suml+double(glo(x,y))*(exp(j*2*pi*(u*x/M+v*y/N)));
end
end
f(u,v)=suml/(M*N);
end
waitbar(u/M);
end
close(h);
%-1 *
for x=1:M
for y=1:N
f(x,y)=f(x,y)*(-1)^(x+y);
end
end
%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');
Matlab: image_gaussian_low_pass
%page=235
%image_gaussian_low_pass.m
clc;
clear all;
close all;
imageAa = imread('6.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=0:5:10
h=zeros(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=exp(-(d^2/(2*(d0^2))));
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
%image_gaussian_low_pass.m
clc;
clear all;
close all;
imageAa = imread('6.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=0:5:10
h=zeros(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=exp(-(d^2/(2*(d0^2))));
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
Matlab: image_gaussian_high_pass
%page=235
%image_gaussian_high_pass
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=0:2:4
h=ones(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1-(exp(-(d^2/(2*(d0^2)))) );
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
%image_gaussian_high_pass
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=0:2:4
h=ones(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1-(exp(-(d^2/(2*(d0^2)))) );
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
Matlab: DFT and IDFT image function
%page=235
%DFT and IDFT
clc;
clear all;
close all;
imageAa = imread('6.jpg');
imageA =rgb2gray(imageAa );
%Display images
subplot(2,2,1);
imshow(imageA);
title('Image A ');
[M N]=size(imageA)
shift_F=dft_img(imageA);
subplot(2,2,2);
imshow(abs(shift_F),[24 100000])
title('Image A DFT Magnitude');
subplot(2,2,3);
%imshow(angle((shift_F)),[-pi pi]);
F2 = log(abs(shift_F));
imshow(F2,[-1 5]);
title('Image A DFT Log');
%Calculating IDFT...
f=idft_img(shift_F);
%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');
%DFT and IDFT
clc;
clear all;
close all;
imageAa = imread('6.jpg');
imageA =rgb2gray(imageAa );
%Display images
subplot(2,2,1);
imshow(imageA);
title('Image A ');
[M N]=size(imageA)
shift_F=dft_img(imageA);
subplot(2,2,2);
imshow(abs(shift_F),[24 100000])
title('Image A DFT Magnitude');
subplot(2,2,3);
%imshow(angle((shift_F)),[-pi pi]);
F2 = log(abs(shift_F));
imshow(F2,[-1 5]);
title('Image A DFT Log');
%Calculating IDFT...
f=idft_img(shift_F);
%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');
Matlab: IDFT image function
function [f]=idft_img(shift_F)
[M N]=size(shift_F);
h=waitbar(0,'Calculating IDFT...');
for u=0:M-1
for v=0:N-1
suml=0.0;
for x=0:M-1
for y=0:N-1
suml=suml+double(shift_F(x+1,y+1))*(exp(j*2*pi*(u*x/M+v*y/N)));
end
end
f(u+1,v+1)=suml/(M*N);
end
waitbar(u/M);
end
close(h);
%-1 *
for x=1:M
for y=1:N
f(x,y)=f(x,y)*(-1)^(x+y);
end
end
[M N]=size(shift_F);
h=waitbar(0,'Calculating IDFT...');
for u=0:M-1
for v=0:N-1
suml=0.0;
for x=0:M-1
for y=0:N-1
suml=suml+double(shift_F(x+1,y+1))*(exp(j*2*pi*(u*x/M+v*y/N)));
end
end
f(u+1,v+1)=suml/(M*N);
end
waitbar(u/M);
end
close(h);
%-1 *
for x=1:M
for y=1:N
f(x,y)=f(x,y)*(-1)^(x+y);
end
end
Matlab: Ideal_Low_Pass_Filter
%page=235
%Ideal_Low_Pass_Filter.m
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Input Image');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=3:3:9
h=zeros(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
if(d<=d0)
h(u,v)=1;
end
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
%Ideal_Low_Pass_Filter.m
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Input Image');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=3:3:9
h=zeros(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
if(d<=d0)
h(u,v)=1;
end
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
Matlab: Ideal_High_Pass_Filter
%page=235
%Ideal_High_Pass_Filter.m
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=0:2:4
h=ones(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
if(d<=d0)
h(u,v)=0;
end
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
%Ideal_High_Pass_Filter.m
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
%Define Filter function h
t=3;
for kk=0:2:4
h=ones(M,N);
%d0=input('Enter the value of D0=');
d0=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
if(d<=d0)
h(u,v)=0;
end
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
Matlab: DFT image function
function [F]=dft_img(imageA)
[M N]=size(imageA);
imageA=double(imageA);
%-1 *
for x=1:M
for y=1:N
imageA(x,y)=imageA(x,y)*(-1)^(x+y);
end
end
%Perform 2D FFTs
h=waitbar(0,'Calculating DFT...');
for u=0:M-1
for v=0:N-1
sum=0.0;
for x=0:M-1
for y=0:N-1
sum=sum+double(imageA(x+1,y+1))*(exp(-j*2*pi*(u*x/M+v*y/N)));
end
end
F(u+1,v+1)=sum;
end
waitbar(u/M);
end
close(h);
[M N]=size(imageA);
imageA=double(imageA);
%-1 *
for x=1:M
for y=1:N
imageA(x,y)=imageA(x,y)*(-1)^(x+y);
end
end
%Perform 2D FFTs
h=waitbar(0,'Calculating DFT...');
for u=0:M-1
for v=0:N-1
sum=0.0;
for x=0:M-1
for y=0:N-1
sum=sum+double(imageA(x+1,y+1))*(exp(-j*2*pi*(u*x/M+v*y/N)));
end
end
F(u+1,v+1)=sum;
end
waitbar(u/M);
end
close(h);
Matlab: Butterworth_LowPass_Filter
%page=235
%Butterworth_LowPass_Filter.m
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
% subplot(2,2,2);
% imshow(abs(F),[24 100000])
% title('Image A DFT Magnitude');
%
%
% subplot(2,2,3);
% imshow(angle((F)),[-pi pi])
% title('Image A DFT Phase');
%Define Filter function h
t=3;
for kk=3:3:9
h=zeros(M,N);
%d0=input('Enter the value of D0=');
%n=input('Enter the order of n=');
d0=kk;
n=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1/(1+(d/d0)^(2*n));
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
%Butterworth_LowPass_Filter.m
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
% subplot(2,2,2);
% imshow(abs(F),[24 100000])
% title('Image A DFT Magnitude');
%
%
% subplot(2,2,3);
% imshow(angle((F)),[-pi pi])
% title('Image A DFT Phase');
%Define Filter function h
t=3;
for kk=3:3:9
h=zeros(M,N);
%d0=input('Enter the value of D0=');
%n=input('Enter the order of n=');
d0=kk;
n=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1/(1+(d/d0)^(2*n));
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
Matlab: Butterworth_HighPass_Filter
%page=235
%Butterworth_HighPass_Filter
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
% subplot(2,2,2);
% imshow(abs(F),[24 100000])
% title('Image A DFT Magnitude');
% subplot(2,2,3);
% imshow(angle((F)),[-pi pi])
% title('Image A DFT Phase');
%Define Filter function h
t=3;
for kk=2:2:6
h=zeros(M,N);
%d0=input('Enter the value of D0=');
%n=input('Enter the order of n=');
d0=kk;
n=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1/(1+(d0/d)^(2*n));
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
%Butterworth_HighPass_Filter
clc;
clear all;
close all;
imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );
[M N]=size(imageA);
%Display images
subplot(4,2,1:2);
imshow(imageA);
title('Image A ');
F=dft_img(imageA);
% subplot(2,2,2);
% imshow(abs(F),[24 100000])
% title('Image A DFT Magnitude');
% subplot(2,2,3);
% imshow(angle((F)),[-pi pi])
% title('Image A DFT Phase');
%Define Filter function h
t=3;
for kk=2:2:6
h=zeros(M,N);
%d0=input('Enter the value of D0=');
%n=input('Enter the order of n=');
d0=kk;
n=kk;
for u=1:M
for v=1:N
d=((u-M/2)^2+(v-N/2)^2)^(1/2);
h(u,v)=1/(1+(d0/d)^(2*n));
end
end
%Filter show
subplot(4,2,t);
t=t+1;
imshow((h));
title(['Filter H, D0= ',int2str(kk)]);
%multiply
for u=1:M
for v=1:N
outPut_img(u,v)=F(u,v)*h(u,v);
end
end
%Calculating IDFT...
f=idft_img(outPut_img);
%image show
subplot(4,2,t);
t=t+1;
imshow(uint8(f));
title(['Image IDFT D0= ',int2str(kk)]);
end
Matlab: Gradient Using manually convolution 2d
%page=167
%Gradient Using manually convolution 2d
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
A=imread('rice.tif');
subplot(2,2,1);
imshow(A);
title('Original Image');
mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];
%H = conv2(double(A),double(mx));
%V = conv2(double(A),double(my));
H=conv_img2(double(mx),double(A)) ;
%H=corr_img(double(mx),double(A)) ;
V=conv_img2(double(my),double(A));
subplot(2,2,2);
imshow(uint8(H));
title('Horizontal delp/delx');
subplot(2,2,3);
imshow(uint8(V));
title('Vertical delp/dely');
subplot(2,2,4);
imshow(uint8(sqrt(H.^2 + V.^2)));
%imshow(uint8(H));
title('Gradient Image');
%---------function
%convolution of image
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
%Gradient Using manually convolution 2d
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
A=imread('rice.tif');
subplot(2,2,1);
imshow(A);
title('Original Image');
mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];
%H = conv2(double(A),double(mx));
%V = conv2(double(A),double(my));
H=conv_img2(double(mx),double(A)) ;
%H=corr_img(double(mx),double(A)) ;
V=conv_img2(double(my),double(A));
subplot(2,2,2);
imshow(uint8(H));
title('Horizontal delp/delx');
subplot(2,2,3);
imshow(uint8(V));
title('Vertical delp/dely');
subplot(2,2,4);
imshow(uint8(sqrt(H.^2 + V.^2)));
%imshow(uint8(H));
title('Gradient Image');
%---------function
%convolution of image
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
Matlab: Gradient Sobel operation
%page=167
%Gradient Sobel operation
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
subplot(1,2,1);
imshow(A);
title('Original Image');
mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];
[r,c]=size(A);
C=double(A);
for i=1:r-2
for j=1:c-2
%X -direction
Gx1=mx(1,1)*C(i,j)+mx(1,2)*C(i,j+1)+mx(1,3)*C(i,j+2);
Gx2=mx(2,1)*C(i+1,j)+mx(2,2)*C(i+1,j+1)+mx(2,3)*C(i+1,j+2);
Gx3=mx(3,1)*C(i+2,j)+mx(3,2)*C(i+2,j+1)+mx(3,3)*C(i+2,j+2);
Gx=Gx1+Gx2+Gx3;
%Y -direction
Gy1=my(1,1)*C(i,j)+my(1,2)*C(i,j+1)+my(1,3)*C(i,j+2);
Gy2=my(2,1)*C(i+1,j)+my(2,2)*C(i+1,j+1)+my(2,3)*C(i+2,j+2);
Gy3=my(3,1)*C(i+2,j)+my(3,2)*C(i+2,j+1)+my(3,3)*C(i+2,j+2);
Gy=Gy1+Gy2+Gy3;
B(i,j)=uint8(sqrt(Gx.^2+Gy.^2));
end
end
subplot(1,2,2);
imshow(B);
title('Gradient Image');
%Gradient Sobel operation
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
subplot(1,2,1);
imshow(A);
title('Original Image');
mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];
[r,c]=size(A);
C=double(A);
for i=1:r-2
for j=1:c-2
%X -direction
Gx1=mx(1,1)*C(i,j)+mx(1,2)*C(i,j+1)+mx(1,3)*C(i,j+2);
Gx2=mx(2,1)*C(i+1,j)+mx(2,2)*C(i+1,j+1)+mx(2,3)*C(i+1,j+2);
Gx3=mx(3,1)*C(i+2,j)+mx(3,2)*C(i+2,j+1)+mx(3,3)*C(i+2,j+2);
Gx=Gx1+Gx2+Gx3;
%Y -direction
Gy1=my(1,1)*C(i,j)+my(1,2)*C(i,j+1)+my(1,3)*C(i,j+2);
Gy2=my(2,1)*C(i+1,j)+my(2,2)*C(i+1,j+1)+my(2,3)*C(i+2,j+2);
Gy3=my(3,1)*C(i+2,j)+my(3,2)*C(i+2,j+1)+my(3,3)*C(i+2,j+2);
Gy=Gy1+Gy2+Gy3;
B(i,j)=uint8(sqrt(Gx.^2+Gy.^2));
end
end
subplot(1,2,2);
imshow(B);
title('Gradient Image');
Matlab: Gradient Robert cross difference
%page=167
%Gradient Robert cross difference
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
subplot(1,2,1);
imshow(A);
title('Original Image');
operators_x=[-1 0; 0 1];
x_pad=padarray(operators_x,[1 1]);
operators_y=[0 -1; 1 0];
y_pad=padarray(operators_y,[1 1]);
% A =
%
% 1 2
% 3 4
% B = padarray(A,[1 1]);
% B =
%
% 0 0 0 0
% 0 1 2 0
% 0 3 4 0
% 0 0 0 0
[r,c]=size(A);
C=double(A);
for i=1:r-2
for j=1:c-2
%X -direction
Gx=x_pad(3,3)*C(i+2,j+2)+x_pad(2,2)*C(i+1,j+1);
%Y -direction
Gy=y_pad(3,2)*C(i+2,j+1)+y_pad(2,3)*C(i+1,j+2);
B(i,j)=uint8(sqrt(Gx.^2+Gy.^2));
end
end
subplot(1,2,2);
imshow(B);
title('Gradient Image');
%Gradient Robert cross difference
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
subplot(1,2,1);
imshow(A);
title('Original Image');
operators_x=[-1 0; 0 1];
x_pad=padarray(operators_x,[1 1]);
operators_y=[0 -1; 1 0];
y_pad=padarray(operators_y,[1 1]);
% A =
%
% 1 2
% 3 4
% B = padarray(A,[1 1]);
% B =
%
% 0 0 0 0
% 0 1 2 0
% 0 3 4 0
% 0 0 0 0
[r,c]=size(A);
C=double(A);
for i=1:r-2
for j=1:c-2
%X -direction
Gx=x_pad(3,3)*C(i+2,j+2)+x_pad(2,2)*C(i+1,j+1);
%Y -direction
Gy=y_pad(3,2)*C(i+2,j+1)+y_pad(2,3)*C(i+1,j+2);
B(i,j)=uint8(sqrt(Gx.^2+Gy.^2));
end
end
subplot(1,2,2);
imshow(B);
title('Gradient Image');
Matlab: Gradient Using Built in convolution 2d
%page=167
%Using Built in convolution 2d
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
A=imread('rice.tif');
subplot(1,2,1);
imshow(A);
title('Original Image');
mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];
H = conv2(double(A),double(mx));
V = conv2(double(A),double(my));
subplot(1,2,2);
imshow(uint8(sqrt(H.^2 + V.^2)));
title('Gradient Image');
%Using Built in convolution 2d
clc;
clear all;
close all;
A=imread('8.jpg');
A=rgb2gray(A);
A=imread('rice.tif');
subplot(1,2,1);
imshow(A);
title('Original Image');
mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];
H = conv2(double(A),double(mx));
V = conv2(double(A),double(my));
subplot(1,2,2);
imshow(uint8(sqrt(H.^2 + V.^2)));
title('Gradient Image');
Matlab: Unsharp masking, HighBoost Filtering Image, De emphasize Image
%Unsharp masking with k=1
%HighBoost Filtering Image with k=5 (k>1)
%De emphasize Image with k=-5 (k<1 br="">
clear all;
close all;
A=imread('8.jpg');
%img=imread('moon.tif');
A=rgb2gray(A);
subplot(2,3,1);
imshow(A);
title('Original Image');
%to blur image
%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];
blur_img=uint8(conv_img2(L,A));
%end of blur image
subplot(2,3,2);
imshow(blur_img);
title('Blurred Image');
Gmask_img=A-blur_img;
subplot(2,3,3);
imshow(Gmask_img);
title('G mask Image');
k=1;
unsharp_img=A+k*Gmask_img;
subplot(2,3,4);
imshow(unsharp_img);
title('Unsharp Image');
k=5;
highboost_img=A+k*Gmask_img;
subplot(2,3,5);
imshow(highboost_img);
title('HighBoost Filtering Image');
k=-5;
de_emphsize_img=A+k*Gmask_img;
subplot(2,3,6);
imshow(de_emphsize_img);
title('De-emphasizes Image');1>
%---function---------
%convolution of image
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
%---function---------
%correlation function of image
function [out_img]=conv_img(mask,input_img)
img=input_img;
%totals=sum(L(:));
total=9;
[r c]=size(img);
out_img=img;
sum=0;
for i=2:(r-1)
for j=2:(c-1)
sum=0;
s=1;
t=1;
for m=(i-1):(i+1)
for n=(j-1):(j+1)
sum=sum+double( ((img(m,n))*mask(s,t)) );
t=t+1;
end
t=1;
s=s+1;
end
% sum=sum/total;
out_img(i,j)=uint8 (sum);
end
end
%HighBoost Filtering Image with k=5 (k>1)
%De emphasize Image with k=-5 (k<1 br="">
clear all;
close all;
A=imread('8.jpg');
%img=imread('moon.tif');
A=rgb2gray(A);
subplot(2,3,1);
imshow(A);
title('Original Image');
%to blur image
%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];
blur_img=uint8(conv_img2(L,A));
%end of blur image
subplot(2,3,2);
imshow(blur_img);
title('Blurred Image');
Gmask_img=A-blur_img;
subplot(2,3,3);
imshow(Gmask_img);
title('G mask Image');
k=1;
unsharp_img=A+k*Gmask_img;
subplot(2,3,4);
imshow(unsharp_img);
title('Unsharp Image');
k=5;
highboost_img=A+k*Gmask_img;
subplot(2,3,5);
imshow(highboost_img);
title('HighBoost Filtering Image');
k=-5;
de_emphsize_img=A+k*Gmask_img;
subplot(2,3,6);
imshow(de_emphsize_img);
title('De-emphasizes Image');1>
%---function---------
%convolution of image
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
%---function---------
%correlation function of image
function [out_img]=conv_img(mask,input_img)
img=input_img;
%totals=sum(L(:));
total=9;
[r c]=size(img);
out_img=img;
sum=0;
for i=2:(r-1)
for j=2:(c-1)
sum=0;
s=1;
t=1;
for m=(i-1):(i+1)
for n=(j-1):(j+1)
sum=sum+double( ((img(m,n))*mask(s,t)) );
t=t+1;
end
t=1;
s=s+1;
end
% sum=sum/total;
out_img(i,j)=uint8 (sum);
end
end
Matlab: convolution of image
%convolution of image
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
Matlab: correlation function of image function
%correlation function of image
function [out_img]=corr_img(mask,input_img)
img=input_img;
%totals=sum(L(:));
total=9;
[r c]=size(img);
out_img=img;
sum=0;
for i=2:(r-1)
for j=2:(c-1)
sum=0;
s=1;
t=1;
for m=(i-1):(i+1)
for n=(j-1):(j+1)
sum=sum+double( ((img(m,n))*mask(s,t)) );
t=t+1;
end
t=1;
s=s+1;
end
% sum=sum/total;
out_img(i,j)=uint8 (sum);
end
end
function [out_img]=corr_img(mask,input_img)
img=input_img;
%totals=sum(L(:));
total=9;
[r c]=size(img);
out_img=img;
sum=0;
for i=2:(r-1)
for j=2:(c-1)
sum=0;
s=1;
t=1;
for m=(i-1):(i+1)
for n=(j-1):(j+1)
sum=sum+double( ((img(m,n))*mask(s,t)) );
t=t+1;
end
t=1;
s=s+1;
end
% sum=sum/total;
out_img(i,j)=uint8 (sum);
end
end
Matlab: Smoothing spatial filter using manual convulation V2
%Smoothing spatial filter using manual convulation
%page=145
clear all;
close all;
img=imread('8.jpg');
%img=imread('moon.tif');
img=rgb2gray(img);
subplot(1,2,1);
imshow(img);
title('Original Image');
%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];
out_img=uint8(conv_img(L,img));
subplot(1,2,2);
imshow(out_img);
title('Smoothing Image');
%------function--------
%convolution of image
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
%page=145
clear all;
close all;
img=imread('8.jpg');
%img=imread('moon.tif');
img=rgb2gray(img);
subplot(1,2,1);
imshow(img);
title('Original Image');
%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];
out_img=uint8(conv_img(L,img));
subplot(1,2,2);
imshow(out_img);
title('Smoothing Image');
%------function--------
%convolution of image
function [Output] = img_conv2( mask,Input)
v=flipdim(mask,2);
w = flipdim(v,1);
[x y] = size(Input);
[m n]=size(w);
k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);
for i = 1 : x
for j = 1 : y
aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
Output(i,j)= aValue;
end
end
Matlab: Smoothing spatial filter using manual convulation V1
%Smoothing spatial filter using manual convulation
%page=145
clear all;
close all;
img=imread('8.jpg');
%img=imread('moon.tif');
img=rgb2gray(img);
subplot(1,2,1);
imshow(img);
title('Original Image');
%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];
out_img=uint8(conv_img(L,img));
subplot(1,2,2);
imshow(out_img);
title('Smoothing Image');
%----------------Function----------
%convolution of image
function [Output] = img_conv2( mask,Input)
Output=conv2(mask,Input);
%page=145
clear all;
close all;
img=imread('8.jpg');
%img=imread('moon.tif');
img=rgb2gray(img);
subplot(1,2,1);
imshow(img);
title('Original Image');
%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];
out_img=uint8(conv_img(L,img));
subplot(1,2,2);
imshow(out_img);
title('Smoothing Image');
%----------------Function----------
%convolution of image
function [Output] = img_conv2( mask,Input)
Output=conv2(mask,Input);
Subscribe to:
Posts (Atom)