Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 30

/******************************************************************

* PROGRAM NO :1 WAP for Image Negation .


*
* Name : Bhawna
*
* Roll No.:120457186
*
******************************************************************/
int i,j;
for(i=0;i<ImageLength;i++)
{
for(j=0;j<ImageWidthByte;j++)
{
fxyout[i][j]=255-fxy[i][j];
}
}

/******************************************************************
* PROGRAM NO :2 WAP for Histogram Equalization.
*
* Name : Bhawna
*
* Roll No. : 120457186
*
*****************************************************************/
int i,j,k;
float histo[256],sk[256];
long nopix=ImageLength*ImageWidthByte;
for(i=0;i<256;i++)
{
histo[i]=0;
sk[i]=0;
}
for(i=0;i<ImageLength;i++)
{
for(j=0;j<ImageWidthByte;j++)
{
histo[fxy[i][j]]++;
}
}
for(i=0;i<256;i++)
{
histo[i]/=nopix;
for(j=0;j<i;j++)
sk[i]+=histo[j];
sk[i]*=255;
}
for(i=0;i<ImageLength;i++)
2

{
for(j=0;j<ImageWidthByte;j++)
{
fxyout[i][j]= char(sk[int(fxy[i][j])]);
}
}

/*****************************************************************
3

* PROGRAM NO :3 WAP to apply Log Transformation on an image


*
* Name:Bhawna
*
* Roll No.: 120457186
*
*****************************************************************/
int i,j;
for(i=0;i<ImageLength;i++)
{
for(j=0;j<ImageWidthByte;j++)
{
fxyout[i][j]=ceil(255*log(1+fxy[i][j]));
}
}

/******************************************************************
* PROGRAM NO:4 WAP to apply Power Law Transformation on an image*
4

* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/

int i,j;
float gamma;
cout<<"Enter value for Gamma: ";
cin>>gamma;
for(i=0;i<ImageLength;i++)
{
for(j=0;j<ImageWidthByte;j++)
{
fxyout[i][j]=unsigned char(pow(fxy[i][j],gamma));
}
}

/******************************************************************
5

* PROGRAM NO:5 WAP for Contrast Streching of an image


*
* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/
int i,j;
for(i=0;i<ImageLength;i++)
{
for(j=0;j<ImageWidthByte;j++)
{
if(fxy[i][j]>=100 && fxy[i][j]<=150)
{
fxyout[i][j]=fxy[i][j]+50;
}
}
}

/******************************************************************
6

* PROGRAM NO:6 WAP to Sharpen an image using given mask


* -1
* -1
* -1

-1
9
-1

-1
-1
-1

*
*
*

*
*

* Name: Bhawna
* Roll No.: 120457186
*
******************************************************************/
int i,j,r,c;
int sum,div;
unsigned char temp;
int mask[3][3]={-1,-1,-1, -1,9,-1, -1,-1,-1};
cout<<"Mask"<<endl;
for(r=-1;r<2;r++)
{
for(c=-1;c<2;c++)
{
cout<<mask[r+1][c+1];
}
cout<<endl;
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum=0;
div=0;
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
temp=fxy[i+r][j+c];
sum+=int(temp*mask[r+1][c+1]);
}
}
fxyout[i][j]=unsigned char(sum);
}
}
for(i=2;i<ImageLength;i++)
{

for(j=2;j<ImageWidthByte;j++)
7

{
sum=0;
div=0;
for(r=-1;r<2;r++)
{
for(c=-1;c<2;c++)
{
temp=fxy[i+r][j+c];
sum+=int(temp*mask[r+1][c+1]);
}
}
fxyout[i][j]=unsigned char(sum);
}
}

