示例#1
0
def get_data_for_auto_init(is_upper, bbox_list, test_img_idx):

    img = cv2.imread('Data/Radiographs/'+str(test_img_idx).zfill(2)+'.tif')
    [mean_bbox, search_region] = get_parameters(is_upper, bbox_list, img)

    def_width = abs(mean_bbox[0] - mean_bbox[2])
    def_height = abs(mean_bbox[1] - mean_bbox[3])
    
    smallImages = np.zeros((13, def_width * def_height)) # building model excluding test image
    
#    # can load preprocessed images directly
#    radiographs = task2.load(preprocessed=True)
    
    # or can load and then preprocess
    radiographs = task2.load(preprocessed=False) 
    del radiographs[test_img_idx-1] # deleteing test index
    
    # skip_amf = without median filter
    radiographs = [task2.enhance(radiograph, skip_amf=True) for radiograph in radiographs] 
    
    for ind, radiograph in enumerate(radiographs):
        [x1, y1, x2, y2] = bbox_list[ind]
        cutImage = radiograph[y1:y2, x1:x2]
        result = cv2.resize(cutImage, (def_width, def_height), interpolation=cv2.INTER_NEAREST)
        smallImages[ind] = result.flatten()
    
    return [smallImages, [def_width, def_height, search_region]]
示例#2
0
def buildASM(incisor_list, test_img_idx, k):
    """
    Build ASM of a given incisor list. 
    Excludes the test image from model construction.
    Args:
        k: No. of pixels on either side of a model point for grey level model
    """
    asm_list = []
    train_imgs = task2.load()
    del train_imgs[test_img_idx-1]
    
    with Timer("Building Active Shape Model"):
    
        for incisor in incisor_list:
            print("..For Incisor "+str(incisor))
            
            train_lms = load_all_landmarks_of(incisor, test_img_idx)
        
            directory = "ASM_Models/test_img_%02d/" %(test_img_idx)
            filename = "incisor_%d.model" %(incisor)
    
            if not os.path.exists(directory+filename):
                asm = ASM(incisor, train_lms, train_imgs, k)
                asm_list.append(asm)
                save_asm(asm, directory, filename)
            else:
                with file(directory+filename, 'rb') as f:
                    asm_list.append( pickle.load(f) )
    
    print("") # just for elegant printing on screen   

    return asm_list
示例#3
0
def fit_model(asm_list,
              incisor_list,
              test_img_idx,
              m,
              auto_estimate=True,
              save=False,
              show=False):

    global save_plots
    global test_idx
    global show_plots
    save_plots = save
    test_idx = test_img_idx
    show_plots = show

    test_img = task2.load([test_img_idx])[0]
    X_init_list = []
    if auto_estimate:
        X_init_list = auto_init.get_estimate(asm_list, incisor_list,
                                             test_img_idx)
    else:
        lms_list = []
        for asm in asm_list:
            lms_list.append(asm.sm.mean_shape)

        X_init_list = manual_init.get_estimate(lms_list, incisor_list,
                                               test_img)

        Plots.plot_landmarks_on_image(X_init_list, test_img, title="manual_init",\
                                  show=False, save=False, wait=True, color=(0,255,0))

    with Timer("Fitting Model in Multi Resolution Framework"):

        final_fit = []
        for ind, X in enumerate(X_init_list):
            print("..For incisor %d" % (asm_list[ind].incisor))
            pyramid = task1.gauss_pyramid(test_img, PYRAMID_LEVELS)
            X = X.scaleposition(1.0 / 2**(PYRAMID_LEVELS + 1))
            level = PYRAMID_LEVELS

            for img in reversed(pyramid):
                #print("..Level %d" %(level))
                X = X.scaleposition(2)
                X = fit_one_level(X, img, asm_list[ind], level, m, MAX_ITER)
                level -= 1

            final_fit.append(X)

    print("")  # just for elegant printing on screen

    if save_plots or show_plots:
        directory = "Plots/model_fit/test_img_" + str(test_idx) + "/"
        Plots.plot_landmarks_on_image(final_fit, test_img, directory=directory, title="Final_fit",\
                          show=show_plots, save=save_plots, wait=True, color=(0,255,0))

    return final_fit
