forked from RockStarCoders/alienMarkovNetworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamntools.py
110 lines (96 loc) · 2.76 KB
/
amntools.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
import numpy as np
import matplotlib.pyplot as plt
import colorsys
import pomio
import cv2
"""
Miscellaneous Tools
"""
def readImage( fn ):
img = cv2.imread(fn,-1)
if img == None:
raise IOError( "Image " + fn + " not found" )
if img.ndim == 3:
# opencv does BGR
img = img[:,:,::-1]
return img
def writeImage( fn, img ):
if img.ndim == 3:
# opencv does BGR
img = img.copy()[:,:,::-1]
cv2.imwrite(fn,img)
def _get_colors(num_colors):
colors=[]
for i in np.arange(0., 360., 360. / num_colors):
hue = i/360.
lightness = (50 + np.random.rand() * 10)/100.
saturation = (90 + np.random.rand() * 10)/100.
colors.append(colorsys.hls_to_rgb(hue, lightness, saturation))
return colors
def estimateNeighbourRMSPixelDiff(imgRGB, nhoodSz):
# estimate neighbourhood average diff
sigsq = 0
cnt = 0
rows = imgRGB.shape[0]
cols = imgRGB.shape[1]
# vertical diffs
idiffs = imgRGB[0:rows-1,:,:] - imgRGB[1:rows,:,:]
sigsq += np.power(idiffs,2.0).sum()
cnt += (rows-1)*cols
# horizontal diffs
idiffs = imgRGB[:,0:cols-1,:] - imgRGB[:,1:cols,:]
sigsq += np.power(idiffs,2.0).sum()
cnt += rows*(cols-1)
if nhoodSz == 8:
# diagonal to right
idiffs = imgRGB[0:rows-1,0:cols-1,:] - imgRGB[1:rows,1:cols,:]
sigsq += np.power(idiffs,2.0).sum()
cnt += (rows-1)*(cols-1)
# diagonal to left
idiffs = imgRGB[0:rows-1,1:cols,:] - imgRGB[1:rows,0:cols-1,:]
sigsq += np.power(idiffs,2.0).sum()
cnt += (rows-1)*(cols-1)
sigsq /= cnt
return np.sqrt(sigsq)
def gplotmatrix( X, labels, msize=5, classColours=None, featureNames=None ):
assert X.ndim == 2
D = X.shape[1]
if labels == None:
labels = np.zeros( (X.shape[0]), dtype=int )
assert type(labels)==list or labels.ndim == 1
assert( len(labels) == X.shape[0] )
ULAB = np.unique(labels)
# assume labels are contiguous
lmin = np.min(labels)
lmax = np.max(labels)
plt.clf()
if classColours == None:
classColours = _get_colors(lmax+1)
idx = 1
for r in range(D):
for c in range(D):
plt.subplot(D,D,idx)
idx += 1
x1 = X[:,r]
x2 = X[:,c]
if r==c:
# histogram
plt.hist( x1 )
else:
for l in ULAB:
plt.plot( x2[labels==l], x1[labels==l], '.', \
color=classColours[l],\
markersize=msize)
plt.hold(1)
plt.hold(0)
plt.grid(1)
if c==0:
sl = str(r)
if featureNames != None:
sl += ': ' + featureNames[r]
plt.ylabel( sl )
if r==D-1:
sl = str(c)
if featureNames != None:
sl += ': ' + featureNames[c]
plt.xlabel( sl )