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 show_feedback(M, P_before, P_after): ''' Shows feedback between iterations @param M: the model for the tooth @param P_before: the current points for the target tooth before validation in the image coordinate frame @param P_after: the current points for the target tooth after validation in the image coordinate frame ''' tx_old, ty_old, s_old, theta_old = mu.full_align_params(P_before, M) tx, ty, s, theta = mu.full_align_params(P_after, M) xm_old, ym_old = mu.get_center_of_gravity(P_before) xm, ym = mu.get_center_of_gravity(P_after) print( str((xm_old - xm)) + ' # ' + str((ym_old - ym)) + ' # ' + str((s_old - s)) + ' # ' + str((theta_old - theta)))
def validate(img, tooth_index, P_before, nb_it, show=False): ''' Validates the current points P for the target tooth corresponding to the given tooth index. @param img: the image @param P_before: the current points for the target tooth before validation in the image coordinate frame @param tooth_index: the index of the the target tooth (used in MS, EWS, fs) @param nb_it: the number of this iteration @param show: must the intermediate results (after each iteration) be displayed ''' MU = MS[tooth_index] E, W = EWS[tooth_index] xm, ym = mu.get_center_of_gravity(P_before) tx, ty, s, theta = mu.full_align_params(P_before, MU) PY_before = mu.full_align(P_before, tx, ty, s, theta) bs = pca.project(W, PY_before, MU) bs = np.maximum(np.minimum(bs, tolerable_deviation * E), -tolerable_deviation * E) PY_after = pca.reconstruct(W, bs, MU) P_after = mu.full_align(PY_after, xm, ym, 1.0 / s, -theta) if (show): fu.show_validation(MU, nb_it, PY_before, PY_after) fu.show_iteration(np.copy(img), nb_it, P_before, P_after) cv2.waitKey(0) pyplot.close() return P_after
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))