/******************************************************************
* PROGRAM NO:7 WAP to Smooth an image using weighted method
*
8

* using following mask


*
*
1
1
1
1
1
*
*
1
2
2
2
1
*
*
1
2
3
2
1
*
*
1
2
2
2
1
*
*
1
1
1
1
1
*
* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/
int i,j,r,c;
int sum,div;
unsigned char temp;
int mask[5][5]={1,1,1,1,1, 1,2,2,2,1 , 1,2,3,2,1, 1,2,2,2,1, 1,1,1,1,1};
cout<<"Mask"<<endl;
for(r=-2;r<3;r++)
{
for(c=-2;c<3;c++)
{
cout<<mask[r+2][c+2];
}
cout<<endl;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
div=0;
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
temp=fxy[i+r][j+c];
sum+=int(temp*mask[r+2][c+2]);
div+=mask[r+2][c+2];
}
}
fxyout[i][j]=unsigned char(sum/div);
}
}
for(i=2;i<ImageLength;i++)
{
9

for(j=2;j<ImageWidthByte;j++)
{
sum=0;
div=0;
for(r=-2;r<3;r++)
{
for(c=-2;c<3;c++)
{
temp=fxy[i+r][j+c];
sum+=int(temp*mask[r+2][c+2]);
div+=mask[r+2][c+2];
}
}
fxyout[i][j]=unsigned char(sum/div);
}
}

/******************************************************************
* PROGRAM NO:8 WAP to apply Contrast Steching to a low contrast
*
10

* image using formula :


*
*
s= ((r-min)/(max-min))*255
*
*
max= maximum pixel intensity in image
*
*
min= minimum pixel intensity in image
*
*
r=pixel to be processed
*
* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/
double min,max;
min=fxy[0][0];
max=fxy[0][0];
for(i=0;i<ImageLength;i++)
{
for(j=0;j<ImageWidthByte;j++)
{
if(min>fxy[i][j])
{
min=fxy[i][j];
}
if(max<fxy[i][j])
{
max=fxy[i][j]
}
}
printf("%f" , min);
printf("\n %f",max);
for(i=0;i<ImageLength;i++)
{
for(j=0;j<ImageWidthByte;j++)
{
fxyout[i][j]=((fxy[i][j]-min)/(max-min))*255;
}
}
}

/******************************************************************
* PROGRAM NO:9 Write a Program to apply the following Sobel Operators
11

to an image to obtain Gx and Gy. Find the gradientmagnitude as |Gx|+|Gy|


and save in the output image *
*
Gx
Gy
*
*
-1
-2
-1
-1
0
1
*
*
0
0
0
-2
0
2
*
*
-1
-2
-1
-1
0
1
*
* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/
int Gx[3][3]={-1,-2,-1, 0,0,0, -1,-2,-1}, Gy[3][3]={-1,0,1, -2,0,2, -1,0,1};
int i,j,temp=0,temp2=0,sum,sum2,r,c;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum=sum2=0;
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
temp=fxy[i+r][j+c]*Gx[r][c];
temp=temp<0?-temp:temp;
temp2=fxy[i+r][j+c]*Gy[r][c];
temp2=temp2<0?-temp2:temp2;
sum+=temp;
sum2+=temp2;
}
}
fxyout[i][j]=sum+sum2;
}
}
for(i=2;i<ImageLength;i++)
{
for(j=2;j<ImageWidthByte;j++)
{
sum=sum2=0;
for(r=-1;r<2;r++)
{
for(c=-1;c<2;c++)
{
temp=(fxy[i+r][j+c])*(Gx[r+1][c+1]);
temp=temp<0?-temp:temp;
12

temp2=fxy[i+r][j+c]*Gy[r+1][c+1];
temp2=temp2<0?-temp2:temp2;
sum+=temp;
sum2+=temp2;
}
}
fxyout[i][j]=sum+sum2;
}
}

/******************************************************************
* PROGRAM NO:10 Write a Program to apply Discrete Fourier Transform
on an Image
*

13

* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/
int m;
int n=0;
int b;
double a,c1,s1,d,e,f,g,ff;
int fun;
int mask=9;
int u,v,x,y;
double sum;
double pi=4*atan(1.0);
double FR[1024][1024];
double FI[1024][1024];
for(u=0; u<ImageLength; u++)
{
d=u*1.0/ImageLength;
for(v=0; v<ImageLength; v++)
{
e=v*1.0/ImageLength;
FR[u][v]=0;
FI[u][v]=0;
for(x=0; x<ImageLength; x++)
{
f=d*x;
for(y=0; y<ImageLength; y++)
{
g=e*y;
ff=f+g;
c1=cos(2*pi*ff); //Real
s1=sin(2*pi*ff); //Imag
FR[u][v]=FR[u][v] + (c1*fxy[x][y])/(ImageLength*ImageLength);
FI[u][v]=FI[u][v] - (s1*fxy[x][y])/(ImageLength*ImageLength);
}
}
printf(" %f\n ",FI[u][v]);
getch();
}
}
for(x=0; x<ImageLength; x++)
{
d=x*1.0/ImageLength;
14

for(y=0; y<ImageLength; y++)


{
e=y*1.0/ImageLength;
sum=0.0;
for(u=0; u<ImageLength; u++)
{
f=u*d;
for(v=0; v<ImageLength; v++)
{
g=v*e;
ff=f+g; c1=cos(2*pi*ff);//Real
s1=sin(2*pi*ff); //Imag
sum=sum+ FR[u][v]*c1 - FI[u][v]*s1;
}
}
fxyout[x][y]=sum;
}
}

/******************************************************************
* PROGRAM NO:11 Write a Program to resize an image
*
* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/
15

d1=[];
c=imread('im1.tif');
%c=im2uint8(c);
d=imresize(c,0.5);
figure
imshow(d);
[m,n,o]=size(c);
c1=c(:,:,1);
for i=1:m/2
for j=1:m/2
d1(i,j)=c1(2*i,2*j);
end
end
figure
imshow(uint8(d1))
d1;

/******************************************************************
* PROGRAM NO:12 WAP to change the contrast of image.
*
* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/

16

c=imread('im2.tif');
n=[];
[m1,n1]=size(c);
e=double(c)+128;
imshow(uint8(n));
n=255-double(c);
imshow(uint8(n));
%range compression
r=25*log(1+abs(double(c)));
imshow(uint8(r));
%contrast streching
filenam='im2.tif';
J=imadjust(c,strechlim(c),[]);
constrstrch(filename,5,50,50,5,150);
%cs=(g2-g1)/(f2-f1)*(im-f1)+g1
cs=255/200*(double(c)-5)+10;
imshow(uint8(cs))
ih=imhist(c);
plot(ih)
hst=zero(256,1);
for i=1:m1
for j=1:n1
tmp=c(i,j)+1;
hst(tmp)=hst(tmp)+1;
end
end
plot(hst)
eh=histeq(c);
imshow(uint8(eh))

/******************************************************************
* PROGRAM NO:13 WAP for applying filters to image.
*
* Name: Bhawna
*
* Roll No.: 120457186
*
******************************************************************/
c=imread('im2.tif');
imfinfo('im2.tif');
cn=imnoise(c,'gaussian');
17

