-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmeanswork.py
44 lines (30 loc) · 926 Bytes
/
kmeanswork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from spectral import kmeans
from PIL import Image
import scipy.misc
import tifwork
import numpy as np
import sys
sys.path.append('../GUI/')
import imageGUI
def kmea(colorArray,iterations,fileName):
print 'Kmeans'
print colorArray
print iterations
print fileName
dataset = tifwork.openTIF(fileName)
(cols,rows,bands,bandArr) = tifwork.detailsTIF(dataset)
bandArr = tifwork.getBand(dataset,bands,bandArr)
imageArray = np.array(bandArr,dtype =float)
clusters = len(colorArray)
print 'clusters = ' , clusters
#iterations = 3
(clusterNumber, colorArr) = kmeans(imageArray,clusters,iterations)
#print colorArray.shape
#print clusterNumber
clusteredArray = np.zeros((rows,cols,3))
for i in range(clusters):
index = clusterNumber == i
clusteredArray[index] = colorArray[i]
scipy.misc.imsave('kMeans.jpg',clusteredArray)
imageGUI.imdisplay('kMeans.jpg','Kmeans-Image')
print 'kmeans done'