Skip to content

Commit

Permalink
Trying graph partitioning.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwa91 committed Nov 12, 2012
1 parent 74ca925 commit 2a40ce2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion community.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def init(self, graph, part = None) :
for node in graph.nodes() :
com = part[node]
self.node2com[node] = com
deg = float(graph.degree(node, weigh = 'weight'))
deg = float(graph.degree(node, weight = 'weight'))
self.degrees[com] = self.degrees.get(com, 0) + deg
self.gdegrees[node] = deg
inc = 0.
Expand Down
12 changes: 3 additions & 9 deletions generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def _create_graph(fvector, xdim, ydim):
dist_max = hypot(xdim, ydim) * 1.0
dist = hypot(pos[0]-pos0[0], pos[1]-pos0[1])

weight = exp(-dot(V, V.T)) * log(dist_max/(1+dist))
#weight = exp(-dot(V, V.T)) * log(dist_max/(1+dist))
weight = exp(-dot(V, V.T)) * (1 - (dist/dist_max))
if weight > 10e-10:
G.add_edge(i, count, weight=weight)
count += 1
Expand All @@ -164,11 +165,4 @@ def create_graph(im, patch_size=8):

return _create_graph(fvector, x, y)
if __name__ == '__main__':
import time
im = imread('ball1.jpg')
print time.clock()
im = _preprocess_image(im)
fvector = _create_feature_vector(im)
x, y, z = im.shape
G = _create_graph(fvector, x, y)
print time.clock()
im = imread('src/ball1.jpg')
28 changes: 18 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

from generator import *
from processor import *
from canny import Canny
import metis

patch_size = 8
imname = 'flower.jpg'
imname = 'src/new_image.jpg'
im = imread(imname)
im_edge = Canny(imname, 1).grad[1:-1, 1:-1]
im_high = empty_like(im)
im_high[:,:,0] = im_edge[:,:]
im_high[:,:,1] = im_edge[:,:]
im_high[:,:,2] = im_edge[:,:]
G = create_graph(gaussian_filter(im, 2)+im_high,patch_size)
community = process_graph(G)
save_community_snapshot(im, G, community, patch_size)
imtemp = im.copy()
print 'Starting graph processing'
G = create_graph(im,patch_size)
comm, partition = process_graph(G)
save_community_snapshot(im, G, comm, patch_size)
node_list = [d for n,d in G.nodes_iter(data=True)]
print 'Level Zero done.'
(edgecuts, parts) = metis.part_graph(G, 2, recursive = True)

for i in range(len(parts)):
if parts[i]==1:
x, y = G.node[i]['pos']
t = patch_size // 2
imtemp[x-t:x+t, y-t:y+t] += 50
Image.fromarray(imtemp).convert('RGB').save('partition_check.jpg')
2 changes: 1 addition & 1 deletion notes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
1. The bounding box is a horrible misnomer
2. Distance penality should be considered. Need to choose linear or exponential

3. Trying METIS for graph partitioning
2 changes: 1 addition & 1 deletion processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def process_graph(imgraph):
for i in range(n_nodes):
communities[partition[i]].append(i)

return communities
return communities, partition

def _draw_line(im, point1, point2):
"""
Expand Down

0 comments on commit 2a40ce2

Please sign in to comment.