imshow(uint8(cn));
[m,n]=size(c);
c=double(cn);
for i=1:m-2
for j=1:n-2
ca(i+1,j+1)=mean(mean(cn(1:i+2,j:j+2)));
end
end
imshow(uint8(c))
a=[1/9 1/9 1/9;1/9 1/9 1/9; 1/9 1/9 1/9]
cf2=filter2(a,c,'same');
figure
hold on
imshow(uint8(cf2))
f1=fspeciqal('average');
cf1=filter2(f1,c);
figure
hold on
imshow(uint8(cf2))
%image scaling
maxcf2=max(cf1(:));
mincf2=min(cf1(:));
cf1g=(cf1-mincf2)/(maxcf2-mincf2);
%unsharp masking
p=imread('pelicans.tif');
u=fspecial('unsharp',05);
pu=filter2(u,p);
imshow(p),figure,imshow(uint8(pu))

/******************************************************************
* PROGRAM NO :14 WAP for apply low pass filter
*
* Name : Bhawna
*
* Roll No.:120457186
*
******************************************************************/
int x,y,u,v;
18

double pi,Fr[MAXSIZE][MAXSIZE],Fi[MAXSIZE][MAXSIZE],sumr,sumi;
int M,N;
double t1,t2,temp1;
M=ImageLength;
N=ImageWidthByte;
pi=4.0*atan(1.0);
double fp[MAXSIZE][MAXSIZE],f1[MAXSIZE][MAXSIZE];
for(i=0;i<(2*M);i++)
{
for(j=0;j<(2*N);j++)
{
if(i<M && j<N)
{
fp[i][j]=fxy[i][j];
}
else
{
fp[i][j]=0;
}
}
}
for(i=0;i<(2*M);i++)
{
for(j=0;j<(2*N);j++)
{
fp[i][j]=fp[i][j]*pow(-1, (i+j));
}
}

