forked from trustdigits/Connected-Component-Labeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.py
136 lines (101 loc) · 3.94 KB
/
graphics.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
##########################################################################################
####
#### Vizualization tools
####
##########################################################################################
import math
import numpy as np
import random
import pylab as pl
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import pyplot as plt
from pylab import *
def MapGenerator(dimx, dimy, n_blobs, elevation, addWhiteNoise = False, noise_level = 0.01, oriented = False):
'''
Compute map with (gaussian) blobs for a map at least 20x20 pixels
If oriented is True, blobs are randomly oriented, and not necessarily parallel to axis.
'''
if(dimx < 20 or dimy < 20):
print 'WARNING: please draw a larger map'
return
Xin, Yin = mgrid[0:dimx, 0:dimy]
mapMat = []
# initialize map with 0 background
for i in range(dimx):
tmp = []
for j in range(dimy):
tmp.append(0.)
mapMat.append(tmp)
# add blobs
rdx = [(int) (math.floor(x * (dimx-1))) for x in np.random.random_sample(n_blobs)]
rdy = [(int) (math.floor(y * (dimy-1))) for y in np.random.random_sample(n_blobs)]
points = zip(rdx, rdy)
volumes = []
for j in range(len(points)):
volumes.append(0.)
k = 0
for center_x, center_y in points:
width_x = math.floor(min([dimx, dimy])/float(10*np.random.randint(2,5))) * (1 + np.random.random())
width_y = math.floor(min([dimx, dimy])/float(10*np.random.randint(2,5))) * (1 + np.random.random())
col = elevation * (1 + (np.random.random()))
if(oriented):
gauss_ = gaussian_oriented(col, center_x, center_y, width_x, width_y)(Xin,Yin)
else:
gauss_ = gaussian(col, center_x, center_y, width_x, width_y)(Xin,Yin)
for i in range(dimx):
for j in range(dimy):
mapMat[i][j] += gauss_[i][j]
volumes[k] += gauss_[i][j]
k += 1
return mapMat, points, volumes
def plotHeatMap(mat):
'''
Plot heatmap with intensity matrix mat (color depends on intensity)
'''
nrows = len(mat)
ncols = len(mat[0])
mat = adjustMat(nrows, ncols,mat)
x = arange(0, nrows+1, 1)
y = arange(0, ncols+1, 1)
X,Y = meshgrid(x,y)
z = [tuple(mat[i]) for i in range(nrows)]
zzip = zip(*z)
pl.pcolor(X,Y,zzip)
pl.colorbar()
pl.axis([0,nrows,0,ncols]) ## modified
pl.show()
def adjustMat(size1, size2, mat):
'''
Shrink matrix to desired size
Zero padding
'''
if(len(mat) >= size1):
mat = mat[:size1]
else: # zero padding
for i in range(len(mat), size1):
mat.append([0]*size2)
matTmp = []
for i in range(size1):
if(len(mat[i])==size2):
matTmp.append([x for x in mat[i]])
elif(len(mat[i])>size2):
tmp = []
for j in range(size2):
tmp.append(mat[i][j])
matTmp.append([x for x in tmp])
else:
tmp = []
for j in range(len(mat[i])):
tmp.append(mat[i][j])
matTmp.append([x for x in tmp]+[0]*(size2-len(mat[i])))
return matTmp
def gaussian(height, center_x, center_y, width_x, width_y):
"""Returns a gaussian function with the given parameters"""
width_x = float(width_x)
width_y = float(width_y)
return lambda x,y: height*np.exp(-(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2)
def gaussian_oriented(height, center_x, center_y, width_x, width_y):
"""Returns a gaussian function with the given parameters"""
width_x = float(width_x)
width_y = float(width_y)
return lambda x,y: height*np.exp(-(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2+np.random.random()*np.random.randint(-1,2)*((center_x-x)/width_x)*((center_y-y)/width_y))/2)