Esempio n. 1
0
def process_image(img):
    # Loader.print_image(img)
    # print("[DEBUG] Showing VISUAL denoising algorithm comparison")
    # denoising_comparison(img)
    # print("[DEBUG] Showing HISTOGRAM denoising algorithm comparison")
    # denoising_comparison(img, True)

    # DENOISING IMAGE
    denoised_img = smooth.median_filter(img, 9)
    denoised_img = smooth.median_filter(denoised_img, 7)
    # PRINT DENOISED IMAGE AND HISTOGRAM
    #Loader.print_image(denoised_img)
    # Loader.hist_and_cumsum(denoised_img)

    # thresholding_comparison(denoised_img)
    th_img = thresh.apply_thresholding_algorithm(denoised_img,
                                                 thresh.THRESH_TRIANGLE)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 5))
    stretched = cv2.morphologyEx(th_img, cv2.MORPH_ERODE, kernel)
    back, front = thresh.get_regions(denoised_img, stretched)
    # Loader.hist_compare([back, front], ["Back", "Front"])
    #Loader.print_image(front)
    eq = Loader.equalization(front.astype("uint8"))
    eq = bright_and_contrast(eq, 2.8, 80)
    eq = smooth.gaussian(eq, 2.5)
    Loader.print_image(eq)
    # Loader.hist_and_cumsum(eq)

    # EDGE DETECTION
    #edgesFunctions(eq) # Comparison of different edge detection method
    edges = edg.laplacian_of_gaussian(eq, 2)
    Loader.print_image(edges)
    # Fill the cornea area with white pixels
    dilated = fill_cornea(edges)
    #Loader.print_image(dilated)
    #Calculate distances in the cornea-lens region
    #lineas = dw.find_vertical_lines(dilated)
    #diferencias,posiciones = dw.calculate_differences(lineas)
    #dw.draw_graph_distance(diferencias, posiciones)
    #output_image = dw.lines_image(lineas, img)
    # Surround the córnea area and lens edges with visible and thin line
    (i, contornos, jerarquia) = cv2.findContours(dilated, cv2.RETR_EXTERNAL,
                                                 cv2.CHAIN_APPROX_SIMPLE)
    cnts = []
    for c in contornos:
        if cv2.contourArea(c) < 1000:
            continue
        else:
            cnts.append(c)
    cv2.drawContours(img, cnts, -1, (0, 0, 255), 3)
    #Loader.print_image(img)
    return img
def denoising_comparison(input_image, hist: bool = False):
    # NON LOCAL MEAN DENOISING
    nl_means_denoised_img = smooth.denoising_NlMeans(input_image)
    # MEDIAN FILTER DENOISING
    mean_denoised_img = smooth.median_filter(input_image, 9)
    mean_denoised_img = smooth.median_filter(input_image, 9)
    # GAUSSIAN DENOISING
    gaussian_denoised = smooth.gaussian(input_image, 1.5)
    # MINIMUM FILTER
    minimum_denoised = smooth.min_filter(input_image, (5, 5))
    # MAXIMUM FILTER
    maximum_denoised = smooth.max_filter(input_image, (5, 5))

    denoised_imgs = [
        img, gaussian_denoised, mean_denoised_img, nl_means_denoised_img,
        minimum_denoised, maximum_denoised
    ]
    denoised_titles = [
        "Original", "Denoised Gaussian", "Median Filtered", "NL Means Filter",
        "Minimums Filter", "Maximums Filter"
    ]
    Loader.hist_compare(denoised_imgs, denoised_titles, hist)

    return denoised_imgs, denoised_titles
    return outputCanvas


if __name__ == '__main__':

    print("[DEBUG] Load image from local sources")
    # img = Loader.load_image("test/th.jpeg")
    # Loader.print_image(stretched)
    # Loader.print_image(smoothed_thresholded)
    # Loader.print_image(front)

    for i in np.arange(1, 13):
        img = Loader.load_image("im" + i.__str__() + ".jpeg")
        print("Loaded image " + "im" + i.__str__() + ".jpeg")
        # DENOISING IMAGE
        denoised_img = smooth.median_filter(img, 9)
        denoised_img = smooth.median_filter(denoised_img, 7)

        # thresholding_comparison(denoised_img)
        th_img = thresh.apply_thresholding_algorithm(denoised_img,
                                                     thresh.THRESH_TRIANGLE)
        #Suavizamos los bordes marcados por la segmentacion
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 5))
        stretched = cv2.morphologyEx(th_img, cv2.MORPH_ERODE, kernel)
        smoothed_thresholded = smooth.max_filter(stretched, 5)
        #Obtenemos las areas correspondientes a la imagen original, despues del segmentado
        back, front = thresh.get_regions(denoised_img, stretched)

        IMAGE_TYPE_CLASSIFICATION = define_image_properties(
            smoothed_thresholded)
        if IMAGE_TYPE_CLASSIFICATION == "A":