Skip to content

Commit

Permalink
First Commit - working version
Browse files Browse the repository at this point in the history
  • Loading branch information
sidcode committed Aug 5, 2014
0 parents commit b3329e7
Show file tree
Hide file tree
Showing 77 changed files with 11,967 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Build Instructions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Use command:
python gui.py

in the command line to invoke the application.
49 changes: 49 additions & 0 deletions Classif-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from PIL import Image
import numpy
import pylab
#from PIL import Image
from numpy import *
import tifwork as tw
import scipy.misc


arr=array(Image.open('guiTry.jpeg'))
(row,col,z) = arr.shape
arr2 = numpy.zeros(arr.shape,int)
print arr.shape

print arr[:,:,0].min(),arr[:,:,0].mean(),arr[:,:,0].max()
print arr[:,:,1].min(),arr[:,:,1].mean(),arr[:,:,1].max()
print arr[:,:,2].min(),arr[:,:,2].mean(),arr[:,:,2].max()
# standard deviation from each band for each class will be required
# the deviation will be about the mean of the brightness value of all the bands
# 12 , 54, 67
# mean = 44


classes = [[(0,17),(0,65),(0,57)], [(17,47),(0,65),(0,57)]]
print classes[0][0][1]
print arr[0,0,0] in range(classes[0][0][0],classes[0][0][1])
x = 0
y = 0

for x in range (0,row):
for y in range (0,col):
if ((arr[x,y,0] in range(classes[0][0][0],classes[0][0][1])) and (arr[x,y,1] in range(classes[0][1][0],classes[0][1][1])) and(arr[x,y,2] in range(classes[0][2][0],classes[0][2][1]))):
arr[x,y,:] = (255,0,0)
if ((arr[x,y,0] in range(classes[1][0][0],classes[1][0][1])) and (arr[x,y,1] in range(classes[1][1][0],classes[1][1][1])) and(arr[x,y,2] in range(classes[1][2][0],classes[1][2][1]))):
arr[x,y,:] = (0,255,0)
else:
arr[x,y,:] = (255,255,255)

scipy.misc.imsave('haha.jpg',arr)
'''
[12 , 13
87, 23]
[32 , 13
84, 23]
[62 , 13
87, 23]
'''

90 changes: 90 additions & 0 deletions FastCMeans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Segment N-dimensional grayscale image into c classes using a memory
# efficient implementation of the c-means (aka k-means) clustering
# algorithm. The computational efficiency is achieved by using the
# histogram of the image intensities during the clustering process instead
# of the raw image data.
#
# INPUT ARGUMENTS:
# - im : N-dimensional grayscale image in integer format.
# - c : positive interger greater than 1 specifying the number of
# clusters. c=2 is the default setting. Alternatively, c can be
# specified as a k-by-1 array of initial cluster (aka prototype)
# centroids.
#
# OUTPUT :
# - L : label image of the same size as the input image. For example,
# L==i represents the region associated with prototype C(i),
# where i=[1,k] (k = number of clusters).
# - C : 1-by-k array of cluster centroids.
# - LUT : L-by-1 array that specifies the intensity-class relations,
# where L is the dynamic intensity range of the input image.
# Specifically, LUT(1) corresponds to class assigned to
# min(im(:)) and LUT(L) corresponds to the class assigned to
# max(im(:)). See 'apply_LUT' function for more info.
#

import numpy as np
import LUT2label as label



def FastCMeans(im, c):

# Intensity range

Imin = float(np.min(im[:]))
Imax = float(np.max(im[:]))
I = np.transpose(np.array(np.arange(Imin,Imax+1)))

I = I[:, np.newaxis]

# Compute intensity histogram

H = np.histogram(im.flatten(),I.flatten())
(H1, H2) = H
#H1 = H1.flatten()
#H2 = H2.flatten()
H1 = H1[:, np.newaxis]
#H2 = H2[:, np.newaxis]
#print 'H1 and H2 Size: ', H1.shape, H2.shape
#print H1.shape
H = np.copy(np.append(H1,[1]))
H = H[:, np.newaxis]


# Initialize cluster centroids

if np.size(c) > 1:
C = c
c = np.size(c)
else:
dl = (Imax - Imin)/c
C = np.arange(Imin + dl/2, Imax+1, dl)

# Update cluster centroids

IH = I * H
dC = float(np.inf)

while (dC > 1.0E-6):

C0 = np.copy(C)

# Distance to the centroids
D = np.abs(I - C)

# Classify by proximity
Dmin = np.min(D,1)
LUT = np.argmin(D,1)

for j in range(0,c):
index = LUT==j
C[j] = np.sum(IH[index]) / np.sum(H[index])

# Change in centroids
#print (C-C0)
dC = np.max(np.abs(C-C0))

L = label.LUT2label(im,LUT)
return (L,C,LUT)