示例#4
0
def test_auto_initial_estimate(incisor_list, test_img_idx, k):

    asm_list = task1.buildASM(incisor_list, test_img_idx, k)
    X_init_list = auto_init.get_estimate(asm_list, incisor_list, test_img_idx, show_bbox_dist=True, \
                           show_app_models=False, show_finding_bbox=True, \
                           show_autoinit_bbox=False, show_autoinit_lms=False, save=False)
    test_img = task2.load([test_img_idx])[0]
    directory = "Plots/auto_init/test_img_%02d/" % (test_img_idx)
    Plots.plot_landmarks_on_image(X_init_list, test_img, directory=directory, title="auto_init",\
                                  show=False, save=True, wait=True, color=(0,255,0))
示例#5
0
def test_manual_init(incisor_list, test_img_idx):

    asm_list = task1.buildASM(incisor_list, test_img_idx, k)
    lms_list = []
    for asm in asm_list:
        lms_list.append(asm.sm.mean_shape)

    test_img = task2.load([test_img_idx])[0]
    X_init_list = manual_init.get_estimate(lms_list, test_img)

    Plots.plot_landmarks_on_image(X_init_list, test_img, title="manual_init",\
                              show=True, save=False, wait=True, color=(0,255,0))
示例#6
0
def get_big_bbox(is_upper, test_img_idx):
    """
    Finds the bounding box surrounding all the four upper(or lower) incisors
    """
    
    bbox_list = extract_roi_for_appModel(is_upper, test_img_idx)
    
    directory = "Autoinit_params/test_img_%02d/" %(test_img_idx)
    filename = "upper_incisor.model" if is_upper else "lower_incisor.model"

    if os.path.exists(directory+filename):
        with file(directory+filename, 'rb') as f:
            [smallImages, [def_width, def_height, search_region]] = pickle.load(f)    
    else:
        [smallImages, [def_width, def_height, search_region]] = \
                                get_data_for_auto_init(is_upper, bbox_list, test_img_idx)
        save_file([smallImages, [def_width, def_height, search_region]], directory, filename)
    
    [_, evecs, mean] = pca(smallImages, 5)
    
    global jaw_split
    global plot_app_models
    global save_plots
    global save_dir
    
    #Visualize the appearance model
    app_model = np.hstack( (mean.reshape(def_height,def_width), \
                                  normalize(evecs[:,0].reshape(def_height,def_width)), \
                                  normalize(evecs[:,1].reshape(def_height,def_width)), \
                                  normalize(evecs[:,2].reshape(def_height,def_width))) \
                                ).astype(np.uint8)
    if plot_app_models:
        cv2.imshow('app_model', app_model)
        cv2.waitKey(0)
    
    if save_plots:
        title = "upper_incisors" if is_upper else "lower_incisors"
        save_image(app_model, "appearance_model_"+title+".png", save_dir)
    
    test_img = task2.load([test_img_idx])[0]
    test_img = task2.enhance(test_img, skip_amf=True)
    [(a, b), (c, d)] = find_bbox(mean, evecs, test_img, def_width, def_height, is_upper, \
                                     jaw_split, search_region)
    
    return [(a, b), (c, d)]