for(u=0;u<(2*M);u++)
{
for(v=0;v<(2*N);v++)
{
sumr=0.0;
sumi=0.0;
for(x=0;x<(2*M);x++)
{
for(y=0;y<(2*N);y++)
19

{
t1=(double)(u*x)/(2*M);
t2=(double)(v*y)/(2*N);
temp1=(2*pi*(t1+t2));
sumr=sumr+(fp[x][y]*(cos(temp1)));
sumi=sumi-(fp[x][y]*(sin(temp1)));
}
}
Fr[u][v]=sumr;
Fi[u][v]=sumi;
}
cout<<"F";
}
double d,d1=10;
double gr[MAXSIZE][MAXSIZE],gi[MAXSIZE][MAXSIZE];
int f;
for(x=0;x<(2*M);x++)
{
for(y=0;y<(2*N);y++)
{
d=sqrt((x-M)*(x-M)+(y-N)*(y-N));
if(d>d1)
f=0
;
else
f=1;
gr[x][y]=f*Fr[x][y];
gi[x][y]=f*Fi[x][y];

}
}
for(x=0;x<(2*M);x++)
{
for(y=0;y<(2*N);y++)
{
sumr=0.0;
20

sumi=0.0;
for(u=0;u<(2*M);u++)
{
for(v=0;v<(2*N);v++
{
t1=(double)(u*x)/(2*M);
t2=(double)(v*y)/(2*N);
temp1=(2*pi*(t1+t2));
sumr=sumr+(gr[u]
[v]*(cos(temp1))) - (gi[u][v]*(sin(temp1)));
sumi=sumi+(gr[u][v]*(sin(temp1))) + (gi[u][v]*(cos(temp1)));
}
}
f1[x][y]= sumr/(4*M*N);
}
cout<<"I";
}
for(i=0;i<(2*M);i++)
{
for(j=0;j<(2*N);j++)
{
f1[i][j]=f1[i][j]*pow(-1,(i+j));
}
}
int count1=0;
int count2=0;
for(i=0;i<(2*M);i++)
{
for(j=0;j<(2*N);j++)
{
if(i<M && j<N)
{
fxyout[i][j]=(unsigned char)f1[i][j];
if(fxyout[i][j]>255)
count1++;
if(fxyout[i][j]<0)
count2++;
}
21

}
}
cout<<"\nvalues greater than 255 are : "<<count1<<" and values less than 0 are :
"<<count2;

/******************************************************************
* PROGRAM NO :15 WAP for Bit slicing
*
* Name : Bhawna
*
* Roll No.:120457186
*
******************************************************************/

int m;
cout<<"enter plane no:";
cin>>m;
22

m=m-1;
for(i=0; i<ImageLength; i++)
{
for(j=0; j<ImageWidth; j++)
{
for(int k=0;k<m;k++)
{
fxy[i][j]=fxy[i][j]/2;
}
int rem=fxy[i][j]%2;
if(rem==0)
fxyout[i][j]=0;
else
fxyout[i][j]=255;
}
}

/******************************************************************
* PROGRAM NO :16 WAP for Median Filter
*
* Name : Bhawna
*
* Roll No.:120457186
*
******************************************************************/
int i,j,s,t,xps,ypt,m,n,a,b,u,v,choice;
int arr[9];
int M,N;
M=ImageLength;
23

N=ImageWidthByte;
m=n=3;
a=(m-1)/2;
b=(n-1)/2;
cout<<"\n Enter 1 for Median : ";
cin>>choice;
if(choice<0 || choice>1) display_error("Choice no. wrong");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
int k=0;
for(s=-a;s<=a;s++)
{
for(t=-b;t<=b;t++)
{
xps=i+s;
if(xps<0)
xps=0;
if(xps>=M)
xps=M-1;
ypt=j+t;
if(ypt<0)
ypt=0;
if(ypt>=N)
ypt=N-1;
arr[k++]=fxy[xps][ypt];
}
}
int min=arr[0];
int max=arr[0];
if(choice==1)//median
{
// input: salt-pepper.tif(fig 5.10(a))
//output: salt-pepper-median1.tif(fig 5.10(b)) , salt-peppermedian2.tif(fig 5.10(c)),salt-pepper-median1.tif(fig 5.10(c))
for(u=0;u<8;u++)////sorting the array for finding median using selecton
sort..
{
24

for(v=u+1;v<9;v++)
{
if(arr[u]>arr[v])
{
int temp;
temp=arr[u];
arr[u]=arr[v];
arr[v]=temp;
}
}
}
fxyout[i][j]=arr[4];
}
}
}

/******************************************************************
* PROGRAM NO :17 WAP for Laplacian filter
*
* Name : Bhawna
*
* Roll No.:120457186
*
******************************************************************/

int x,y,u,v;

25

