Commit 75db1611 authored by Richard Torenvliet's avatar Richard Torenvliet

Refactored some code and set the trackbar to 0

parent acb7592b
import numpy as np
def build_mean_aam(imm_points):
def get_mean(imm_points):
""" construct a mean from a matrix of x,y values
Args:
imm_points(numpy array) that follows the following structure:
......
import numpy as np
from aam import build_mean_aam
from aam import get_mean
def test_build_mean_aan():
......@@ -14,28 +14,28 @@ def test_build_mean_aan():
[2.5, 5.]
])
mean = build_mean_aam(imm_points)
mean = get_mean(imm_points)
np.testing.assert_array_equal(mean, expected)
def test_zero_mean_aan():
imm_points = np.array([
[[1, 2], [2, 4]],
[[2, 3], [3, 6]],
[1, 2, 2, 4],
[2, 3, 3, 6],
])
expected = np.array([
[[-0.5, -0.5], [-0.5, -1.0]],
[[0.5, 0.5], [0.5, 1.0]],
[-0.5, -0.5, -0.5, -1.0],
[0.5, 0.5, 0.5, 1.0],
])
mean = build_mean_aam(imm_points)
mean = get_mean(imm_points)
zero_mean = imm_points - mean
# test that zero mean has indeed zero mean
np.testing.assert_array_equal(
np.mean(zero_mean, axis=0), np.zeros((2, 2))
np.mean(zero_mean, axis=0), np.zeros((4))
)
np.testing.assert_array_equal(zero_mean, expected)
......@@ -70,6 +70,66 @@ class IMMPoints():
cv2.imshow(window_name, img)
def flatten_feature_vectors(data):
"""
Flattens the feature vectors inside a ndarray
Example:
input:
[
[[1, 2], [3, 4], [5, 6]],
...
[[1, 2], [3, 4], [5, 6]]
]
output:
[
[1, 2, 3, 4, 5, 6],
...
[1, 2, 3, 4, 5, 6]
]
Args:
data (numpy array): array of feature vectors
return:
array: (numpy array): array flattened feature vectors
"""
flattened = []
rows, _, _ = data.shape
for i in range(rows):
flattened.append(np.ndarray.flatten(data[i]))
return np.array(flattened)
def build_feature_vectors(files, flattened=False):
"""
Gets the aam points from the files and appends them seperately to one
array.
Args:
files (list): list files
return:
list: list of feature vectors
"""
imm_points = []
for f in files:
imm = IMMPoints(filename=f)
imm_points.append(imm.get_points())
imm_points = np.array(imm_points)
if flattened:
imm_points = flatten_feature_vectors(imm_points)
return imm_points
def add_parser_options():
parser = argparse.ArgumentParser(description='IMMPoints tool')
......
import numpy as np
from imm_points import flatten_feature_vectors
def test_flatten_feature_vectors():
imm_points = np.array([
[[1, 2], [2, 4]],
[[2, 3], [3, 6]],
])
expected = np.array([
[1, 2, 2, 4],
[2, 3, 3, 6]
])
result = flatten_feature_vectors(imm_points)
np.testing.assert_array_equal(result, expected)
......@@ -5,8 +5,10 @@ import cv2
import numpy as np
from pca import pca, reconstruct
from aam import build_mean_aam
from imm_points import IMMPoints
from aam import get_mean
from imm_points import IMMPoints, build_feature_vectors, \
flatten_feature_vectors
def nothing(_):
......@@ -16,11 +18,23 @@ def nothing(_):
def add_parser_options():
parser = argparse.ArgumentParser(description='IMMPoints tool')
pca_group = parser.add_argument_group('show_pca')
pca_group.add_argument(
'--show_pca', action='store_true',
help='number of principle components to keep and are able to manipulate'
)
# asf files
parser.add_argument(
pca_group.add_argument(
'--asf', nargs='+', help='asf files to process'
)
pca_group.add_argument(
'--n_components', default=5, type=int,
help='number of principle components to keep and are able to manipulate'
)
return parser
......@@ -28,45 +42,38 @@ def init_eigenvalue_trackbars(n_components, s):
cv2.namedWindow('eigenvalues')
for i in range(n_components):
cv2.createTrackbar('{}'.format(i), 'eigenvalues', 0, 1000, nothing)
cv2.createTrackbar('{}'.format(i), 'eigenvalues', 500, 1000, nothing)
if __name__ == '__main__':
parser = add_parser_options()
args = parser.parse_args()
def show_pca():
assert args.asf, '--asf files should be supplied'
if args.asf:
imm_points = []
imm_points = build_feature_vectors(args.asf, flattened=True)
mean_values = get_mean(imm_points)
for f in args.asf:
imm = IMMPoints(filename=f)
imm_points.append(imm.get_points())
# imm.show()
imm_points = np.array(imm_points)
mean_values = build_mean_aam(np.array(imm_points))
U, s, Vt = pca(imm_points, mean_values)
# init trackbars
index = 0
cv2.namedWindow('index')
cv2.createTrackbar('index', 'index', index, len(args.asf) - 1, nothing)
n_components = 5
n_components = args.n_components
init_eigenvalue_trackbars(n_components, s)
# use a copy of s to manipulate so we never touch the original
s_copy = copy.copy(s)
while True:
projection = reconstruct(U, s_copy, Vt, n_components)
X_reconstructed = projection[index].reshape((58, 2)) + mean_values
X_reconstructed = (projection[index] + mean_values).reshape((58, 2))
imm = IMMPoints(points=X_reconstructed)
img = np.full((480, 640, 3), 255, np.uint8)
imm.show_on_img(img)
for i in range(n_components):
s_copy[i] = s[i] * (cv2.getTrackbarPos(str(i), 'eigenvalues') / 10.0)
s_copy[i] = s[i] * ((cv2.getTrackbarPos(str(i), 'eigenvalues') - 500) / 10.0)
index = cv2.getTrackbarPos('index', 'index')
imm = IMMPoints(filename=args.asf[index])
......@@ -77,3 +84,11 @@ if __name__ == '__main__':
break
cv2.destroyAllWindows()
if __name__ == '__main__':
parser = add_parser_options()
args = parser.parse_args()
if args.show_pca:
show_pca()
import numpy as np
def flatten_feature_vectors(data):
"""
Flattens the feature vectors inside.
Example:
input:
[
[[1, 2], [3, 4], [5, 6]],
...
[[1, 2], [3, 4], [5, 6]]
]
output:
[
[1, 2, 3, 4, 5, 6],
...
[1, 2, 3, 4, 5, 6]
]
Args:
data (numpy array): array of feature vectors
return:
array: (numpy array): array flattened feature vectors
"""
flattened = []
y, x, dim = data.shape
for i in range(y):
flattened.append(np.ndarray.flatten(data[i]))
return np.array(flattened)
def pca(data, mean_values):
# subtract mean
zero_mean = data - mean_values
X = flatten_feature_vectors(zero_mean)
_, dim = X.shape
U, s, V = np.linalg.svd(X, full_matrices=False)
U, s, V = np.linalg.svd(zero_mean, full_matrices=False)
return U, s, V
......
import numpy as np
from pca import pca, reconstruct
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