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 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 create_full_XS(): ''' Creates an array that contains all the training samples corresponding to all the teeth. @return np.array, shape=(nb of teeth, nb of training samples, nb of dimensions) ''' return create_partial_XS(c.get_trainingSamples_range())
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 create_full_X(nr_tooth=1): ''' Creates an array that contains all the training samples corresponding to the given tooth number. @param nrTooth: the number of the tooth @return np.array, shape=(nb of training samples, nb of dimensions) ''' return create_partial_X(c.get_trainingSamples_range(), nr_tooth)
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 vis(): for m in ['l', 'u']: for i in c.get_trainingSamples_range(): cascade = cv2.CascadeClassifier( "CV/data/Training/training/cascadesSCD" + str(i) + '-' + m + "/cascade.xml") fname = c.get_fname_vis_pre(i, 'SCD') img = cv2.imread(fname) rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=1, minSize=(100, 100)) # result is an array of x coordinate, y coordinate, weight, height for each rectangle rects[:, 2:] += rects[:, :2] # result is an array of x coordinate, y coordinate, x + weight, y + height for each rectangle (opposite corners) for r in range(rects.shape[0]): cv2.rectangle(img, (rects[r, 0], rects[r, 1]), (rects[r, 2], rects[r, 3]), (0, 255, 0), 2) fname = 'test' + str(i) + '-' + m + '.png' cv2.imwrite(fname, img)
def preproccess(): ''' Preproccess all the radiographs. ''' XS = l.create_full_XS() (ymin, ymax, xmin, xmax) = learn_offsets_safe(XS) print("Preproccess learned offsets:") print(" * ymin: " + str(ymin)) #ymin: 497.0 print(" * ymax: " + str(ymax)) #ymax: 1362.0 print(" * xmin: " + str(xmin)) #xmin: 1234.0 print(" * xmax: " + str(xmax)) #xmax: 1773.0 for i in c.get_trainingSamples_range(): # read -> crop -> convert to grey scale grey_image = cv2.cvtColor(crop_by_diagonal(cv2.imread(c.get_fname_radiograph(i)), ymin, ymax, xmin, xmax), cv2.COLOR_BGR2GRAY) grey_image_denoised = cv2.fastNlMeansDenoising(grey_image) cv2.imwrite(c.get_fname_vis_pre(i, method='O'), grey_image) cv2.imwrite(c.get_fname_vis_pre(i, method='D'), grey_image_denoised) cv2.imwrite(c.get_fname_vis_pre(i, method='EH'), cv2.equalizeHist(grey_image)) cv2.imwrite(c.get_fname_vis_pre(i, method='EHD'), cv2.equalizeHist(grey_image_denoised)) cv2.imwrite(c.get_fname_vis_pre(i, method='SC'), stretch_contrast(grey_image)) cv2.imwrite(c.get_fname_vis_pre(i, method='SCD'), stretch_contrast(grey_image_denoised))
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 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))