Anda di halaman 1dari 43

Advanced Image Processing

Practical No:01(A)

AIM: Apply DFT on Image.( Black & white image)

Code:
clc
close all
clear all
a=zeros(30,30);
figure,subplot(2,2,1),imshow(a),title('black image');
a(5:22,17:20)=1;
subplot(2,2,2),imshow(a),title('white image');
Img=double(a);
[r c]=size(Img)
DFTImg=size(Img);
for x=1:r
for y=1:c
Img1(x,y)=Img(x,y)*(-1)^(x+y);
end
end
subplot(2,2,3),imshow(Img1),title('shifted frequency components');
for x=1:r
for y=1:c
DFTImg(x,y)=Img1(x,y)*exp(-1i*2*pi*((x*x)/r)+((y*y)/c));
end
end
subplot(2,2,4),imshow(DFTImg),title('DFT by implementing');

b=fftshift(fft2(a,250,250));
f=abs(b);
F=log(1+f);
figure,subplot(1,2,1),imshow(F),title('DFT using functions');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:01(B)

AIM: Apply DFT on Image.( Gray scale image)

Code:
clc
close all
clear all
a=imread('cameraman.tif')
figure,subplot(2,2,1),imshow(a),title('original image');
Img=double(a);
[r c]=size(Img)
DFTImg=size(Img);
for x=1:r
for y=1:c
Img1(x,y)=Img(x,y)*(-1)^(x+y);
end
end
subplot(2,2,3),imshow(Img1),title('shifted frequency components');
for x=1:r
for y=1:c
DFTImg(x,y)=Img1(x,y)*exp(-1i*2*pi*((x*x)/r)+((y*y)/c));
end
end
subplot(2,2,4),imshow(DFTImg),title('DFT by implementing');

b=fftshift(fft2(a,250,250));
f=abs(b);
F=log(1+f);
figure,subplot(1,2,1),imshow(F),title('DFT using functions');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:01(C)

AIM: Apply DFT on Image.( Colored image)

Code:
clc
close all
clear all
a=imread('Sunset.jpg')
figure,subplot(2,2,1),imshow(a),title('original image');
a1=rgb2gray(a);
subplot(2,2,2),imshow(a1),title('Gray Scale Image');
Img=double(a1);
[r c]=size(Img)
DFTImg=size(Img);
for x=1:r
for y=1:c
Img1(x,y)=Img(x,y)*(-1)^(x+y);
end
end
subplot(2,2,3),imshow(Img1),title('shifted frequency components');
for x=1:r
for y=1:c
DFTImg(x,y)=Img1(x,y)*exp(-1i*2*pi*((x*x)/r)+((y*y)/c));
end
end
subplot(2,2,4),imshow(DFTImg),title('DFT by implementing');

b=fftshift(fft2(a,250,250));
f=abs(b);
F=log(1+f);
figure,subplot(1,2,1),imshow(F),title('DFT using functions');

OUTPUT:
Advanced Image Processing

Practical No:02(A)

AIM: WAP for implementing LPF (Ideal LPF on square image)

Code:
close all;
clear all;
clc;
a=imread('cameraman.tif');
a=double(a);
[row col]=size(a);
D0=input('Enter the cut off Frequency:');

for u=1:1:row
for v=1:1:col
D=(((u-row/2))^2+(v-(col/2))^2)^0.5;
if D<D0;
H(u,v)=1;
else
H(u,v)=0;
end
end
end

vv=fft2(a);
vc=fftshift(vv);
x=vc.*H;
X=abs(ifft2(x));

figure(1);
imshow(uint8(a));
title('original image');

figure(2);
imshow(H);
title('Transform Function');

figure(3);
mesh(H);
title('Frequency response');

figure(4);
imshow(uint8(X));
title('Ideal low pass filtered image');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:02(B)

AIM: WAP for implementing LPF (Butterworth Filter)

