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_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 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 classify_positives(method=''): XS = l.create_full_XS() for s in c.get_trainingSamples_range(): trainingSamples = c.get_trainingSamples_range() trainingSamples.remove(s) try: info_name_upper = c.get_dir_prefix() + 'data/Visualizations/Classified Samples/info' + method + str(s) + '-u' + '.txt' info_name_lower = c.get_dir_prefix() + 'data/Visualizations/Classified Samples/info' + method + str(s) + '-l' + '.txt' info_file_upper = open(info_name_upper, "w") info_file_lower = open(info_name_lower, "w") for i in trainingSamples: s = '' if (i < 10): s = '0' img_name = s + str(i) + '.png' min_y = min_x = float("inf") max_y = max_x = 0 for j in range(0, c.get_nb_teeth()/2): x_coords, y_coords = mu.extract_coordinates(XS[j, i-1, :]) for k in range(c.get_nb_landmarks()): if x_coords[k] < min_x: min_x = x_coords[k] if x_coords[k] > max_x: max_x = x_coords[k] if y_coords[k] < min_y: min_y = y_coords[k] if y_coords[k] > max_y: max_y = y_coords[k] line = 'rawdata/' + method + img_name + ' 1 ' + str(int(min_x - fu.offsetX)) + ' ' + str(int(min_y - fu.offsetY)) + ' ' + str(int(max_x - min_x)) + ' ' + str(int(max_y - min_y)) + '\n' info_file_upper.write(line) min_y = min_x = float("inf") max_y = max_x = 0 for j in range(c.get_nb_teeth()/2, c.get_nb_teeth()): x_coords, y_coords = mu.extract_coordinates(XS[j, i-1, :]) for k in range(c.get_nb_landmarks()): if x_coords[k] < min_x: min_x = x_coords[k] if x_coords[k] > max_x: max_x = x_coords[k] if y_coords[k] < min_y: min_y = y_coords[k] if y_coords[k] > max_y: max_y = y_coords[k] line = 'rawdata/' + method + img_name + ' 1 ' + str(int(min_x - fu.offsetX)) + ' ' + str(int(min_y - fu.offsetY)) + ' ' + str(int(max_x - min_x)) + ' ' + str(int(max_y - min_y)) + '\n' info_file_lower.write(line) finally: info_file_upper.close() info_file_lower.close()
def create_fitting_functions(GNS, GTS): ''' Creates the fitting function for each tooth, for each landmark. @param GNS: the matrix GNS which contains for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile normal through that landmark) @param GTS: the matrix GTS which contains for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile tangent through that landmark) @return The fitting functions for each tooth, for each landmark. ''' fns = [[ get_fitting_function(tooth, landmark, GNS) for landmark in range(c.get_nb_landmarks()) ] for tooth in range(c.get_nb_teeth())] fts = [[ get_fitting_function(tooth, landmark, GTS) for landmark in range(c.get_nb_landmarks()) ] for tooth in range(c.get_nb_teeth())] return fns, fts
def create_fitting_functions_for_multiple_levels(L_GNS, L_GTS): ''' Creates the fitting function for each level, for each tooth, for each landmark. @param L_GNS: the matrix L_GNS which contains for each level, for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile normal through that landmark) @param L_GTS: the matrix L_GTS which contains for each level, for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile tangent through that landmark) @return The fitting functions for each level, for each tooth, for each landmark. ''' l_fns = [[[ get_fitting_function(tooth, landmark, L_GNS[level, :]) for landmark in range(c.get_nb_landmarks()) ] for tooth in range(c.get_nb_teeth())] for level in range(L_GNS.shape[0])] l_fts = [[[ get_fitting_function(tooth, landmark, L_GTS[level, :]) for landmark in range(c.get_nb_landmarks()) ] for tooth in range(c.get_nb_teeth())] for level in range(L_GTS.shape[0])] return l_fns, l_fts
def create_partial_GS(trainingSamples, XS, MS, level=0, offsetX=0, offsetY=0, k=5, method=''): ''' Creates the matrix GNS which contains for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile normal through the landmarks). Creates the matrix GTS which contains for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile tangent through the landmarks). @param trainingSamples: the number of the training samples (not the test training samples!) @param XS: contains for each tooth, for each training sample, all landmarks (in the image coordinate frame) @param MS: contains for each tooth, the tooth model (in the model coordinate frame) @param level: the current level @param offsetX: the possible offset in x direction (used when working with cropped images and non-cropped landmarks) @param offsetY: the possible offset in y direction (used when working with cropped images and non-cropped landmarks) @param k: the number of pixels to sample either side for each of the model points along the profile normal @param method: the method used for preprocessing @return The matrix GNS which contains for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile normal through that landmark). The matrix GTS which contains for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile tangent through that landmark). ''' GNS = np.zeros((c.get_nb_teeth(), len(trainingSamples), c.get_nb_landmarks(), 2 * k + 1)) GTS = np.zeros((c.get_nb_teeth(), len(trainingSamples), c.get_nb_landmarks(), 2 * k + 1)) for j in range(c.get_nb_teeth()): index = 0 for i in trainingSamples: # model of tooth j from model coordinate frame to image coordinate frame xs, ys = mu.extract_coordinates( mu.full_align_with(MS[j], XS[j, index, :])) fname = c.get_fname_vis_pre(i, method) img = cv2.imread(fname) pyramid = gip.get_gaussian_pyramid_at(img, level) GN, GT = create_G(pyramid, k, xs, ys, offsetX, offsetY) GNS[j, index, :] = GN GTS[j, index, :] = GT index += 1 return GNS, GTS
def create_partial_GS_for_multiple_levels(trainingSamples, XS, MS, nb_levels=1, offsetX=0, offsetY=0, k=5, method=''): ''' Creates the matrix L_GNS which contains for each level, for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile normal through the landmarks). Creates the matrix L_GTS which contains for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile tangent through the landmarks). @param trainingSamples: the number of the training samples (not the test training samples!) @param XS: contains for each tooth, for each training sample, all landmarks (in the image coordinate frame) @param MS: contains for each tooth, the tooth model (in the model coordinate frame) @param nb_levels: the number of levels @param offsetX: the possible offset in x direction (used when working with cropped images and non-cropped landmarks) @param offsetY: the possible offset in y direction (used when working with cropped images and non-cropped landmarks) @param k: the number of pixels to sample either side for each of the model points along the profile normal @param method: the method used for preprocessing @return The matrix L_GNS which contains for each level, for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile normal through that landmark). The matrix L_GTS which contains for each level, for each tooth, for each of the given training samples, for each landmark, a normalized sample (along the profile tangent through that landmark). ''' L_GNS = np.zeros((nb_levels, c.get_nb_teeth(), len(trainingSamples), c.get_nb_landmarks(), 2 * k + 1)) L_GTS = np.zeros((nb_levels, c.get_nb_teeth(), len(trainingSamples), c.get_nb_landmarks(), 2 * k + 1)) for i in range(nb_levels): GNS, GTS = create_partial_GS(trainingSamples, np.around(np.divide(XS, 2**i)), MS, i, offsetX=round(float(offsetX) / 2**i), offsetY=round(float(offsetY) / 2**i), k=k, method=method) L_GNS[i, :] = GNS L_GTS[i, :] = GTS return L_GNS, L_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 get_average_size(method=''): IBS = create_individual_bboxes(method) Avg = np.zeros((c.get_nb_teeth(), 2)) for j in range(IBS.shape[0]): x = y = 0 for i in range(IBS.shape[1]): x += IBS[j,i,1] - IBS[j,i,0] y += IBS[j,i,3] - IBS[j,i,2] Avg[j,0] = x / float(IBS.shape[1]) Avg[j,1] = y / float(IBS.shape[1]) return Avg
def create_landmarks_and_models_images(color_init=np.array([0,255,255]), color_mid=np.array([255,0,255]), color_end=np.array([255,255,0]), color_line=np.array([0,0,255]), color_model_line=np.array([255,0,0]), method=''): ''' Stores all the preprocessed images corresponding to the given method with the landmarks of the training samples and models (transformed to the image coordinate system) marked. @param color_init: the BGR color for the first landmark @param color_mid: the BGR color for all landmarks except the first and last landmark @param color_end: the BGR color for the last landmark @param color_line: the BGR color for the line between two consecutive landmarks of the training samples @param color_model_line: the BGR color for the line between two consecutive landmarks of the models @param method: the method used for preproccesing ''' for i in c.get_trainingSamples_range(): fname = c.get_fname_vis_pre(i, method) img = cv2.imread(fname) for j in range(c.get_nb_teeth()): xs, ys = mu.extract_coordinates(XS[j,(i-1),:]) mxs, mys = mu.extract_coordinates(mu.full_align_with(MS[j], XS[j,(i-1),:])) for k in range(c.get_nb_landmarks()): x = int(xs[k] - offsetX) y = int(ys[k] - offsetY) mx = int(mxs[k] - offsetX) my = int(mys[k] - offsetY) if (k == c.get_nb_landmarks()-1): x_succ = int(xs[0] - offsetX) y_succ = int(ys[0] - offsetY) mx_succ = int(mxs[0] - offsetX) my_succ = int(mys[0] - offsetY) else: x_succ = int(xs[(k+1)] - offsetX) y_succ = int(ys[(k+1)] - offsetY) mx_succ = int(mxs[(k+1)] - offsetX) my_succ = int(mys[(k+1)] - offsetY) cv2.line(img, (x,y), (x_succ,y_succ), color_line) cv2.line(img, (mx,my), (mx_succ,my_succ), color_model_line) for k in range(c.get_nb_landmarks()): x = int(xs[k] - offsetX) y = int(ys[k] - offsetY) mx = int(mxs[k] - offsetX) my = int(mys[k] - offsetY) if (k == 0): img[y,x] = color_init img[my,mx] = color_init elif (k == c.get_nb_landmarks()-1): img[y,x] = color_end img[my,mx] = color_end else: img[y,x] = color_mid img[my,mx] = color_mid fname = c.get_fname_vis_ff_landmarks_and_models(i, method) cv2.imwrite(fname, img)
def create_profile_normals_images(k=5, color_init=np.array([0,255,255]), color_mid=np.array([255,0,255]), color_end=np.array([255,255,0]), color_line=np.array([255,0,0]), color_profile_point=np.array([0,255,0]), method=''): ''' Stores all the preprocessed images corresponding to the given method with the landmarks of the models (transformed to the image coordinate system) and the points along the profile normals marked. @param k: the number of profile points along either side of the profile normal and profile tangent @param color_init: the BGR color for the first landmark @param color_mid: the BGR color for all landmarks except the first and last landmark @param color_end: the BGR color for the last landmark @param color_line: the BGR color for the line between two consecutive landmarks @param color_profile_point: the BGR color for the profile points @param method: the method used for preproccesing ''' for i in c.get_trainingSamples_range(): fname = c.get_fname_vis_pre(i, method) img = cv2.imread(fname) for j in range(c.get_nb_teeth()): xs, ys = mu.extract_coordinates(mu.full_align_with(MS[j], XS[j,(i-1),:])) for h in range(c.get_nb_landmarks()): x = int(xs[h] - offsetX) y = int(ys[h] - offsetY) if (h == c.get_nb_landmarks()-1): x_succ = int(xs[0] - offsetX) y_succ = int(ys[0] - offsetY) else: x_succ = int(xs[(h+1)] - offsetX) y_succ = int(ys[(h+1)] - offsetY) cv2.line(img, (x,y), (x_succ,y_succ), color_line) for h in range(c.get_nb_landmarks()): x = int(xs[h] - offsetX) y = int(ys[h] - offsetY) tx, ty, nx, ny = ff.create_ricos(img, h, xs, ys) for n in range(-k, k+1): kx = round(x + n * nx) ky = round(y + n * ny) img[ky, kx] = color_profile_point for t in range(-k, k+1): kx = round(x + t * tx) ky = round(y + t * ty) img[ky, kx] = color_profile_point if (h == 0): img[y,x] = color_init elif (h == c.get_nb_landmarks()-1): img[y,x] = color_end else: img[y,x] = color_mid fname = c.get_fname_vis_ff_profile_normals(i, method) cv2.imwrite(fname, img)
def create_negatives(method=''): XS = l.create_full_XS() for i in c.get_trainingSamples_range(): fname = c.get_fname_vis_pre(i, method) img = cv2.imread(fname) s = '' if (i < 10): s = '0' min_y = min_x = float("inf") max_y = max_x = 0 for j in range(0, c.get_nb_teeth()/2): x_coords, y_coords = mu.extract_coordinates(XS[j, i-1, :]) for k in range(c.get_nb_landmarks()): if x_coords[k] < min_x: min_x = x_coords[k] if x_coords[k] > max_x: max_x = x_coords[k] if y_coords[k] < min_y: min_y = y_coords[k] if y_coords[k] > max_y: max_y = y_coords[k] fname = c.get_dir_prefix() + 'data/Visualizations/Classified Samples/' + method + str(s) + str(i) + '-l' + '.png' cv2.imwrite(fname, img[max_y-fu.offsetY+1:,:]) min_y = min_x = float("inf") max_y = max_x = 0 for j in range(c.get_nb_teeth()/2, c.get_nb_teeth()): x_coords, y_coords = mu.extract_coordinates(XS[j, i-1, :]) for k in range(c.get_nb_landmarks()): if x_coords[k] < min_x: min_x = x_coords[k] if x_coords[k] > max_x: max_x = x_coords[k] if y_coords[k] < min_y: min_y = y_coords[k] if y_coords[k] > max_y: max_y = y_coords[k] fname = c.get_dir_prefix() + 'data/Visualizations/Classified Samples/' + method + str(s) + str(i) + '-u' + '.png' cv2.imwrite(fname, img[:min_y-fu.offsetY,:])
def test(): 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 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=' ')) fname = str(i) + '-' + str((j + 1)) + '.png' cv2.imwrite(fname, fu.show_iteration(np.copy(img), 10000, P, IS[j, :]))
def test1(): 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 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=' ')) R = multi_resolution_search(img, P, j) fname = str(i) + '-' + str((j + 1)) + '.png' cv2.imwrite(fname, fu.mark_results(np.copy(img), np.array([P, R])))
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))