The document describes various image filtering and processing techniques in Matlab code, including maximum, minimum, average, smoothing, median, difference, Prewitt, Sobel, unsharp mask, Robert, and Gaussian filters. It also provides examples of quicksort, perfect number, greatest common divisor (GCD), and palindrome algorithms in Java code, as well as descriptions of the Tower of Hanoi problem and finding the shortest path on a grid.
1. PRACTICAL COMPUTING
CSC305MC3
ASSIGNMENT-01
R LOGARAJAH
2005/SP/30
DEPARTMENT OF COMPUTER SCIENCE
2. Matlab Coding
Filter
1).Maximum filter:-
S
function Img=MaximumFilter(Img)
[row,col]=size(Img);
copyImg=Img;
filter=[1 1 1;1 1 1;1 1 1];
filter1=zeros(3,3);
for x=2:row-1
for y=2:col-1
for i=-1:1
for j=-1:1
p=copyImg(x+i,y+j);
c=filter(j+2,i+2);
filter1(j+2,i+2)=c*p;
end
end
q=max(max(filter1));
Img(x,y)=q;
end
end
I=imread('123.tif');
b=MaximumFilter(I);
subplot(2,2,1); imshow(I);
subplot(2,2,2); imshow(b);
Output:-
3. 2) Minimum filter
function Img=mininumfilter(Img)
[row,col]=size(Img);
copyImg=Img;
filter=[1 1 1;1 1 1;1 1 1];
filter1=zeros(3,3);
for x=2:row-1
for y=2:col-1
for i=-1:1
for j=-1:1
c=copyImg(x+i,y+j);
p=filter(j+2,i+2);
filter1(j+2,i+2)=c*p;
end
end
q=min(min(filter1));
Img(x,y)=q;
end
end
>>I=imread('45.tif');
>> m=mininumfilter(I);
>> subplot(2,2,1);imshow(I);//h
>> subplot(2,2,2);imshow(m);
Output:-
4. 3) Average Filter
function Img=AverageFilter(Img)
[row,col]=size(Img);
copyImg=Img;
for x=2:row-1
for y=2:col-1
sum=0;
for i=-1:1
for j=-1:1
p=copyImg(x+i,y+j);
sum=sum+p;
end
end
q=sum/2;
Img(x,y)=q;
end
end
>> I=imread('11.tif');
>> m=AverageFilter(I);
>>subplot(2,2,1);imshow(I);
>> subplot(2,2,2);imshow(m);
Output:-
5. 4) Smoothing Filter
function Img=Smoothing(Img)
[row,col]=size(Img);
copyImg=Img;
filter=[0.075 0.125 0.175;0.125 0.200 0.125;0.075 0.125 0.175];
for x=2:row-1
for y=2:col-1
sum=0;
for i=-1:1
for j=-1:1
p=copyImg(x+i,y+j);
c=filter(i+2,j+2);
sum=sum+c*p;
end
end
q=sum;
Img(x,y)=q;
end
end
>> I=imread('45.tif');
>> m=Smoothing(I);
>>subplot(2,2,1);imshow(I);
>> subplot(2,2,2);imshow(m);
Output:-
5) Median filter
function Img=MedianFilter(Img)
[row,col]=size(Img);
6. k=4;
p=zeros(2*k+1);
copyImg=Img;
for x=2:row-1
for y=2:col-1
m=1;
for i=-1:1
for j=-1:1
p(m)=copyImg(x+i,y+j);
m=m+1;
end
end
sort(p);
Img(x,y)=p(k);
end
end
>>I=imread('11.tif');
>> m=MedianFilter(I);
>> subplot(2,2,1);imshow(I);
>> subplot(2,2,2);imshow(m);
Output:-
6) Difference Filter
function Img=DifferenceFilter(Img)
[row,column]=size(Img);
CopyImg=Img;
filter=[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];
for x=2:row-1
for y=2:column-1
sum=0;
for i=-1:1
for j=-1:1
7. p=CopyImg(x+i,y+j);
c=filter(i+2,j+2);
sum=sum+c*p;
end
end
q=sum;
Img(x,y)=q;
end
end
>> I=imread('22.tif');
>>Im=DifferenceFilter(I);
>>subplot(1,2,1),imshow(I);
>>subplot(1,2,2),imshow(Im);
Output:-
7) Prewitt Filter
function Img=PrewittFilter(Img)
CopyImg=Img;
Hx=[-1 0 1 ; -1 0 1; -1 0 1];
Hy=[-1 -1 -1; 0 0 0 ; 1 1 1];
Dx=conv2(CopyImg ,Hx);
Dy=conv2(CopyImg ,Hy);
Img=round(sqrt(Dx.*Dx+Dy.*Dy));
end
>>I=imread('44.tif');
>>Im=PrewittFilter(I);
9. function Img=UnsharpMask(Img,a)
CopyImg=Img;
D1=Smoothing(CopyImg);
I=(1+a).*CopyImg;
J=a.*D1;
Img=I-J;
end
I=imread('55.tif');
Im=UnsharpMask(I,0.5);
subplot(1,2,1),imshow(I);
subplot(1,2,2),imshow(Im)
10) Robert filter
function Img=RobertFilter(Img)
CopyImg=Img;
H0=[0 1;-1 0];
H1=[-1 0;0 1];
D0=conv2(CopyImg ,H0);
D1=conv2(CopyImg ,H1);
Img=round(sqrt(D0.*D0+D1.*D1));
end
I=imread('7.tif');
Im=RobertFilter(Im);
subplot(1,2,1),imshow(I);
subplot(1,2,2),imshow(Im)
output:-
11) Gaussian filter
10. ๏ Gaussian kernel.
function kernal=makeGaussian(sigma)
center=3*sigma;
kernal=zeros(2*center+1);
sigma2=sigma*sigma;
for i=1:size(kernal)-1
r=center-i;
kernal(i)=exp(-0.5*(r*r)/sigma2);
end
===================================
function Img=Gussianfilter(Img)
[row,col]=size(Img);
copyImg=Img;
filter=makeGaussian(5);
for x=2:row-1
for y=2:col-1
sum=0;
for i=-1:1
for j=-1:1
c=copyImg(x+i,y+j);
p=filter(j+2,i+2);
sum=sum+c*p;
end
end
q=sum;
Img(x,y)=q;
end
end
I=imread('122.tif');
Im=Gussianfilter(I);
subplot(1,2,1),imshow(I);
subplot(1,2,2),imshow(Im)
output:-
11. Programming in Java
1) Quick Sort in the array elements
import java.util.*;
public class QuickSort
{
static int n;
static int[] array;
Scanner s;
public QuickSort()
{s=new Scanner(System.in);
System.out.print("How many elements :");
n=s.nextInt();
array=new int[n];
}
public void readArray()
{
for (int i=0;i<array.length;i++)
{
System.out.print("array["+i+"] :");
array[i]=s.nextInt();
}
}
public void printArray()
{
System.out.print("The Array Elements are :");
for (int i=0;i<array.length;i++)
{
12. System.out.print(array[i]+" ");
}
System.out.println();
}
public void quickSort(int[] array,int startIndex,int endIndex)
{
if(startIndex<endIndex)
{
int j=partition(startIndex,endIndex);
quickSort(array,startIndex,j-1);
quickSort(array,j+1,endIndex);
}
}
public int partition(int startIndex,int endIndex)
{
int i=startIndex;
int j=endIndex+1;
int pivot=array[startIndex];
do
{
do
{
i=i+1;
}while(array[i]<pivot&&i<endIndex);
do
{
j=j-1;
}while(array[j]>pivot&&j>startIndex);
if(i<j)
{
swap(array,i,j);
}
}while(i<j);
swap(array,startIndex,j);
return j;
}
public void swap(int[] array, int index1, int index2 )
{
13. int tmp = array[index1];
array[index1]=array[index2];
array[index2]=tmp;
}
public static void main(String args[])
{
QuickSort q=new QuickSort();
q.readArray();
q.printArray();
q.quickSort(array,0,array.length-1);
q.printArray();
}
}
Output:-
F:Java>java QuickSort
How many elements :7
array[0] :22
array [1] :11
array [2] :55
array [3] :12
array[4] :99
array[5] :77
array[6] :55
The Array Elements are: 22 11 55 12 99 77 55
The Array Elements are: 11 12 22 55 55 77 99
Quick sort:-
Partition procedure
Step1:- p- startindex.
r- lastidex.
Step2:-If startindex < lastidex then
step3:-q Partition(array,p,r)
Quick Sort (array, p, q)
Quick Sort (array, q + r, r)
Step4:-pivot array[p]
i p
j r+1
step5:-while do
14. Repeat j j-1
Until array[j]โค pivot
Repeat i i+1
until array[i] โฅ pivot
If i < j
then swap(array,i,j)
else return j
step:-stop.
Best Case
Produces two regions of size n/2
T (n)=T(n/2)+T(N/2)+ัฒ(n)
T (n) = ัฒ (n lg n)
Worst case
T(n)= ัฒ (n2)
Average case
T (n) = ัฒ (n lg (n))
Conclusion:-
Compare to quick sort running time, worst case >average case>best case
2)
Import java .util.*;
public class PerfectNumber
{
static int n;
static int pNo = 0;
public static void main(String []args)
15. {
System.out.print("Enter any number =");
Scanner s = new Scanner(System.in);
n= s.nextInt();
System.out.println("PerfectNumber are:");
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{ pNo += i;
System.out.println(i);
}
}
if (pNo == n)
{
System.out.println("number is a perfect number");
}
else
{
System.out.println ("number is not a perfect number");
}
}
}
Compiler and output
C:UserslograjahDesktop>java PerfectNumber
Enter any number =6
PerfectNumber are:
1
2
3
number is a perfect number
16. C:UserslograjahDesktop>java PerfectNumber
Enter any number =100
PerfectNumber are:
1
2
4
5
10
20
25
50
number is not a perfect number
4)
The greatest common divisor (GCD)
Import java.uti1l.*;
Public class GCD
{
Static int a,b;
Public GCD()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter the first number: ");
a=s.nextInt();
System.out.print("Enter the second number: ");
b=s.nextInt();
}
Public int gcd(int a,int b)
{
if(b==0)
return a;
else if(a<b)
return gcd(b,a);
else
17. return gcd(b,a%b);
}
public static void main(String args[])
{
GCD g=new GCD();
System.out.println("The GCD is: "+g.gcd(a,b));
}
}
Output:-
C:UserslograjahDesktop>java GCD
Enter the first number: 12
Enter the second number: 6
The GCD is: 6
C:UserslograjahDesktop>java GCD
Enter the first number: 56
Enter the second number: 48
The GCD is: 8
5) A palindrome is a number or a text phrase that reads the same back and forth.
Import java.util.*;
public class palindrome
{
String word;
String copyword="";
public Stack s=new Stack();
public palindrome()
{
Scanner input=new Scanner(System.in);
System.out.print("Enter the string :-");
word=input.nextLine();
word=word.toUpperCase();
filter();
System.out.println("Given string after ignore special characters is: "+copyword);
for(int i=0;i<copyword.length();i++)
{
s.push((Object)copyword.charAt(i));
}
}
18. public void filter()
{
for(int i=0;i<word.length();i++)
{
char ch=word.charAt(i);
if(ch>=48 && ch<=57 || ch>=65 && ch<=90)
copyword+=ch;
}
}
public void checkpalin()
{
for(int i=0;i<copyword.length();i++)
{
if(s.peek()==(Object)copyword.charAt(i))
s.pop();
else
break;
}
}
public void getResult()
{
if(s.empty())
System.out.println("Given word or phrase is palindrome");
else
System.out.println("Given word or phrase is not palindrome");
}
public static void main(String args[])
{
palindrome p=new palindrome();
p.checkpalin();
p.getResult();
}
}
Output:-
I:Assignment>java palindrome
Enter the string :-radar
Given string after ignore special characters is: RADAR
Given word or phrase is palindrome
19. I:Assignment>java palindrome
Enter the string :-won't lovers revolt now?
Given string after ignore special characters is: WONTLOVERSREVOLTNOW
Given word or phrase is palindrome
6) Tower of Hanoi
function[] = towers(n,source,destination,tmp)
if (n == 1)
fprintf('t move disk 1 source %c destination %c n',source,destination);
else
towers(n-1,source,tmp,destination);
fprintf('t move disk %d source %c destination %c n',n,source,destination);
towers(n-1,tmp,destination,source);
end
>> n= input('Towers of Hanoi: How many disks?n');
fprintf('nn')
towers(n,'A','B','C');
fprintf('nn');
Towers of Hanoi: How many disks?
3
move disk 1 source A destination B
move disk 2 source A destination C
move disk 1 source B destination C
move disk 3 source A destination B
move disk 1 source C destination A
move disk 2 source C destination B
20. move disk 1 source A destination B
7) Give area may be found shortest path
import java.util.*;
class shortpath
{
public static int x1,x2,y1,y2,n,m;
public static int a[][]=new int[10][10],parent[][]=new
int[10][10],dist[][]=new int[10][10];
public static void main(String s[])
{
Scanner s1=new Scanner(System.in);
int x,y,num;
LinkedList l=new LinkedList();
LinkedList path=new LinkedList();
System.out.println("Enter the value for n & m 1st matrix");
n=s1.nextInt();
m=s1.nextInt();
System.out.println("Enter the elements for 1st matrix(0 or 1)");
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
a[i][j]=s1.nextInt();
parent[i][j]=-1;
dist[i][j]=n*m;
}
System.out.println("Enter the position of source(x,y)");
x1=s1.nextInt();
y1=s1.nextInt();
System.out.println("Enter the position of Destination(x,y)");
x2=s1.nextInt();
y2=s1.nextInt();
l.add(getnum(x1,y1));
dist[x1][y1]=0;
while(l.size()!=0)
22. }while(!(x==x1&&y==y1));
while(path.size()!=0)
System.out.println(path.removeLast());
}
public static Integer getnum(int x,int y)
{
return new Integer(x*m+y);
}
}
Output:-
I:Assignment>java shortpath
Enter the value for n & m 1st matrix
4
4
Enter the elements for 1st matrix(0 or 1)
1
2
3
6
5
4
7
8
9
4
5
6
7
8
9
4
Enter the position of source(x,y)
0
0
Enter the position of Destination(x,y)
3
23. 3
(0,0)
(0,1)
(0,2)
(0,3)
(1,3)
(2,3)
(3,3)
10) A car dealer, who sells petrol and diesel car, need to print personalized letters
for customers according to the type of car they bough.
Diesel Car class
public class DieselCar extends Car
{
public DieselCar(String name)
{
super(name);
}
public String toString()
{
return String.format("%s","many thanks for your custom");
}
}
Car class
public class Car
{
String Customername;
public Car(String name)
{
Customername=name;
24. setName(name);
}
public void setName(String name)
{
Customername=name;
}
public String getName()
{
return Customername;
}
public String toString1()
{
return String.format("%s %s, ","Dear",Customername);
}
}
Customer Directory class(main)
import java.util.*;
public class CustomerDirectory
{
static Scanner s=new Scanner(System.in);
static String n;
static int t;
public static void main(String args[])
{
System.out.println("Enter the Name: ");
n=s.nextLine();
System.out.println("Enter the Car type(petrolcar=1 or dieselcar=0):");
t=s.nextInt();
if(t==1)
{PetrolCar pc=new PetrolCar(n);
System.out.println(pc.toString1()+pc.toString());
}
else if(t==0)
{
DieselCar dc=new DieselCar(n);
System.out.println(dc.toString1()+dc.toString());
}
25. else System.out.println("another Type");
}}
Output:-I:Assignment>javac CustomerDirectory.java
I:Assignment>java CustomerDirectory
Enter the Name:
loga
Enter the Car type(petrolcar=1 or dieselcar=0):
1
Dear loga, Please find enclosed a complimentary voucher for the amount of Rs.5000