Code:
clc
close all
clear all
img=imread('coins.png');
figure,imshow(img),title('original image');
sze=size(img);
rows=sze(1);cols=sze(2);
cutoff=input('Enter,cutoff value(0 to 0.5):');
n=1.0;
x=(ones(rows,1)*(1:cols)-(fix(cols/2)+1))/cols;
y=((1:rows)'*ones(1,cols)-(fix(rows/2)+1))/rows;
radius=sqrt(x.^2+y.^2);
filt=1 ./(1.0+(radius ./cutoff).^(2*n));
f=fftshift(filt);
figure;
surf(filt);
title('Surface plot');
im=double(img);
FFTIm=fft2(im);
him=real(ifft2(FFTIm.*f));
figure,imshow(uint8(him)),title('Butterworth LPF');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:02(C)

AIM: WAP for implementing LPF (Gaussian filter)

Code:
clc;
close all;
clear all;
I=imread('cameraman.tif');

%I=rgb2gray(I);

A=fft2(double(I));
A1=fftshift(A);
[M N]=size(A);
R=10;
X=0:N-1;
Y=0:M-1;
[X Y]=meshgrid(X,Y);
Cx=0.5*N;
Cy=0.5*M;
Lo=exp(-((X-Cx).^2+(Y-Cy).^2)./(2*R).^2);

%Hi=1-Lo;

J=A1.*Lo;
J1=ifftshift(J);
B1=ifft2(J1);

%K=A1.*Hi;
%K1=ifftshift(K);
%B2=ifft2(K1);

figure(1)
imshow(I);colormap gray
title('original image','fontsize',14)

figure(2)
imshow(abs(A1),[-12 300000]);colormap gray
title('fft of original image','fontsize',14)

figure(3)
imshow(abs(B1),[12 290]);colormap gray
title('Low pass filtered image','fontsize',14)

%figure(4)
%imshow(abs(B2),[12 290]);colormap gray
%title('High pass filtered image','fontsize',14)
figure(5)
Advanced Image Processing

mesh(X,Y,Lo)
axis([0 N 0 M 0 1])
h=gca;
get(h,'fontsize')
set(h,'fontsize',14)
title('Gaussian LPF H(f)')

%figure(6)
%mesh(X,Y,Hi)
%axis([0 N 0 M 0 1])
%h=gca;
%get(h,'fontsize')
%set(h,'fontsize',14)
%title('Gaussian HPF H(f)','fontsize',14)
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:03(A)

AIM: WAP for implementing HPF (Ideal HPF on square image)

Code:
close all;
clear all;
clc;
a=imread('cameraman.tif');
a=double(a);
[row col]=size(a);
D0=input('Enter the cut off Frequency:');

for u=1:1:row
for v=1:1:col
D=(((u-row/2))^2+(v-(col/2))^2)^0.5;
if D<D0;
H(u,v)=0;
else
H(u,v)=1;
end
end
end

vv=fft2(a);
vc=fftshift(vv);
x=vc.*H;
X=abs(ifft2(x));

figure(1);
imshow(uint8(a));
title('original image');

figure(2);
imshow(H);
title('Transform Function');

figure(3);
mesh(H);
title('Frequency response');

figure(4);
imshow(uint8(X));
title('Ideal High pass filtered image');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:03(B)

AIM: WAP for implementing HPF (Butterworth Filter)

Code:
clc;
close all;
clear all;
img=imread('mn.tif');
figure,imshow(img),title('original');
sze=size(img);
rows=sze(1);cols=sze(2);
cutoff=input('Enter the cutoff value(0 to .5:)');

n=1.0;
x=(ones(rows,1)*(1:cols)-(fix(cols/2)+1))/cols;
y=((1:rows)'*ones(1,cols)-(fix(rows/2)+1))/rows;
radius=sqrt(x.^2 + y.^2);
filtlow=1./(1.0+(radius./cutoff).^(2*n));
filt=1.0-filtlow;
f=fftshift(filt);

figure;
surf(filt);
title('surface plot');
im=double(img);
FFTIm=fft2(im);
him=real(ifft2(FFTIm.*f));
figure,imshow(uint8(him)),title('butterworth HPF');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:03(C)

AIM: WAP for implementing HPF (Gaussian Filter)

Code:
clc;
close all;
clear all;
I=imread('cameraman.tif');

%I=rgb2gray(I);

A=fft2(double(I));
A1=fftshift(A);
[M N]=size(A);
R=10;
X=0:N-1;
Y=0:M-1;
[X Y]=meshgrid(X,Y);
Cx=0.5*N;
Cy=0.5*M;
Lo=exp(-((X-Cx).^2+(Y-Cy).^2)./(2*R).^2);

Hi=1-Lo;

%J=A1.*Lo;
%J1=ifftshift(J);
%B1=ifft2(J1);

K=A1.*Hi;
K1=ifftshift(K);
B2=ifft2(K1);

figure(1)
imshow(I);colormap gray
title('original image','fontsize',14)

figure(2)
imshow(abs(A1),[-12 300000]);colormap gray
title('fft of original image','fontsize',14)

%figure(3)
%imshow(abs(B1),[12 290]);colormap gray
%title('Low pass filtered image','fontsize',14)

figure(4)
imshow(abs(B2),[12 290]);colormap gray
title('High pass filtered image','fontsize',14)
Advanced Image Processing

%figure(5)
%mesh(X,Y,Lo)
%axis([0 N 0 M 0 1])
%h=gca;
%get(h,'fontsize')
%set(h,'fontsize',14)
%title('Gaussian LPF H(f)')

figure(6)
mesh(X,Y,Hi)
axis([0 N 0 M 0 1])
h=gca;
get(h,'fontsize')
set(h,'fontsize',14)
title('Gaussian HPF H(f)','fontsize',14)
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:04(A)

AIM: WAP for high boost filtering &homomorphic filtering on square


image(Highboost filter)

Code:
clc
clear all
close all
img=imread('cameraman.tif');
imagesc(img); %scaling
axis('square');
colormap(gray);
title('original');
sze=size(img);
rows=sze(1);
cols=sze(2);

%highboost
%cutoff explaination
%if boost is less than 1 then lowboostfilter is generated
cutoff=0.1;
n=1.0;
boost=2;
%x and y matrices with range normalized to +/-0.5
x=(ones(rows,1)*(1:cols)-(fix(cols/2)+1))/cols;
y=((1:rows)' *ones(1,cols) -(fix(rows/2)+1))/rows;
radius=sqrt(x.^2+y.^2);
filtlow=1 ./(1.0+(radius ./cutoff).^(2*n));
filthigh=1.0-filtlow;
filthboost=(1-1/boost)*filthigh+1/boost;
f=fftshift(filthboost); %the filter

figure(2);
surf(filthboost);
title('surface plot');
im=double(img);
FFTIm=fft2(im);
him=real(ifft2(FFTIm.*f)); %apply filter and invert

figure(3);
imagesc(him);
axis('square');
colormap(gray);
title('highboost filter');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:04(B)

AIM: WAP for high boost filtering &homomorphic filtering on square


image(Homomorphic filter).

Code:
clc
clear all
close all
img=imread('spine.tif');
imagesc(img);
axis('square');
colormap('gray');
title('original');
[rows , cols]=size(img);
cutoff=0.1;
n=1.0;
boost=3;
x=(ones(rows,1)*(1:cols)-(fix(cols/2)+1))/cols;
y=((1:rows)'*ones(1,cols)-(fix(rows/2)+1))/rows;
radius=sqrt(x.^2+y.^2);
filtlow=1./(1.0+(radius./cutoff).^(2*n));
filthigh=1.0-filtlow;
filthboost=(1-1/boost)*filthigh+1/boost;
f=fftshift(filthboost);
im=double(img);
FFTlogIm=fft2(log(im+.01));
FFTIm=fft2(im);
him=exp(real(ifft2(FFTlogIm.*f)));
figure(2);
imagesc(him);
axis('square');
colormap('gray');
title('highboost filter');
Advanced Image Processing

OUTPUT:
Advanced Image Processing

Practical No:05

AIM: Acquire satellite/medical image and apply pre-processing techniques to improve


the quality of image (use different low pass filters and compare the results).
Code:
clc
clear all
close all
img=imread('a1.png');
img1=imcrop(img,[0 0 240 240]);
figure,subplot(2,2,1),imshow(img1),title('Original image');
img1=double(img1);

GausFilt=fspecial('gaussian',11,9);
GausImg=filter2(GausFilt,img1);
subplot(2,3,4),imshow(uint8(GausImg)),title('Gaussian');

AvgFilt=fspecial('average',5);
AvgImg=filter2(AvgFilt,img1);
subplot(2,3,5),imshow(uint8(AvgImg)),title('Average');

DiskFilt=fspecial('disk',10);
DiskImg=imfilter(DiskFilt,img1);
subplot(2,3,6),imshow(uint8(DiskImg)),title('Disk');

OUTPUT:
Advanced Image Processing

Practical No:06

AIM: Apply different image enhancement techniques (to improve contrast, brightness,
sharpness) on satellite image
Code:
clc
clear all
close all
img=imread('a1.png');
img1=imcrop(img,[0 0 240 240]);
figure,subplot(2,2,1),imshow(img1),title('Original image');
[r,c]=size(img1);
op=input('Enter 1 to increase brightness or 2 to decrease brightness:');
val=input('Enter value:');
for x=1:r
for y=1:c
if op==1
BrImg(x,y)=img1(x,y)+val;
ifBrImg(x,y)>=255
BrImg(x,y)=255;
end;
elseif op==2
BrImg(x,y)=img1(x,y)- val;
ifBrImg(x,y)<=0
BrImg(x,y)=0;
end;
end;
end;
end;

subplot(2,3,4),imshow(BrImg),title('Brightness adjusted image');


minVal=min(min(BrImg));
maxVal=max(max(BrImg));
newMin=0;
newMax=255;
for x=1:r
for y=1:c
ConImg(x,y)=(BrImg(x,y)-minVal)*((newMax-newMin)/(maxVal-minVal))
+newMin;
end
end

subplot(2,3,5),imshow(ConImg),title('Contrast Streched image');


SharpImage=imsharpen(ConImg);
subplot(2,3,6),imshow(SharpImage),title('Sharpened image');
Advanced Image Processing

OUTPUT:
Advanced Image Processing
Advanced Image Processing

Practical No:07
AIM:Apply different supervised classification techniques to classify satellite image.
Steps:
Task 1: Define new GRASS Location
1.Open GRASS 6.4.3 GUI.In windows this can be found at Start->All Programs-
>QGIS Chugiak->GRASS GIS 6.4.3 GUI. create new folder in your directory.

2.Click 'Location wizard' button on the 'welcome to GRASS GIS' window. this
will open the 'Define new grass location' wizard.
Verify that the GIS data directory points to AIP directory.
Set the project location and location title both to 'Satellite'.
Press Next.
Advanced Image Processing

3.Select 'Select EPSG code of spatial reference system'.


4.Click next.
Advanced Image Processing

5.Search for EPSG code 3309.select NAD27/California Albers.


6.Click next to set the CRS. The 'Select datum transformation' window will appear.
7.choose '1:Used in whole nad27 region' as the datum transformation.
8.Click OK to set datum transformation.

9.Click on finish.
Advanced Image Processing

10. A dialog box named 'Location<Satellite> created' will appear asking if we wish to
set the default region extents and resolution now.click NO.

11.A dialog box 'Create new mapset' will appear asking if we wish to create new
mapset. Enter 'classification' and click on OK.

12. Select 'Classification' from the accessible mapsets list then click on 'Start GRASS'
button. This will open grass GUI.
Advanced Image Processing

Task 2:Import and Group Imagery and Set region.


1.Click File->Import Raster Data ->Common Formats import. this will open the 'Import
raster data' module window.

2.Set the following options.


Advanced Image Processing

3.click 'Import' button and then 'Close' button to close the dialog box.
4.Select layer manager window and select 'Command Console' tab if it is not already
selected. The console displays the result of import function.

5.Click imagery ->Devlope images and group ->Create/edit group. This will open
create/edit imagery groups tool.
Advanced Image Processing

6.Enter satellite as group name in dropdown box. The 'layer in selected group' should
automatically populate with 3 raster maps. If layers do not populate click on Add
button.
7.Check 'Define also sub-group....'
8.Click OK.

9.Click Settings ->Region ->Set Region on layer manager window. This will open the
region tool.
Advanced Image Processing

10.Set the following options:


[multiple] Set region to match this raster map:a1.red@student
11.Click on run. The tool will switch to 'command output' tab. If you do not see any
line that begins with word 'ERROR' Then region has been successfully set.
12.Click Close.

We can perform unsupervised classification.


Advanced Image Processing

Task 3:Create Training Dataset.


1.Vector map ->Create new vector map from the layer manger menu bar. This will open
the 'Create new vector map' dialog.

2.Set the following option given in below screen.


3.click OK.
Advanced Image Processing

4.In layer tree, right click on data@student and choose show attribute data. This will
open 'GRASS GIS Attribute table manager' which allows for display, query,
modification of vector maps attribute table.

5.Click 'Manage Table' tab. This tab allows us to create,rename,remove attribute table
columns.
6.In the ' ADD column' section, create new column with following properties.
a.column:descr
b.Type:varchar
c.Length:10
7.click on Add button if you want to create the column.
Advanced Image Processing

8.click on close button to close the table manager.

9.In layer manager, click 'Add various raster map layers' button then select 'Add RGB
map layer' from the contextual menu.
Advanced Image Processing

10.Set the following attribute in Required tab.


11.Click on apply and then OK to create temporary raster overlay and add it to map
layer list.

12.Drag the data@student map above the raster overlay in map layer list to have vector
map display on the top of raster map.
Advanced Image Processing
Advanced Image Processing

Practical No:08(A)

AIM: Apply DCT and PCA on image.(DCT)


Code:
clc
close all
clear all
img=imread('dng_gmslogo.png');
figure(1);
subplot(1,2,1);
imshow(img);
title('Original image');
img1=double(img);
[r,c]=size(img1);
for x=1:r
for y=1:c
DCTImg(x,y)=img1(x,y)*exp(-1i*2*pi*(x*x+y*y)/x);
end;
end
figure(1);
subplot(1,2,2);
imshow(DCTImg);
title(DCT);

OUTPUT:
Advanced Image Processing

Practical No:08(B)

AIM: Apply DCT and PCA on image.(PCA)


Code:
clc
clear all
close all
A=imread('cameraman.tif');
[m n]=size(A);
A=double(A);
AMean=mean(A);
AStd=std(A);

% STANDARDIZE DATA
B=(A-repmat(AMean,[n 1]))./(repmat(AStd,[n 1]));

%B=zscore(A);
covMat=cov(B);
[PC D]=eig(covMat);
D=diag(D);
[b,I]=sort(-1*D);
D=D(I);
PC=PC(:,I);
Final=B*PC;
Original=((B*PC)*PC').*(repmat(AMean,[n 1]))+(repmat(AStd,[n 1]));

figure(1),imshow(uint8(A)),title('Original image');
figure(2),imshow(Final),title('Image after applying PCA');
figure(3),imshow(uint8(Original)),title('Restored image');
Advanced Image Processing

OUTPUT:

Anda mungkin juga menyukai