0% found this document useful (0 votes)
78 views14 pages

Code of Project Final

The document describes code for classifying pixels in an image based on aggregation pheromone density. It defines training datasets for 6 colonies (Pure Water, Concrete Area, Habitation, Turbid Water, Vegetation, Open Space). The imageHandling.py code preprocesses the image. intensityCal.py calculates pheromone intensity at points. classification.py trains on the datasets and calculates intensities. apdc.py implements the classifier, running it on each pixel to determine the colony label.

Uploaded by

Tezaswi Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views14 pages

Code of Project Final

The document describes code for classifying pixels in an image based on aggregation pheromone density. It defines training datasets for 6 colonies (Pure Water, Concrete Area, Habitation, Turbid Water, Vegetation, Open Space). The imageHandling.py code preprocesses the image. intensityCal.py calculates pheromone intensity at points. classification.py trains on the datasets and calculates intensities. apdc.py implements the classifier, running it on each pixel to determine the colony label.

Uploaded by

Tezaswi Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Implementation of the project :

Language used : Python

Libraries used : OpenCv, numpy, matplotlib, statistics, math

Module used : pickle

Image used :

IRS 1-A image of Calcutta

Code :

1. imageHandling.py : The code in this module is responsible for


manipulation of the image for the project. Various operations
such as image reading, histogram equalization, event handling
on clicking the mouse and image displaying, etc is taken care
of.

The code is as below :


imageHandling.py
import numpy as np

import cv2

def click_event(event,x,y,flags,param):

if event==cv2.EVENT_LBUTTONDOWN:

font=cv2.FONT_HERSHEY_SIMPLEX

coordinate1='('+str(x)+', '+str(y)+') = '+str(grayImage[y][x])

coordinate2='('+str(x)+', '+str(y)+') = '+str(equaImage [y][x])

cv2.putText(grayImage,coordinate1,(x,y),font,.5,(0,0,0),2)
cv2.putText(equaImage,coordinate2,(x,y),font,.5,(0,0,0),2)

