Commit bafb7dbd authored by Richard Torenvliet's avatar Richard Torenvliet

Add a way to store the pca model and add the pca model itself

parent 75db1611
...@@ -10,7 +10,6 @@ from imm_points import IMMPoints, build_feature_vectors, \ ...@@ -10,7 +10,6 @@ from imm_points import IMMPoints, build_feature_vectors, \
flatten_feature_vectors flatten_feature_vectors
def nothing(_): def nothing(_):
pass pass
...@@ -20,6 +19,11 @@ def add_parser_options(): ...@@ -20,6 +19,11 @@ def add_parser_options():
pca_group = parser.add_argument_group('show_pca') pca_group = parser.add_argument_group('show_pca')
pca_group.add_argument(
'--store_pca', action='store_true',
help='number of principle components to keep and are able to manipulate'
)
pca_group.add_argument( pca_group.add_argument(
'--show_pca', action='store_true', '--show_pca', action='store_true',
help='number of principle components to keep and are able to manipulate' help='number of principle components to keep and are able to manipulate'
...@@ -35,6 +39,11 @@ def add_parser_options(): ...@@ -35,6 +39,11 @@ def add_parser_options():
help='number of principle components to keep and are able to manipulate' help='number of principle components to keep and are able to manipulate'
) )
pca_group.add_argument(
'--file', type=str,
help='pca model file that contains or is going to contain the pca model'
)
return parser return parser
...@@ -45,14 +54,35 @@ def init_eigenvalue_trackbars(n_components, s): ...@@ -45,14 +54,35 @@ def init_eigenvalue_trackbars(n_components, s):
cv2.createTrackbar('{}'.format(i), 'eigenvalues', 500, 1000, nothing) cv2.createTrackbar('{}'.format(i), 'eigenvalues', 500, 1000, nothing)
def show_pca(): def store_model(args):
assert args.asf, '--asf files should be supplied' """
Store the U, s, Vt and mean of all the asf datafiles given by the asf
files.
"""
assert args.asf, '--asf files should be given'
assert args.file, '--file needs to be provided to store the pca model'
imm_points = build_feature_vectors(args.asf, flattened=True) imm_points = build_feature_vectors(args.asf, flattened=True)
mean_values = get_mean(imm_points) mean_values = get_mean(imm_points)
U, s, Vt = pca(imm_points, mean_values) U, s, Vt = pca(imm_points, mean_values)
np.save(args.file, np.asarray([U, s, Vt, mean_values]))
def show_pca(args):
assert args.asf, '--asf files should be given to allow the image to be shown'
assert args.file, '--file needs to be provided to get the pca model'
# load the stored model file
UsVtm = np.load(args.file)
U = UsVtm[0]
s = UsVtm[1]
Vt = UsVtm[2]
mean_values = UsVtm[3]
# init trackbars # init trackbars
index = 0 index = 0
cv2.namedWindow('index') cv2.namedWindow('index')
...@@ -73,7 +103,8 @@ def show_pca(): ...@@ -73,7 +103,8 @@ def show_pca():
imm.show_on_img(img) imm.show_on_img(img)
for i in range(n_components): for i in range(n_components):
s_copy[i] = s[i] * ((cv2.getTrackbarPos(str(i), 'eigenvalues') - 500) / 10.0) s_copy[i] = s[i] * (
(cv2.getTrackbarPos(str(i), 'eigenvalues') - 500) / 10.0)
index = cv2.getTrackbarPos('index', 'index') index = cv2.getTrackbarPos('index', 'index')
imm = IMMPoints(filename=args.asf[index]) imm = IMMPoints(filename=args.asf[index])
...@@ -86,9 +117,15 @@ def show_pca(): ...@@ -86,9 +117,15 @@ def show_pca():
cv2.destroyAllWindows() cv2.destroyAllWindows()
if __name__ == '__main__': def main():
parser = add_parser_options() parser = add_parser_options()
args = parser.parse_args() args = parser.parse_args()
if args.show_pca: if args.show_pca:
show_pca() show_pca(args)
elif args.store_pca:
store_model(args)
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment