-
Notifications
You must be signed in to change notification settings - Fork 2
/
pca_statistical_shape_model.py
176 lines (134 loc) · 5.61 KB
/
pca_statistical_shape_model.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 7 08:54:23 2020
@author: ms
"""
import os
import numpy as np
import scipy.linalg
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from read_files import *
from my_interpolate import *
DATASET = 'HAND'
def get_centroids(points):
""" obtain centroid of LV cardiac dataset which consists of two
circles each containing 33 points """
c1 = np.mean(points[:33],axis = 0)
c2 = np.mean(points[33:],axis = 0)
return c1,c2
def unmake_1d(hand):
return np.array([ [hand[i], hand[i+56]] for i in range(0,hand.shape[0]//2)])
def make_1d(hand):
return np.concatenate((hand[:,0],hand[:,1])).reshape(-1)
def showImg(filename,show = False):
plt.imshow(mpimg.imread(filename))
plt.axis('off')
if show:
plt.show()
def showPoints(points,W=256,H=256, show = False,color = 'white'):
points = np.array(points)
plt.scatter(points[:,0]*W,points[:,1]*H,color=color,s = 1)
if show:
plt.show()
def showSegImg(imgpath,points,W = None,H = None):
if W is None:
W,H = getImageWH(imgpath)
showImg(imgpath)
if DATASET == 'HAND':
pass
else:
showInterp(interp(points[:33]),W,H)
showInterp(interp(points[33:]),W,H)
showPoints(points,W,H,True)
def showCentroids(centroids,W=256,H=256):
plt.scatter(centroids[:,0,0]*W,centroids[:,0,1]*H,marker = '4',color = 'black')
plt.scatter(centroids[:,1,0]*W,centroids[:,1,1]*H,marker = '4',color = 'black')
plt.axis('off')
def showPCAModes(mean_centre, mode ,title = None):
mean_center_in = mean_centre.reshape(66,-1)[:33]
mean_center_out = mean_centre.reshape(66,-1)[33:]
ax1 = plt.subplot(1,2,1)
showInterp(interp(mean_center_in),marker = 'r')
showInterp(interp(mean_center_out),marker = 'r')
showInterp(interp(mean_center_in + mode.reshape(66,-1)[:33]),marker = 'b')
showInterp(interp(mean_center_out + mode.reshape(66,-1)[:33]),marker = 'b')
plt.subplot(1,2,2, sharex = ax1,sharey = ax1)
showInterp(interp(mean_center_in),marker = 'r')
showInterp(interp(mean_center_out),marker = 'r')
showInterp(interp(mean_center_in - mode.reshape(66,-1)[33:]),marker = 'g')
showInterp(interp(mean_center_out - mode.reshape(66,-1)[33:]),marker = 'g')
if title:
plt.suptitle(title)
plt.show()
def procrustes_hand(hands):
np.testing.assert_equal(make_1d(unmake_1d(hands[0])),hands[0])
normalized_hands = hands
old_normalized_hands = hands
fig = plt.figure()
for hand in normalized_hands:
showInterp(interp(unmake_1d(hand)))
plt.title('Before Procrustes Alignment')
plt.show()
for count in range(5):
mean_hand = np.mean(normalized_hands,axis = 0)
for i,hand in enumerate(hands):
_, mtx, disparity = scipy.spatial.procrustes(unmake_1d(mean_hand),
unmake_1d(hand))
normalized_hands[i] = make_1d(mtx)
fig = plt.figure()
for hand in normalized_hands:
showInterp(interp(unmake_1d(hand)))
plt.title('After Procrustes Alignment')
plt.show()
return normalized_hands
def main():
filepath = './ssm_datasets/hand/all/shapes'
segmentationlist = readSegmentations(filepath,getxy)[0]
hands = np.array(segmentationlist).T
showSegImg(os.path.join(filepath,'0000.jpg'),unmake_1d(hands[0]),600,600)
normalized_hands = procrustes_hand(hands)
mean_normalized_hand = np.mean(normalized_hands,axis = 0)
cov_mat = np.cov(normalized_hands.T)
eig_val, eig_vec = np.linalg.eigh(cov_mat)
m = unmake_1d(mean_normalized_hand)
for i in range(1,5):
modeminus = unmake_1d(eig_vec[:,-i]*-3*np.sqrt(eig_val[-i]))+unmake_1d(mean_normalized_hand)
modeplus = unmake_1d(eig_vec[:,-i]*3*np.sqrt(eig_val[-i]))+unmake_1d(mean_normalized_hand)
fig = plt.figure(figsize =(11,3))
ax1 = plt.subplot(131)
showInterp(interp(modeminus),marker = 'b')
plt.subplot(132,sharex = ax1, sharey = ax1)
showInterp(interp(m))
plt.subplot(133,sharex = ax1, sharey = ax1)
showInterp(interp(modeplus),marker = 'g')
plt.suptitle('PCA Mode' + str(i))
plt.show()
def lv_cardiac_pca():
filepath = './ssm_datasets/lv_cardiac/data'
segmentationlist = readSegmentations(filepath)
lv_cardiac = np.array([np.array(segment) for segment in segmentationlist])
mean_lv_cardiac = np.mean(lv_cardiac, axis = 0)
showSegImg(imgname_from_segfilename(filepath,'c4480h_s1.asf'),
lv_cardiac[0].reshape(-1,2))
mean_centroids = np.array([get_centroids(mean_lv_cardiac.reshape(-1,2))])
centroids = np.array([get_centroids(segment) for segment in segmentationlist ])
diff1 = centroids[:,0,:] - mean_centroids[:,0,:]
centred1 = lv_cardiac[:,:33,:] - diff1.reshape(14,1,2)
diff2 = centroids[:,1,:] - mean_centroids[:,1,:]
centred2 = lv_cardiac[:,33:,:] - diff2.reshape(14,1,2)
centred = np.concatenate((centred1.reshape(14,-1),centred2.reshape(14,-1)),axis = 1)
_cov_mat = np.cov(centred.T)
mean_centred = np.mean(centred, axis = 0)
eig_val, eig_vec = scipy.linalg.eigh(_cov_mat)
for i in range(1,5):
mode = eig_vec[:,-i] * 3 * np.sqrt(eig_val[-i])
showPCAModes(mean_centred,mode,"PCA Major Mode "+ str(i))
for c1,c2 in zip(centred1,centred2):
showInterp(interp(c1),marker = 'b')
showInterp(interp(c2))
plt.title('Training Data LV Segmentation')
plt.show()
if __name__ == '__main__':
main()