double pi,Fr[MAXSIZE][MAXSIZE],Fi[MAXSIZE][MAXSIZE],fp[MAXSIZE]
[MAXSIZE],f1[MAXSIZE][MAXSIZE],gr[MAXSIZE][MAXSIZE],gi[MAXSIZE]
[MAXSIZE],sumr,sumi;
int M,N,P,Q;
double pi2,pi2m,pi2n,c1,c2,c3,c4,c5,c6,c7,c8,theta;
int M2,N2;
M=ImageLength;
N=ImageWidthByte;
M2=M/2;
N2=N/2;
P=2*M;
Q=2*M;
pi=4.0*atan(1.0);
pi2=2*pi;
pi2m=pi2/P;
pi2n=pi2/Q;
double s1,s2,sout1,sout2;
s1=fxy[0][0];
s2=fxy[0][0];
sout1=0;
sout2=1;
for(x=0; x<M; x++)
{
for(y=0; y<N; y++)
{
if(fxy[x][y]<s1)
s1=fxy[x][y];
if(fxy[x][y]>s2)
s2=fxy[x][y];
}
}
for(x=0; x<M; x++)
{
for(y=0; y<N; y++)
{
fxy[x][y]=(unsigned char)(sout1 + (sout2-sout1)/(s2-s1) * (fxy[x][y] - s1));
}
}
for(x=0;x<P;x++)
{
for(y=0;y<Q;y++)
26

{
if(x<M && y<N)
fp[x][y]=fxy[x][y];
else
fp[x][y]=0;
}
}

int dd;
for(i=0;i<P;i++)
{
for(j=0;j<Q;j++)
{
dd=i+j;
fp[i][j]=fp[i][j]*pow(-1,dd);
}
}
//fourier
for(u=0;u<P;u++)
{
c1=pi2m*u;
for(v=0;v<Q;v++)
{
c2=pi2n*v;
sumr=0.0;
sumi=0.0;
for(x=0;x<P;x++)
{
c3=c1*x;
for(y=0;y<Q;y++)
{
c4=c2*y;
theta=c3+c4;
sumr=sumr+(fp[x][y]*(cos(theta)));
sumi=sumi-(fp[x][y]*(sin(theta)));
cout<<"F";
}
}
Fr[u][v]=sumr;
Fi[u][v]=sumi;
}
}
27

double d,d1=10;
int h;
double pi4=(-4*pi*pi);
for(x=0;x<P;x++)
{
c5=(x-M+0.5);
c6=c5*c5;
for(y=0;y<Q;y++)
{
c7=(y-N+0.5);
c8=c7*c7;
d=(c6+c8);
h=(pi4*d);
gr[x][y]=Fr[x][y]*h;
gi[x][y]=Fi[x][y]*h;
}
}
for(x=0;x<P;x++)
{
c1=pi2m*x;
for(y=0;y<Q;y++)
{
c2=pi2n*y;
sumr=0.0;
sumi=0.0;
for(u=0;u<P;u++)
{
c3=c1*u;
for(v=0;v<Q;v++)
{
c4=c2*v;
theta=c3+c4;
sumr=sumr+(gr[u][v]*(cos(theta))) - (gi[u][v]*(sin(theta)));
sumi=sumi+(gr[u][v]*(sin(theta))) + (gi[u][v]*(cos(theta)));
cout<<"I";
}
}
f1[x][y]=(sumr/(4*M*N));
28

}
}
for(i=0;i<(2*M);i++)
{
for(j=0;j<(2*N);j++)
{
dd=i+j;
f1[i][j]=f1[i][j]*pow(-1,dd);
}
}
//double s1,s2,sout1,sout2;
s1=f1[0][0];
s2=f1[0][0];
sout1=-1;
sout2=1;
for(i=0;i<(2*M);i++)
{
for(j=0;j<(2*N);j++)
{
if(i<M && j<N)
{
for(x=0; x<M; x++)
{
for(y=0; y<N; y++)
{
if(f1[x][y]<s1)
s1=f1[x][y];
if(f1[x][y]>s2)
s2=f1[x][y];
}
}
for(x=0; x<M; x++)
{
for(y=0; y<N; y++)
{
f1[x][y]=(unsigned char)(sout1 + (sout2-sout1)/(s2-s1) * (f1[x][y] - s1));
}
}
fxyout[i][j]=(unsigned char)(-f1[i][j]+fxy[i][j]);
29

}
}
}

30

You might also like