示例#7
0
def evaluate_results(test_img_idx, incisor_list, final_fit_list):
    """
    Uses the Dice Coefficient to evaulate the similarity between the final landmarks 
    given by the model and the ground truth landmarks of the test image
    """

    test_img = task2.load([test_img_idx])[0]
    test_lms_list = load_landmarks(test_img_idx, incisor_list)

    dice_scores = []
    height, width, _ = test_img.shape

    with Timer("Evaluating Results"):
        print("..Dice similarity score")

        for ind, incisor in enumerate(incisor_list):

            image1 = np.zeros((height, width), np.uint8)
            image2 = np.zeros((height, width), np.uint8)

            X1 = test_lms_list[ind].as_matrix()  # X1 - ground truth

            cv2.fillPoly(image1, np.int32([X1]), 255)

            X2 = final_fit_list[ind].as_matrix()  # X2 - best fit

            cv2.fillPoly(image2, np.int32([X2]), 255)

            dice = np.sum(image1[image2 == 255]) * 2.0 / (np.sum(image1) +
                                                          np.sum(image2))
            print("....for incisor %02d - %.2f" % (incisor, dice))
            dice_scores.append(dice)

    if save_plots or show_plots:
        Plots.plot_results(np.arange(len(dice_scores)), incisor_list, dice_scores, \
                           test_img_idx, show=show_plots, save=save_plots)
示例#8
0
def get_estimate(asm_list,incisor_list, test_img_idx, show_bbox_dist=False, show_app_models=False, \
                 show_finding_bbox=False, show_autoinit_bbox=False, show_autoinit_lms=False, save=False):
    """
    Finds an initial estimate for all the incisors in the incisor_list
    """
    global plot_bbox_dist
    global plot_app_models
    global plot_finding_bbox
    global plot_autoinit_bbox
    global plot_autoinit_lms
    global save_plots
    global save_dir
    global jaw_split
    
    plot_bbox_dist = show_bbox_dist
    plot_app_models = show_app_models
    plot_finding_bbox = show_finding_bbox
    plot_autoinit_bbox = show_autoinit_bbox
    plot_autoinit_lms = show_autoinit_lms
    save_plots = save
    save_dir = "Plots/auto_init/test_img_%02d/" %(test_img_idx)
 
    with Timer("Finding Initial Estimate automatically"):
        
        if any(incisor < 5 for incisor in incisor_list): # upper incisor
            is_upper= True
            with Timer("..for upper incisors", dots="...."):
                [(w1U, h1U), (w2U, h2U)] = get_big_bbox(is_upper, test_img_idx)
            
        if any(incisor > 4 for incisor in incisor_list): # lower incisor
            is_upper= False
            with Timer("..for lower incisors", dots="...."):
                [(w1L, h1L), (w2L, h2L)] = get_big_bbox(is_upper, test_img_idx)        
    
    print("") # just for elegant printing on screen   
        
    init_list = []
    test_img = task2.load([test_img_idx])[0]
    img_org = test_img.copy()
    test_img = task2.enhance(test_img, skip_amf=True)
        
    for index,incisor in enumerate(incisor_list): 
        # Assume all teeth have more or less the same width
        
        if incisor < 5:
            ind = incisor
            bbox = [(w1U +(ind-1)*(w2U-w1U)/4, h1U), (w1U +(ind)*(w2U-w1U)/4, h2U)]
        else:
            ind = incisor - 4
            bbox = [(w1L +(ind-1)*(w2L-w1L)/4, h1L), (w1L +(ind)*(w2L-w1L)/4, h2L)]
            
        center = np.mean(bbox, axis=0)
        
        Plots.plot_autoinit(test_img, jaw_split, lowest_error_bbox=bbox, directory=save_dir, \
                            title="initial_estimate_bbox_incisor_%d" %(incisor), wait=True, \
                            show=plot_autoinit_bbox, save=False)#save=save_plots

        init = asm_list[index].sm.mean_shape.scale_to_bbox(bbox).translate(center)
        Plots.plot_landmarks_on_image([init], img_org, directory=save_dir, \
                                      title="initial_estimate_lms_incisor_%d" %(incisor), \
                                      show=plot_autoinit_lms, save=False, color=(0,255,0))#save=save_plots
        
        init_list.append(init)
        
    return init_list