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 fill_cornea(edge_image):

    k1 = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 10))
    res1 = cv2.morphologyEx(edge_image, cv2.MORPH_DILATE, k1)
    print("DILATED")
    #Loader.print_image(res1)
    enhance_black = smooth.min_filter(res1, 7)
    print("BLACk")
    #Loader.print_image(enhance_black)
    k2 = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 5))
    res2 = cv2.morphologyEx(enhance_black, cv2.MORPH_ERODE, k2)
    print("eroded")
    #Loader.print_image(res2)
    res3 = smooth.max_filter(res2, 3)
    res3 = smooth.gaussian(res3, 1.5)
    print("dilated")
    #Loader.print_image(res3)

    return res3
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
        #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":
            # RESOLUTION 1
            print("Executing custom resolution, mode 1")
            # EDGE DETECTION
            #eq = Loader.equalization(front.astype("uint8"))
            eq = Loader.bright_and_contrast(front.astype("uint8"), 1.0, 20)
            eq = smooth.gaussian(eq, 1.5)
            #Loader.print_image(eq)
            edges = edg.laplacian_of_gaussian(eq, 2)
            #Loader.print_image(edges)
            normalized = fill_cornea(edges)
            #Loader.print_image(normalized)
            # Calculate distances in the cornea-lens region
            lineas = dw.find_vertical_lines(normalized)
            diferencias, posiciones, error = dw.calculate_differences(lineas)
            dw.draw_graph_distance(diferencias, posiciones)
            output_image = dw.lines_image(lineas, img, 7, error)

            contourned_img = draw_contours(normalized, output_image)

            print("Saved image by M1 " + "im" + i.__str__() + ".jpeg")
            cv2.imwrite(