def preprocess(trainingSamples): global MS, IS XS = l.create_partial_XS(trainingSamples) MS = np.zeros((c.get_nb_teeth(), c.get_nb_dim())) IS = np.zeros((c.get_nb_teeth(), c.get_nb_dim())) for j in range(c.get_nb_teeth()): S = XS[j, :, :] M, Y = pa.PA(S) MS[j, :] = M mtx = mty = ms = mtheta = 0 n = S.shape[0] for i in range(n): tx, ty, s, theta = mu.full_align_params( M, fu.original_to_cropped(S[i, :])) mtx += tx mty += ty ms += s mtheta += theta n = float(n) mtx /= n mty /= n ms /= n mtheta /= n IS[j, :] = mu.full_align(M, mtx, mty, ms, mtheta)
def preprocess(trainingSamples): ''' Creates MS, EWS and fs, used by the fitting procedure * MS contains for each tooth, the tooth model (in the model coordinate frame) * EWS contains for each tooth, a (sqrt(Eigenvalues), Eigenvectors) pair (in the model coordinate frame) * fitting function for each tooth, for each landmark. ''' global MS, EWS, fns, fts XS = l.create_partial_XS(trainingSamples) MS = np.zeros((c.get_nb_teeth(), c.get_nb_dim())) for j in range(c.get_nb_teeth()): S = XS[j, :, :] M, Y = pa.PA(S) MS[j, :] = M E, W, MU = pca.pca_percentage(Y) EWS.append((np.sqrt(E), W)) GNS, GTS = ff.create_partial_GS_for_multiple_levels(trainingSamples, XS, MS, (max_level + 1), offsetX=fu.offsetX, offsetY=fu.offsetY, k=k, method=method) fns, fts = ff.create_fitting_functions_for_multiple_levels(GNS, GTS)
def create_average_models(trainingSamples, method=''): XS = l.create_partial_XS(trainingSamples) MS = np.zeros((c.get_nb_teeth(), c.get_nb_dim())) IS = np.zeros((c.get_nb_teeth(), c.get_nb_dim())) for j in range(c.get_nb_teeth()): S = XS[j,:,:] M, Y = pa.PA(S) MS[j,:] = M mtx = mty = ms = mtheta = 0 n = S.shape[0] for i in range(n): tx, ty, s, theta = mu.full_align_params(M, fu.original_to_cropped(S[i,:])) mtx += tx mty += ty ms += s mtheta += theta n = float(n) IS[j,:] = mu.full_align(M, (mtx / n), (mty / n), (ms / n), (mtheta / n))
def create_partial_XS(trainingSamples): ''' Creates an array that contains all the training samples corresponding to the given training samples and corresponding to all the teeth. @param trainingSamples: the training samples @return np.array, shape=(nb of teeth, nb of training samples, nb of dimensions) ''' XS = np.zeros(np.array([c.get_nb_teeth(), len(trainingSamples), c.get_nb_dim()])) for j in range(c.get_nb_teeth()): XS[j,:,:] = create_partial_X(trainingSamples, nr_tooth=(j+1)) return XS
def store_plotted_vary_mode_param(closed_curve=False): ''' Stores the plots of the effects of varying one mode parameter / shape parameter in -3 s.d. / -2 s.d. / -1 s.d. / M / +1 s.d. / +2 s.d. / +3 s.d. in the model coordinate frame for all the teeth. @param closed_curve: must the curve be closed ''' for t in c.get_teeth_range(): M, Y = pa.PA(l.create_full_X(nr_tooth=t)) E, W, MU = pca.pca_percentage(Y) YS = np.zeros(np.array([E.shape[0], 7, c.get_nb_dim()])) for e in range(YS.shape[0]): se = math.sqrt(E[e]) C = np.zeros(W.shape[1]) C[e] = 1 j = 0 for i in range(-3, 4): YS[e, j, :] = pca.reconstruct(W, C * i * se, M) j += 1 (ymin, ymax, xmin, xmax) = pre.learn_offsets(YS) ymin -= 0.01 ymax += 0.01 xmin -= 0.01 xmax += 0.01 for e in range(YS.shape[0]): pyplot.figure() for j in range(1, YS.shape[1] + 1): xCoords, yCoords = mu.extract_coordinates(YS[e, (j - 1), :]) if (closed_curve): xCoords = mu.make_circular(xCoords) yCoords = mu.make_circular(yCoords) # x coordinates , y coordinates pyplot.subplot(1, 7, j) pyplot.plot(xCoords, yCoords, '-+r') pyplot.axis([xmin, xmax, ymin, ymax]) pyplot.gca().set_aspect('equal', adjustable='box') if j == 1: pyplot.ylabel('y\'') if j == 4: pyplot.title('Tooth nr: ' + str(t) + ' | ' + 'Eigenvalue: ' + str(E[e])) pyplot.xlabel('x\'') frame = pyplot.gca() if j % 2 == 0: frame.axes.get_xaxis().set_ticks([]) else: frame.axes.get_xaxis().set_ticks([xmin, xmax]) pyplot.gca().invert_yaxis() #pyplot.subplots_adjust(right=1) fname = c.get_fname_vis_pca(t, (e + 1)) pyplot.savefig(fname, bbox_inches='tight') pyplot.close()
def preprocess(): ''' Creates XS and MS, used by the drawing functions. * XS contains for each tooth, for each training sample, all landmarks (in the image coordinate frame) * MS contains for each tooth, the tooth model (in the model coordinate frame) ''' global XS, MS XS = l.create_full_XS() MS = np.zeros((c.get_nb_teeth(), c.get_nb_dim())) for j in range(c.get_nb_teeth()): M, Y = pa.PA(l.create_full_X(j+1)) MS[j,:] = M
def test2_combined(): Results = np.zeros( (c.get_nb_trainingSamples(), 3 * c.get_nb_teeth(), c.get_nb_dim())) color_lines = np.array([ np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), ]) for i in c.get_trainingSamples_range(): trainingSamples = c.get_trainingSamples_range() trainingSamples.remove(i) preprocess(trainingSamples) fname = c.get_fname_vis_pre(i, method) img = cv2.imread(fname) for f in range(2): for j in range(c.get_nb_teeth()): fname = c.get_fname_original_landmark(i, (j + 1)) P = fu.original_to_cropped( np.fromfile(fname, dtype=float, count=-1, sep=' ')) if f == 0: Results[(i - 1), j, :] = P Results[(i - 1), (f + 1) * c.get_nb_teeth() + j, :] = multi_resolution_search(img, P, j, fitting_function=f) fname = str(i) + 'm.png' cv2.imwrite( fname, fu.mark_results(np.copy(img), Results[(i - 1), :], color_lines))
def create_partial_X(trainingSamples, nr_tooth=1): ''' Creates an array that contains all the training samples corresponding to the given training samples and tooth number. @param trainingSamples: the training samples @param nrTooth: the number of the tooth @return np.array, shape=(nb of training samples, nb of dimensions) ''' X = np.zeros((len(trainingSamples), c.get_nb_dim())) index = 0 for i in trainingSamples: fname = c.get_fname_original_landmark(i, nr_tooth) print("loaded: " + fname) X[index,:] = np.fromfile(fname, dtype=float, count=-1, sep=' ') index += 1 return X
def test3_combined(): BS = cu.create_bboxes(method) Avg = cu.get_average_size(method) Results = np.zeros( (c.get_nb_trainingSamples(), 3 * c.get_nb_teeth(), c.get_nb_dim())) color_lines = np.array([ np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 0, 255]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([0, 255, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), np.array([255, 0, 0]), ]) for i in c.get_trainingSamples_range(): trainingSamples = c.get_trainingSamples_range() trainingSamples.remove(i) preprocess(trainingSamples) fname = c.get_fname_vis_pre(i, method) img = cv2.imread(fname) Params = cu.get_average_params(trainingSamples, method) x_min = BS[(i - 1), 0] x_max = BS[(i - 1), 1] y_min = BS[(i - 1), 2] y_max = BS[(i - 1), 3] ty = y_min + (y_max - y_min) / 2 for j in range(c.get_nb_teeth() / 2): if j == 0: tx = x_min + Avg[0, 0] / 2.0 if j == 1: tx = x_min + Avg[0, 0] + Avg[1, 0] / 2.0 if j == 2: tx = x_max - Avg[3, 0] - Avg[2, 0] / 2.0 if j == 3: tx = x_max - Avg[3, 0] / 2.0 P = limit( img, mu.full_align(MS[j, :], tx, ty, Params[j, 2], Params[j, 3])) fname = c.get_fname_original_landmark(i, (j + 1)) I = fu.original_to_cropped( np.fromfile(fname, dtype=float, count=-1, sep=' ')) Results[(i - 1), j, :] = I Results[(i - 1), c.get_nb_teeth() + j, :] = limit( img, multi_resolution_search(img, P, j)) #only limit for i=9: gigantic fail Results[(i - 1), 2 * c.get_nb_teeth() + j, :] = P x_min = BS[(i - 1), 4] x_max = BS[(i - 1), 5] y_min = BS[(i - 1), 6] y_max = BS[(i - 1), 7] ty = y_min + (y_max - y_min) / 2 for j in range(c.get_nb_teeth() / 2, c.get_nb_teeth()): if j == 4: tx = x_min + Avg[4, 0] / 2.0 if j == 5: tx = x_min + Avg[4, 0] + Avg[5, 0] / 2.0 if j == 6: tx = x_max - Avg[7, 0] - Avg[6, 0] / 2.0 if j == 7: tx = x_max - Avg[7, 0] / 2.0 P = limit( img, mu.full_align(MS[j, :], tx, ty, Params[j, 2], Params[j, 3])) fname = c.get_fname_original_landmark(i, (j + 1)) I = fu.original_to_cropped( np.fromfile(fname, dtype=float, count=-1, sep=' ')) Results[(i - 1), j, :] = I Results[(i - 1), c.get_nb_teeth() + j, :] = limit( img, multi_resolution_search(img, P, j)) #only limit for i=9: gigantic fail Results[(i - 1), 2 * c.get_nb_teeth() + j, :] = P fname = str(i) + 'c.png' cv2.imwrite( fname, fu.mark_results(np.copy(img), Results[(i - 1), :], color_lines))
def init_params(fname): global img, count, landmarks count = 0 landmarks = np.zeros((c.get_nb_dim())) img = cv2.imread(fname)
import cv2 import numpy as np import configuration as c img = None count = 0 landmarks = np.zeros((c.get_nb_dim())) def draw_landmark(event, x, y, flags, param): if (event == cv2.EVENT_LBUTTONDOWN and count < c.get_nb_landmarks()): global img, count, landmarks landmarks[(2 * count)] = float(x) landmarks[(2 * count + 1)] = float(y) if count > 0: xp = int(landmarks[(2 * count - 2)]) yp = int(landmarks[(2 * count - 1)]) cv2.line(img, (xp, yp), (x, y), (0, 0, 255)) cv2.circle(img, (x, y), 2, (0, 0, 255), -1) count += 1 if count == c.get_nb_landmarks(): xp = int(landmarks[0]) yp = int(landmarks[1]) cv2.line(img, (x, y), (xp, yp), (0, 0, 255)) def init_params(fname): global img, count, landmarks count = 0 landmarks = np.zeros((c.get_nb_dim())) img = cv2.imread(fname)