INDEX Sr. No. 01
Date
Topic (a) Transformation of Geometric objects (b) Drawing output primitives
02
Image enhancement
03
Basic Transformation
04
Signature
Different Filters(LPF, HPF, Laplacian,LOG, etc)
05
Plot a Histogram
06
Histogram Equalization
07
Gaussian filter on grey level image
08
Morphological operation on Image
1
2
3
4
5
Practical No: 01
(a)Transformation of Geometric objects Aim: Write a program to apply transformation on geometric objects. a) Translation b) Rotation c) Scaling
Translation: Source Code: #include #include #include void main() { int gdriver = DETECT, gmode, xy[4],t[4]; initgraph(&gdriver, &gmode, "c:\\tc"); setcolor(15); printf("Enter the original coordinates : "); scanf("%d %d %d %d",&xy[0], &xy[1],&xy[2],&xy[3]); printf("Enter the translation factor : "); scanf("%d %d",&t[0], &t[1]); rectangle(xy[0],xy[1],xy[2],xy[3]); outtextxy(xy[0],xy[1]-10, "Original Figure"); rectangle(xy[0]+t[0],xy[1]+t[1],xy[2]+t[0],xy[3]+t[1]); outtextxy(xy[0]+t[0],xy[1]+t[1]-10, "Translated Figure"); getch(); closegraph(); }
6
Output:
7
Rotation: Source Code: #include #include #include #include void main() { int i,gdriver=DETECT,gmode; float x1,y1,x2,y2,x3,y3,rot,theta; clrscr(); initgraph(&gdriver,&gmode,"E:\\TC\\TC\\BGI"); printf("Enter the value of x1,y1,x2,y2::"); scanf("%f%f%f%f",&x1,&y1,&x2,&y2); line(x1,y1,x2,y2); printf("Enter the rotational angle::"); scanf("%f",&theta); rot= (theta/180)*3.14; x3=(x2*cos(rot)) - (y2*sin(rot)); y3=(x2*sin(rot)) + (y2*cos(rot)); line(x1,y1,x3,y3); getch(); closegraph(); }
8
Output:
Enter the value of x1,y1,x2,y2:: 100 200 300 400 Enter the rotational angle:: 50
Scaling: Source Code: #include #include #include void main() { int gdriver = DETECT, gmode, xy[4], crScaled[4]; float scaling; initgraph(&gdriver, &gmode, "c:\\tc"); setcolor(15); printf("Enter the original coordinates : "); 9
scanf("%d %d %d %d",&xy[0], &xy[1],&xy[2],&xy[3]); printf("Enter the scaling factor : "); scanf("%f", &scaling); crScaled[0] = xy[0]+320; crScaled[1] = xy[1]; crScaled[2] = (int)((xy[2] - xy[0]) * scaling) + xy[0]+320; crScaled[3] = (int)((xy[3] - xy[1]) * scaling) + xy[1]; rectangle(xy[0],xy[1],xy[2],xy[3]); outtextxy(10,50, "Original Figure"); line(320,50,320,479); rectangle(crScaled[0],crScaled[1],crScaled[2],crScaled[3]); outtextxy(330,50, "Scaled Figure"); getch(); closegraph(); } Output:
10
11
12
13
14
(a)Drawing Output Primitives Aim: Write a program for drawing output primitives. a) Mid-point circle algorithm b) Bresenham line algorithm
Mid Point Circle Algorithm: Source Code: #include #include #include void main() { float d; int gd,gm,x,y,r; printf("Enter the radius of a circle :"); scanf("%d",&r); detectgraph(&gd,&gm); initgraph(&gd,&gm,"e:\\tc\\bgi"); x = 0,y = r; d = 3 - 2 * r; do { putpixel(200+x,200+y,15); putpixel(200+y,200+x,15); putpixel(200+y,200-x,15); putpixel(200+x,200-y,15); putpixel(200-x,200-y,15); putpixel(200-y,200-x,15); putpixel(200-y,200+x,15); putpixel(200-x,200+y,15); if (d <= 0) d = d + 4*x + 6; else { d = d + 4*(x-y) + 10; y = y - 1; } x = x + 1; }while(x < y); getch(); closegraph(); }
15
Output:
16
Bresenham Line Algorithm: Source Code: #include #include #include #include void main() { float x,y,x1,y1,x2,y2,dx,dy,e; int i,gd,gm; printf("Enter the value of x1 :\t"); scanf("%f",&x1); printf("Enter the value of y1 :\t"); scanf("%f",&y1); printf("Enter the value of x2 :\t"); scanf("%f",&x2); printf("Enter the value of y2 :\t"); scanf("%f",&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI"); dx=abs(x2-x1); dy=abs(y2-y1); x = x1; y = y1; e = 2 * dy-dx; i = 1; do { putpixel(x,y,15); while(e >= 0) { y = y + 1; e = e - 2 * dx; } x = x + 1; e = e + 2 * dy; i = i + 1; }while( i <= dx); getch(); closegraph(); }
17
Output:
18
19
20
21
Practical No: 02
Image enhancement Aim: Image Enhancement a) Thresholding b) Contrast adjustment c) Brightness adjustment d) Gray level slicing Source Code: % Thresholding clear all clc p = imread('cameraman.tif'); a = p; [row col] = size(a); maximum_value = max( max(a) ); maximum_value; row; col; thresholding_value = input('Enter a thresholding value : '); for i=1 : 1 : row for j=1 : 1 : col if (a(i,j) < thresholding_value) a(i,j) = 255; else a(i,j) = 0; end end end subplot(3,2,1), imshow(p) title('Original Image'); subplot(3,2,2), imshow(uint8(a)) title('Thresholding'); %Contast r1=100; s1=150; r2=140; s2=240; l=s1/r1; m=(s2-s1)/(r2-r1); n=(255-s2)/(255-r2); s=size(p); for x=1:s(1); for y=1:s(2); if((p(x,y)>0) && (p(x,y)r1) && (p(x,y)r2) && (p(x,y)<256)) o(x,y)=(n*(p(x,y)-150))+s2; end end
22
end subplot(3,2,3),imshow(o); title('contrast image'); %Brightness B=double(p)+100; subplot(3,2,4) imshow(uint8(B)); title('Brightness Adjusted'); %Graylevel u = imread('cameraman.tif'); a = u; s=size(u); a = input('Enter a value A : '); b = input('Enter a value B : '); for x=1:s(1) for y=1:s(2) if(u(x,y)>=a && (u(x,y)<=b)) g(x,y)=255; else g(x,y)=0; end end end for x=1:s(1) for y=1:s(2) if(u(x,y)>=a && (u(x,y)<=b)) v(x,y)=255; else v(x,y)=u(x,y); end end end subplot(3,2,5),imshow(g); title('graylevel slicing without background'); subplot(3,2,6),imshow(v); title('graylevel slicing with background');
23
Output: Enter a thresholding value : 100 Enter a value A : 50 Enter a value B : 100
24
25
26
Practical No: 03
Basic Transformation Aim: Basic transformations a) Log transformation b) Power law transformation c) Negation Source Code: clear all; clc; img=imread('cameraman.tif'); subplot(3,2,1) imshow(img); title('Original Image'); L=255; c=L/log10(1+L); d = c*(log10(1+ double(img))); a =uint8(d); subplot(3,2,2) imshow(uint8(a)); title('Log Transformed Image'); s=size(img); c1=1; img=double(img); gamma=0.5; for x=1:s(1) for y=1:s(2) j(x,y)=c*(img(x,y)^gamma); end end subplot(3,2,3); imshow(j,[]); title('Power Law Transformed Image'); for x=1:s(1) for y=1:s(2) img(y,x)=255-img(y,x); end end img=uint8(img); subplot(3,2,4); imshow(img); title('Image Negative');
27
Output:
28
29
30
31
Practical No: 04
Different Filters Aim: Different filters(LPF,HPF,Laplacian,LOG,etc)
Low Pass Filter: Source Code: clear all; clc; a=imread('cameraman.tif'); a=double(a); [row col k]=size(a); while(1) rc=input('Enter size of mask'); remain=mod(rc,2); if remain==0 disp('Enter the ODD value'); continue else break end end z=(rc+1)/2; msk=ones(rc)/(rc*rc); a1=a; for d=1:1:k for x=z:1:row-z+1 for y=z:1:col-z+1 for i=1:1:rc for j=1:1:rc a1(x,y,d)=a1(x,y,d)+msk(i,j)*a(x-z+i,y-z+j,d); end end end end end subplot(1,2,1); imshow(uint8(a)); title('original image'); subplot(1,2,2); imshow(uint8(a1)); title('Image after Low Pass Filter');
32
Output: Enter size of mask6 Enter the ODD value Enter size of mask5
33
High Pass Filter: 4b)Source Code: clear all; clc; a=imread('cameraman.tif'); a=double(a); [row col k]=size(a); rc=input('Enter size of mask'); z=(rc+1)/2; msk=ones(rc)/(rc*rc); a1=a; for i=1:1:rc for j=1:1:rc if(i==z)&&(j==z) msk(i,j)=(rc*rc)-1; else msk(i,j)=-1; end end end a1=a; for d=1:1:k for x=z:1:row-z+1 for y=z:1:col-z+1 a1(x,y,d)=0; end end end j=1; for d=1:1:k for x=z:1:row-z+1 for y=z:1:col-z+1 for i=1:1:rc for j=1:1:rc a1(x,y,d)=a1(x,y,d)+msk(i,j)*a(x-z+i,y-z+j,d); end end if a1(x,y,d)<0 a1(x,y,d)=0; end end end end subplot(1,2,1); imshow(uint8(a)); 34
title('original image'); subplot(1,2,2); imshow(uint8(a1)); title('Image after High Pass Filter'); Output: Enter size of mask 5
35
Laplacian Filter 4c)Source Code clc; clear all; close all; m=3; a1=input('enter the value of alpha between 0 and 1='); w=[(a1/4),(1-a1)/4,(a1/4);(1-a1)/4, -1,(1-a1)/4; a1/4,(1-a1)/4, (a1/4)]; w=w*4/(a1+1); aa=imread('cameraman.tif'); a=double(aa); [row,col,d]=size(a); s=(m+1)/2; for k=1:1:d for x=1:1:row for y=1:1:col b(x,y,k)=a(x,y,k); end end end for k=1:1:d for x=s:1:row-s for y=s:1:col-s b(x,y,k)=0; end end end for k=1:1:d for x=s:1:row-s for y=s:1:col-s for i=1:1:m for j=1:1:m b(x,y,k)=a(x-s+i,y-s+j,k)*w(i,j)+b(x,y,k); end end if b(x,y,k)<0 b(x,y,k)=0; end end end end subplot(1,2,1); imshow(uint8(a)); title('original image'); subplot(1,2,2); imshow(uint8(b)); 36
title('Image after Laplacian Filter'); Output: Enter the value of alpha between 0 and 1=0.5
37
Laplacian of Gaussian 4d) Source Code: clc; clear all; a=imread('cameraman.tif'); a=double(a); [row col]=size(a); lap=[0 1 0;1 -4 1; 0 1 0]; m=5; s=m/2; s=floor(m/2); log5=[0 0 -1 0 0;0 -1 -2 -1 0; -1 -2 16 -2 -1;0 -1 -2 -1 0;0 0 -1 0 0]; m=5; s=m/2; s=floor(m/2); for x=3:1:row-2 for y=3:1:col-2 t=1; B(x,y)=0; for i=-s:1:s for j=-s:1:s B(x,y)=B(x,y)+log5(t)*a(x+i,y+j); t=t+1; end end end end subplot(1,2,1); imshow(uint8(a)); title('Original image'); subplot(1,2,2); imshow(uint8(B)); title('Laplacian of Gaussian');
38
Output:
39
40
5) Source Code: clear all; clc; a=imread('cameraman.tif'); a=double(a); [row col]=size(a); h=zeros(1,300); for n=1:1:row for m=1:1:col if a(n,m)==0 a(n,m)=1; end end end for n=1:1:row for m=1:1:col t=a(n,m); h(t)=h(t)+1; end end figure(1); subplot(1,2,1); imshow(uint8(a)); title('Original Image') subplot(1,2,2); bar(h); title('Image histogram');
41
Output:
42
43
44
45
6) Source Code: clear all; clc; a =imread('Cameraman.tif'); a=double(a); big=256; [row col d]=size(a); c=row*col; h=zeros(1,300); z=zeros(1,300); for e=1:1:d for n=1:1:row for m=1:1:col if a(n,m,e)==0 a(n,m,e)=1; end end end end for n=1:1:row for m=1:1:col t=a(n,m); h(t)=h(t)+1; end end pdf=h/c; cdf(1)=pdf(1); for x=2:1:big cdf(x)=pdf(x)+cdf(x-1); end new=round(cdf*big); new=new+1; for r=1:1:d for p=1:1:row for q=1:1:col temp=a(p,q,r); b(p,q,r)=new(temp); t=b(p,q,r); z(t)=z(t)+1; end end end b=b-1; subplot(2,2,1); imshow(uint8(a)); title('original image'); 46
subplot(2,2,2); bar(h); title('Image Histogram'); subplot(2,2,3); imshow(uint8(b)) title('Image after histogram '); subplot(2,2,4); bar(z); title('Image histogram equalization'); Output:
47
48
7) Source Code: clc; clear all; close all; m=input(' enter size'); s=input(' enter value of sigma'); sum1=0; a=m/2; p=0; q=0; r=1; t=1; w=floor(a); for i=-w:w for j=-w:w p=(i*i); q=(j*j); % disp(p); g(r,t)=exp(-(p+q)/(2*s*s)); sum1=sum(sum(g(r,t)+sum1)); t=t+1; % disp(r); % disp(t); end t=1; r=r+1; end disp(g); for r=1:m for t=1:m %disp(sum1); h(r,t)=g(r,t)/sum1; t=t+1; end t=1; r=r+1; end disp(h); img = imread('cameraman.tif'); %img=rgb2gray(im); s1=0; f = double(img); [M N]=size(f); for x=0:M-m for y=0:N-m 49
for s=1:m for z=1:m s1=(h(s,z)* (f(x+s,y+z)))+ s1; end end N_img(x+1,y+1)= s1; s1=0; end end disp(N_img); subplot(1,2,1); imshow(img); title('Original Image'); subplot(1,2,2); imshow(uint8(N_img)); title('Image after Gaussian filtering'); Output:
50
51
8a) Source Code: clc; clear all; BW1=imread('cameraman.tif'); SE=strel('square',3); disp(SE); BW2=imerode(BW1,SE); BW3=imdilate(BW2,SE); figure(1); subplot(1,2,1); imshow(BW1); title('Original image'); subplot(1,2,2); imshow(BW3); title('Morphological opening'); Output:
52
8b) Source Code: clc; clear all; BW1=imread('cameraman.tif'); SE=strel('square',3); disp(SE); BW2=imdilate(BW1,SE); BW3=imerode(BW2,SE); figure(1); subplot(1,2,1); imshow(BW1); title('Original image'); subplot(1,2,2); imshow(BW3); title('Morphological Closing'); Output:
8c) Source Code: clc; 53
clear all; BW1=imread('cameraman.tif'); SE=strel('square',3); disp(SE); BW2=imdilate(BW1,SE); BW3=imerode(BW1,SE); BW4=BW2-BW3; BW5=BW1-BW3; BW6=BW2-BW1; figure(1); subplot(2,2,1); imshow(BW1); title('Original image'); subplot(2,2,2); imshow(BW4); title('Morphological Gradient'); subplot(2,2,3); imshow(BW5); title('Internal Gradient'); subplot(2,2,4); imshow(BW6); title('External Gradient');
54
OUTPUT:
55
8d) Source Code: clc; clear all; BW1=imread('cameraman.tif'); SE=strel('square',3); disp(SE); BW2=imerode(BW1,SE); BW3=imdilate(BW2,SE); BW4=BW1-BW3; figure(1); subplot(1,2,1); imshow(BW1); title('Original image'); subplot(1,2,2); imshow(BW4); title('Top Hat Transform');
56
Output:
57
58