Esempio n. 1
0
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) 
Esempio n. 2
0
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))
Esempio n. 3
0
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) 
Esempio n. 4
0
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, :]))
Esempio n. 5
0
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_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 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)
Esempio n. 8
0
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,:])
Esempio n. 9
0
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))
Esempio n. 10
0
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))
Esempio n. 11
0
def draw_and_write(nr_trainingSample, nr_tooth, method=''):
    selected = draw_landmarks(c.get_fname_vis_pre(nr_trainingSample, method))
    fname = c.get_fname_fitting_manual_landmark(nr_trainingSample, nr_tooth)
    selected.tofile(fname, sep=" ", format="%s")