153 changes: 153 additions & 0 deletions FastFCMeans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Segment N-dimensional grayscale image into c classes using a memory
# efficient implementation of the fuzzy c-means (FCM) clustering algorithm.
# The computational efficiency is achieved by using the histogram of the
# image intensities during the clustering process instead of the raw image
# data.
#
# INPUT ARGUMENTS:
# - im : N-dimensional grayscale image in integer format.
# - c : positive interger greater than 1 specifying the number of
# clusters. c=2 is the default setting. Alternatively, c can be
# specified as a k-by-1 array of initial cluster (aka prototype)
# centroids.
# - q : fuzzy weighting exponent. q must be a real number greater than
# 1.1. q=2 is the default setting. Increasing q leads to an
# increased amount of fuzzification, while reducing q leads to
# crispier class memberships.
#
# OUTPUT :
# - L : label image of the same size as the input image. For example,
# L==i represents the region associated with prototype C(i),
# where i=[1,k] (k = number of clusters).
# - C : 1-by-k array of cluster centroids.
# - U : L-by-k array of fuzzy class memberships, where k is the number
# of classes and L is the intensity range of the input image,
# such that L=numel(min(im(:)):max(im(:))).
# - LUT : L-by-1 array that specifies the defuzzified intensity-class
# relations, where L is the dynamic intensity range of the input
# image. Specifically, LUT(1) corresponds to class assigned to
# min(im(:)) and LUT(L) corresponds to the class assigned to
# max(im(:)). See 'apply_LUT' function for more info.
# - H : image histogram. If I=min(im(:)):max(im(:)) are the intensities
# present in the input image, then H(i) is the number of image
# pixels/voxels that have intensity I(i).
#


import numpy as np
import matplotlib.pyplot as plt
import tifwork as tw
import scipy.misc
import LUT2label as label
import FastCMeans as fc

def FastFCMeans(im, c, q):

#intensity range

Imin = float(np.min(im[:]))
Imax = float(np.max(im[:]))
I = np.transpose(np.array(np.arange(Imin,Imax+1)))

I = I[:, np.newaxis]

#print 'I size ', I.shape

#print Imin, Imax

# Compute intensity histogram

H = np.histogram(im.flatten(),I.flatten())

(H1, H2) = H

#H1 = H1.flatten()
#H2 = H2.flatten()

H1 = H1[:, np.newaxis]
H2 = H2[:, np.newaxis]

#print 'H1 and H2 Size: ', H1.shape, H2.shape



H = np.copy(np.append(H1,[1]))
H = H[:, np.newaxis]



#print H.shape

#plt.show()
#print H

#Initialize Cluster Centroids

if np.size(c) > 1:
C = c
c = np.size(c)
else:

(l,C,lut) = fc.FastCMeans(im,c)

#Update Fuzzy Memberships and cluster centroids

#I = repmat(I,[1 c])
I = np.tile(I,np.array([1,c]))

#print ' I shape ', I.shape

dC = np.inf

eps = np.finfo(float).eps

#print 'Epsilon is ', eps

while (dC > 1E-6):

C0 = np.copy(C)
#Distance to the centroids
D = np.abs(I-C)
D = D**(2/(q-1)) + eps

#compute fuzzy memberships

recipSum = np.sum(1/D,1)
recipSum = recipSum[:, np.newaxis]


U = D * recipSum

U = 1/(U+ eps)

#update the centroids

UH = (U**q) * H

s1 = np.sum(UH * I,0)
s1 = s1[:,np.newaxis]
s2 = np.sum(UH,0).astype(float)
s2 = s2[:, np.newaxis]

s1 = np.transpose(s1)
s2 = np.transpose(s2)
C = s1 /s2


C = np.sort(C)
#Change in centroids
dC = np.max(np.abs(C-C0))

#Defuzzify and create a label image

Umax = np.max(U,1)
#Umax = Umax[:,np.newaxis]
#print Umax
LUT = np.argmax(U,1)
#print LUT2label

L = label.LUT2label(im,LUT)

return (L,C,U,LUT,H)


58 changes: 58 additions & 0 deletions GUI/back_imageGUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

import Tkinter as tk
from Tkinter import *
from tkMessageBox import *
import ttk
import tkFileDialog
from PIL import Image
import tifwork
import sys
import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.figure import Figure

#from tkFileDialog import askopenfilename

# implement the default mpl key bindings
#import Image, ImageTk


def imdisplay(filename,title):
openImFile(filename,title)

def openImFile(filename,title):
Root1 = tk.Tk()
Root1.wm_title(title)
Root1.geometry("600x600")
fx = Figure(figsize=(5,4), dpi=100)
ax = fx.add_subplot(111)
img=mpimg.imread(filename)
plt.gray()
ax.imshow(img)

canvasx = FigureCanvasTkAgg(fx, master=Root1)
canvasx.show()
canvasx.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg( canvasx, Root1 )
toolbar.update()
canvasx._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

def on_key_event(event):
print('you pressed %s'%event.key)
key_press_handler(event, canvasx, toolbar)

def _quitx():
Root1.quit() # stops mainloop
Root1.destroy()
button1 = tk.Button(master=Root1, text='Quit', command=_quitx)
button1.pack(side=tk.BOTTOM)

fx.canvas.mpl_connect('key_press_event', on_key_event)
Root1.mainloop()

# a tk.DrawingArea
Loading

0 comments on commit b3329e7

Please sign in to comment.