cv2.putText( grayImage, coordinate1 cv2.putText((182,182),font,.5,(0,0,0),2)


cv2.imshow('grayImage',grayImage) cv2.imshow('equalizedImage',equaImage)

grayImage=cv2.imread('image.png',0)

colorImage=cv2.imread('image.png')

# Performs Histogram Equalization


equaImage = cv2.equalizeHist(grayImage)

cv2.imshow('grayImage',grayImage)

cv2.imshow('colorImage',colorImage)

cv2.imshow('equalizedImage',equaImage)

cv2.setMouseCallback('colorImage',click_event)

cv2.waitKey(0)

cv2.destroyAllWindows()
intensityCal.py : The code in this file serves as a helper to calculate the
intensity deposited by the ant at a point. In order to do so, various
operations like calculation of mean values, standard deviation, Gaussian
distribution function value are performed to assist the process of
intensity calculation.

The code for intensityCal.py is as under :


from statistics import mean,stdev

from math import sqrt,e,pi


def gaussian_value(point,mu,sigma):
value=e**(-0.5*((point[0]-mu[0])/sigma[0])**2+((point[1]-mu[1])/
sigma[1])**2)/( sqrt(2*pi) *sigma[0]*sigma[1])
return value

def calculate(sample,point,target): length=len(sample)

x_array=[]

y_array=[]

for i in range(length):

x_array.append(sample[i][0])

y_array.append(sample[i][1])

mu_x=mean(x_array)

mu_y=mean(y_array)

mu=(mu_x,mu_y)

sigma_x=stdev(x_array)

sigma_y=stdev(y_array)

sigma=(sigma_x,sigma_y)

gaussian=gaussian_value(point,mu,sigma)
#Calculating pheromone intensity emitted by ant located
at point to target
pheromone_intensity=e**(-sqrt((point[0]-target[0])**2 + (point[1]-
target[1])**2))/(2*pow(gaussian,2))
return pheromone_intensity

2. classification.py : The code in this file is responsible for training


the data set. Here, we declare six different colonies, and the training
and classification of different pixels to their respective colony is
performed here.

The code for classification.py is as under:


import imageHandling

import intensityCal

def intensity(colony,target):

pheromone_intensity=0

size=len(colony)

for i in range(size): point=colony[i]

pheromone_intensity+=intensityCal.calculate(colony,point,target)

return pheromone_intensity

# Training dataset for Pure Water Colony


PW =
[(301,309),(302,309),(302,310),(301,310),(304,309),(328,358),(328,359),(327,35
8),(327,358),(157,129),(157,130),(158,129),(158,130),(159,130), (159,129),
(157,132),(157,133),(157,131),(156,130),(156,132),(75,93),(75,94),(75,95)
,(76,93),(76,94),(76,95),(248,313),(247,313),(247,314),(248,314),(248,315),
(247,315),(33,354),(33,353),(33,352),(33,351),(32,35),(32,351),(34,352),
(15,195),(15,196),(15,197),(16,197), (16,198), (15,198), (14,197)]
# Training dataset for Concrete Area Colony
CA =
[(152,289),(152,290),(152,289),(152,291),(152,292),(153,289),(153,291),(1
78,263),(178,264),(178,265),(178,263),(177,264),(177,263),(177,262),(177
,264),(177,265),(179,263),(177,266),(176,264),(176,264),(147,319),
(147,320),(147,318),(147,317),(146,319),(146,318),(146,320),(146,321),
(114,251),(114,252),(114,250),(114,253),(113,252),(113,251),(113,250),
(115,251),(51,30),(51,31),(51,32),(51,29),(51,33),(52,32),(52,33),(52,30),(5
0,31), (50,32),(50,33),(156,130), (156,131),(156,132),(157,130),(157,131),
(157,133),(214,146),(214,147),(214,145),(213,146),(213,145),(111,264),
(111,265),(111,263),(110,264),(110,265),(273,44),(273,45),(273,43),
(272,44),(272,43),(272,45),(76,313),(76,314),(76,312),(76,315),(75,314),
(75,313),(77,314),(178,285),(178,286),(177,285),(263,28),(264,28),(264,27
),(265,28)]

# Training dataset for Habitation Colony


HAB =
[(282,51),(282,50),(281,50),(282,53),(281,53),(283,51),(282,45),(282,46),(2
82,44),(280,47),(149,276),(149,277),(148,276),(148,275),(145,164),(146,1
63),(145,163),(146,164),(197,240),(197,239),(197,241),(198,240),(83,298),
(83,297),(84,298),(83,299),(329,41),(329,42),(329,43),(329,44)]

# Training dataset for turbid Water Colony


TW =
[(117,113),(117,114),(117,115),(117,116),(116,113),(116,112),(116,114),(1
15,116),(115,117),(115,118),(120,143),(120,144),(120,145),(120,143),(119
,145), (119,146),(143,187),(143,188),(144,186),(142,187),(143,189),(125,
329),(125,330),(126,329),(126,330),(112,351),(111,352),(112,353),(120,12
4),(120,123),(120,125),(116,21),(116,22),(116,20),(116,27),(127,52),(127,5
3),(127,54),(126,53),(126,52)]

# Training dataset for Vegetation Colony


VEG=
[(290,276),(290,277),(291,276),(292,276),(292,277),(291,276),(291,277),(2
91,278),(292,279),(292,276),(70,34),(70,35),(70,36),(70,33),(69,34),(69,35)
,(69,36),(69,38),(69,37),(69,39),(68,38),(68,39),(67,37),(115,214),(115,213)
,(115,212),(115,210),(115,211),(115,216),(115,215),(115,217),(115,218),(2
80,137),(280,138),(280,139),(280,136),(279,138),(279,139),(279,140),(281
,137),(281,138),(8,170),(8,171),(8,169),(8,168),(9,170),(9,171),(5,182),(6,1
83),(218,63),(218,64)]

# Training dataset for Open Space Colony


OS =
[(275,297),(275,298),(275,299),(275,300),(276,297),(276,298),(276,296),(2
72,318),(272,319),(272,320),(271,316),(271,317),(271,218),(40,70),(40,71)
,(40,72),(41,70),(41,71),(39,70),(39,71),(5,352),(5,351),(5,350),(6,350),(6,3
51),(6,352),(6,353),(4,352),(4,351),(4,353),(4,354),(216,158),(216,159),(21
6,160),(215,158),(215,157),(215,159),(215,160),(137,355),(137,354),(137,
356),(136,353),(136,352),(136,351)]

#function to calculate avg aggregation pheromone density at


new_test location for all the colonies
def calculate_aapd(target):
colonies=[PW,CA,HAB,TW,VEG,OS]
number_of_colonies = len(colonies)
colony_names=['PW','CA','HAB','TW','VEG','OS']
avg_intensities=[]
#There are 6 colonies
for i in range(number_of_colonies):
colony=colonies[i]
pheromone_intensity=intensity(colony,target)
print('Total pheromone intensity deposited by ants of colony {} at
location {} is : {}'.format(colony,target,pheromone_intensity))
avg_intensity=pheromone_intensity/len(colony)
avg_intensities.append(avg_intensity)
print('Average pheromone intensity deposited by ants of colony {} at
location {} is : {}'.format(colony,target,avg_intensity))
max_intensity=max(avg_intensities)
colonyLabel=colony_names[avg_intensities.index(max_intensity)]
print(max_intensity)
print('Colony Label : ',colonyLabel)
return colonyLabel
3. apdc.py : The aggregation pheromone based classifier
algorithm is implemented in this file.

The code is for apdc.py as under :


import cv2
import classification
import pickle
image=cv2.imread('image.png',0)
rows=image.shape[0]
cols=image.shape[1]

# Training dataset for Pure Water Colony


PW =
[(301,309),(302,309),(302,310),(301,310),(304,309),(328,358),(328,359),(3
27,358),(327,358),(157,129)
,(157,130),(158,129),(158,130),(159,130),(159,129),(157,132),(157,133),(1
57,131),(156,130),(156,132),(75,93),
(75,94),(75,95),(76,93),(76,94),(76,95),(248,313),(247,313),(247,314),(248,
314),(248,315),(247,315),(33,354),(33,353),(33,352),(33,351),(32,354),(32,
351),(34,352),(15,195),(15,196),(15,197),(16,197),(16,198),(15,198),(14,19
7)]

# Training dataset for Concrete Area Colony


CA =
[(152,289),(152,290),(152,289),(152,291),(152,292),(153,289),(153,291),(1
78,263),(178,264),(178,265),(178,263),(177,264),(177,263),(177,262),
(177,264),(177,265),(179,263),(177,266),(176,264),(176,264),(147,319),
(147,320),(147,318),(147,317),(146,319),(146,318),(146,320), (146,321),
(114,251),(114,252),(114,250),(114,253),(113,252),(113,251), (113,250)
,(115,251),(51,30),(51,31),(51,32),(51,29),(51,33),(52,32),(52,33),(52,30),(5
0,31), (50,32),(50,33),(156,130),(156,131),(156,132),(157,130),(157,131)
,(157,133),(214,146),(214,147),(214,145),(213,146),(213,145),(111,264),(1
11,265),(111,263),(110,264),(110,265),(273,44),(273,45),(273,43),(272,44)
,(272,43),(272,45),(76,313),(76,314),(76,312),(76,315),(75,314),(75,313),(7
7,314),(178,285),(178,286),(177,285),(263,28),(264,28),(264,27),(265,28)]
# Training dataset for Habitation Colony
HAB =
[(282,51),(282,50),(281,50),(282,53),(281,53),(283,51),(282,45),(282,46),(2
82,44),(280,47),(149,276),(149,277),(148,276),(148,275),(145,164),(146,1
63),(145,163),(146,164),(197,240),(197,239),(197,241),(198,240),(83,298),
(83,297),(84,298),(83,299),(329,41),(329,42),(329,43),(329,44)]

# Training dataset for Turbid Water Colony


TW =
[(117,113),(117,114),(117,115),(117,116),(116,113),(116,112),(116,114),(1
15,116),(115,117),(115,118),(120,143),(120,144),(120,145),(120,143),(119
,145),(119,146),(143,187),(143,188),(144,186),(142,187),(143,189),(125,3
29),(125,330),(126,329),(126,330),(112,351),(111,352),(112,353),(120,124
),(120,123),(120,125),(116,21),(116,22),(116,20),(116,27),(127,52), (127,
53),(127,54),(126,53),(126,52)]

# Training dataset for Vegetation Colony


VEG =
[(290,276),(290,277),(291,276),(292,276),(292,277),(291,276),(291,277),(2
91,278),(292,279),(292,276),(70,34),(70,35),(70,36),(70,33),(69,34),(69,35)
,(69,36),(69,38),(69,37),(69,39),(68,38),(68,39),(67,37),(115,214),(115,213)
,(115,212),(115,210),(115,211),(115,216),(115,215),(115,217),(115,218),(2
80,137),(280,138),(280,139),(280,136),(279,138),(279,139),(279,140),(281
,137),(281,138),(8,170),(8,171),(8,169),(8,168),(9,170),(9,171),(5,182),(6,1
83),(218,63),(218,64)]

# Training dataset for Open Space Colony


OS =
[(275,297),(275,298),(275,299),(275,300),(276,297),(276,298),(276,296),(2
72,318),(272,319),(272,320),(271,316),(271,317),(271,218),(40,70),(40,71)
,(40,72),(41,70),(41,71),(39,70),(39,71),(5,352),(5,351),(5,350),(6,350),(6,3
51),(6,352),(6,353),(4,352),(4,351),(4,353),(4,354),(216,158),(216,159),(21
6,160),(215,158),(215,157),(215,159),(215,160),(137,355),(137,354),(137,
356),(136,353),(136,352),(136,351)]
new_pw = []
new_ca = []
new_hab = []
new_tw = []
new_veg = []
new_os = []

#Function to add new off-spring i.e new ant to the


appropriate colony to which it belongs
def add_offspring(new_test,colonyLabel):
if colonyLabel is 'PW':
new_pw.append(new_test)
elif colonyLabel is 'CA':
new_ca.append(new_test)
elif colonyLabel is 'HAB':
new_hab.append(new_test)
elif colonyLabel is 'TW':
new_tw.append(new_test)
elif colonyLabel is 'VEG':
new_veg.append(new_test)
else:
new_os.append(new_test)

#Implementation of Aggregation pheromone density based


classifier
for i in range(rows):
for j in range(cols):
new_test=(j,i)
#Calculate average aggregation pheromone density at location new_test
due to all the ants in their respective colonies
colonyLabel=classification.calculate_aapd(new_test)
#Putting new_test ant to its appropriate colony
add_offspring(new_test,colonyLabel)
new_pw.sort()
new_ca.sort()
new_hab.sort()
new_tw.sort()
new_veg.sort()
new_os.sort()
f=open('pureWater-file.data','wb')
pickle.dump(new_pw,f)
f.close()
f=open('concreteArea-file.data','wb')
pickle.dump(new_ca,f)
f.close()
f=open('habitation-file.data','wb')
pickle.dump(new_hab,f)
f.close()
f=open('turbidWater-file.data','wb')
pickle.dump(new_tw,f)
f.close()
f=open('vegetation-file.data','wb')
pickle.dump(new_veg,f)
f.close()
f=open('openSpace-file.data','wb')
pickle.dump(new_os,f)
f.close()
4. results.py : In this file, the result obtained is displayed. The
percentage of space occupied by different colonies is
calculated and displayed.

The code for results.py is as under:


import pickle
f=open('pureWater-file.data','rb')
pw=pickle.load(f)
print('Co-ordinates belonging to pureWater class :')
print(pw)
f.close()
f=open('concreteArea-file.data','rb')
ca=pickle.load(f)
print('Co-ordinates belonging to concreteArea class :')
print(ca)
f.close()
f=open('habitation-file.data','rb')
hab=pickle.load(f)
print('Co-ordinates belonging to habitation class :')
print(hab)
f.close()
f=open('turbidWater-file.data','rb')
tw=pickle.load(f)
print('Co-ordinates belonging to turbidWater class :')
print(tw)
f.close()
f=open('vegetation-file.data','rb')
veg=pickle.load(f)
print('Co-ordinates belonging to vegetation class :')
print(veg)
f.close()
f=open('openSpace-file.data','rb')
os=pickle.load(f)
print('Co-ordinates belonging to openSpace class :')
print(os)
f.close()
pwSize=len(pw)
caSize=len(ca)
habSize=len(hab)
twSize=len(tw)
vegSize=len(veg)
osSize=len(os)
total=pwSize + caSize + habSize + twSize + vegSize + osSize
print('Number of pixels in pureWater class:',pwSize)
print('Number of pixels in concreteArea class:', caSize)
print('Number of pixels in habitation class : ',habSize)
print('Number of pixels in turbidWater class:',twSize)
print('Number of pixels in vegetation class: ',vegSize)
print('Number of pixels in openSpace class : ',osSize)
print('Total number of pixels : ',total)
pw_percent = (pwSize/total)*100
ca_percent = (caSize/total)*100
hab_percent = (habSize/total)*100
tw_percent = (twSize/total)*100
veg_percent = (vegSize/total)*100
os_percent = (osSize/total)*100
print('Percentage of area occupied by pureWater class : %.2f %%'
%pw_percent)
print('Percentage of area occupied by concreteArea class : %.2f %%'
%ca_percent)
print('Percentage of area occupied by habitation class : %.2f %%'
%hab_percent)
print('Percentage of area occupied by turbidWater class : %.2f %%'
%tw_percent)
print('Percentage of area occupied by vegetation class : %.2f %%'
%veg_percent)
print('Percentage of area occupied by openSpace class : %.2f %%'
%os_percent)
5. Plot.py : In this module, a pie-chart is plotted depicting the
distribution of different colonies in the image.

The code for plot.py is as under :


from results
import pw_percent,ca_percent,hab_percent,
tw_percent,veg_percent,os_percent
import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and


plotted counter-clockwise:
labels = ['PW', 'CA','HAB','TW','VEG','OS']
slices =[pw_percent,ca_percent,hab_percent,
tw_percent,veg_percent,os_percent]
explode = (0, 0.1,0,0,0,0) # only "explode" the 2nd slice (i.e. 'CA')
plt.pie(slices, explode=explode, labels=labels, autopct='%1.2f%%',
shadow=True, startangle=90)
plt.axis('equal')
# Equal aspect ratio ensures that pie is drawn as a
circle.
plt.title('Pie-chart showing colonies distribution')
plt.show()

You might also like