Exemple #1
0
def active_snake(image_file, num_iters):

    #Parse image path  and create result image path
    path, filename = os.path.split(image_file)

    print("processing image : {0} \n".format(str(filename)))

    #load the image and perform pyramid mean shift filtering to aid the thresholding step
    imgcolor = cv2.imread(image_file)
    img_gray = cv2.cvtColor(imgcolor, cv2.COLOR_BGR2GRAY)

    # Image binarization by applying otsu threshold
    thresh = threshold_otsu(img_gray)
    binary = img_gray > thresh

    # Extract convex hull of the binary image
    convexhull = convex_hull_image(invert(binary))

    # label image regions
    label_image_convexhull = label(convexhull)

    # Measure properties of labeled image regions.
    regions = regionprops(label_image_convexhull)

    # center location of region
    y0, x0 = regions[0].centroid
    #print(y0,x0)
    print("Coordinates of centroid: {0} , {0} \n".format(y0, x0))

    # axis length of region
    d_major = regions[0].major_axis_length
    d_minor = regions[0].minor_axis_length

    diameter = regions[0].equivalent_diameter

    minr, minc, maxr, maxc = regions[0].bbox
    d_bbox = max(maxr - minr, maxc - minc)
    radius = int(max(d_major, d_minor, d_bbox) / 2) + 20

    print("Radius of convex hull region is: {0} \n".format(radius))

    gI = morphsnakes.gborders(img_gray, alpha=5, sigma=1)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.24, balloon=-1)

    mgac.levelset = circle_levelset(img_gray.shape, (y0, x0),
                                    radius,
                                    scalerow=0.75)

    # Visual evolution.
    morphsnakes.evolve_visual(mgac, num_iters=num_iters, background=imgcolor)

    #define result path for simplified segmentation result
    result_img_path = save_path_ac + str(filename[0:-4]) + '.png'

    # suppose that img's dtype is 'float64'
    img_uint8 = img_as_ubyte(mgac.levelset)

    cv2.imwrite(result_img_path, img_uint8)
Exemple #2
0
def test_tian():
    # Load the image.
    imgcolor = imread("../training/color_1.jpg") / 255.0
    img = rgb2gray(imgcolor)  # float in [0,1]

    # g(I)
    gI = morphsnakes.gborders(img, alpha=1000, sigma=2)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=10, threshold=0.3, balloon=-1)

    #macwe = morphsnakes.MorphACWE(img, smoothing=3, lambda1=1, lambda2=10)

    mask = imread("../intermediate/mask1.jpg")
    _, mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
    mgac.levelset = mask
    #macwe.levelset = mask

    # plot
    cv2.imshow('imgcolor', imgcolor)
    cv2.imshow('img', img)
    cv2.imshow('gI', gI)
    cv2.imshow('mgac.levelset', mgac.levelset)
    cv2.waitKey(0)

    # Visual evolution.
    ppl.figure()
    morphsnakes.evolve_visual(mgac, num_iters=20, background=imgcolor)
    #morphsnakes.evolve_visual(macwe, num_iters=20, background=imgcolor)

    cv2.destroyAllWindows()
    def snake(self, imgBGR, imgMask):
        # Load the image.
        #imgcolor = imread("../training/color_1.jpg")/255.0
        imgGray = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2GRAY)
        imgBGR = (imgBGR / 255.0).astype(np.float)
        imgGray = (imgGray / 255.0).astype(np.float)

        # g(I)
        gI = morphsnakes.gborders(imgGray, alpha=1000, sigma=2)

        # Morphological GAC. Initialization of the level-set.
        mgac = morphsnakes.MorphGAC(gI,
                                    smoothing=10,
                                    threshold=0.3,
                                    balloon=-1)
        _, imgMask = cv2.threshold(imgMask, 127, 255, cv2.THRESH_BINARY)
        mgac.levelset = imgMask

        # plot
        cv2.imshow('imgBGR', imgBGR)
        cv2.imshow('imgGray', imgGray)
        cv2.imshow('gI', gI)
        cv2.imshow('mgac.levelset', mgac.levelset)
        cv2.waitKey(0)

        # Visual evolution.
        plt.figure()
        b, g, r = cv2.split(imgBGR)
        imgRGB = cv2.merge([r, g, b])
        morphsnakes.evolve_visual(mgac, num_iters=10, background=imgRGB)
        return mgac.levelset
Exemple #4
0
def test_nodule():
    # Load the image.
    img = imread("testimages/mama07ORI.bmp")[..., 0] / 255.0

    # g(I)
    gI = morphsnakes.gborders(img, alpha=1000, sigma=5.48)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.31, balloon=1)
    mgac.levelset = circle_levelset(img.shape, (100, 126), 20)

    # Visual evolution.
    ppl.figure()
    morphsnakes.evolve_visual(mgac, num_iters=45, background=img)
Exemple #5
0
def test_lakes():
    # Load the image.
    imgcolor = imread("testimages/lakes3.jpg") / 255.0
    img = rgb2gray(imgcolor)

    # MorphACWE does not need g(I)

    # Morphological ACWE. Initialization of the level-set.
    macwe = morphsnakes.MorphACWE(img, smoothing=3, lambda1=1, lambda2=1)
    macwe.levelset = circle_levelset(img.shape, (80, 170), 25)

    # Visual evolution.
    ppl.figure()
    morphsnakes.evolve_visual(macwe, num_iters=190, background=imgcolor)
Exemple #6
0
def test_lakes():
    # Load the image.
    imgcolor = imread("testimages/lakes3.jpg") / 255.0
    img = rgb2gray(imgcolor)

    # MorphACWE does not need g(I)

    # Morphological ACWE. Initialization of the level-set.
    macwe = morphsnakes.MorphACWE(img, smoothing=3, lambda1=1, lambda2=1)
    macwe.levelset = circle_levelset(img.shape, (80, 170), 25)

    # Visual evolution.
    ppl.figure()
    morphsnakes.evolve_visual(macwe, num_iters=190, background=imgcolor)
Exemple #7
0
def test_nodule():
    # Load the image.
    img = imread("testimages/mama07ORI.bmp")[..., 0] / 255.0

    # g(I)
    gI = morphsnakes.gborders(img, alpha=1000, sigma=5.48)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.31, balloon=1)
    mgac.levelset = circle_levelset(img.shape, (100, 126), 20)

    # Visual evolution.
    ppl.figure()
    morphsnakes.evolve_visual(mgac, num_iters=45, background=img)
def test_nodule():
    # Load the image.
    #img = imread("testimages/mama07ORI.bmp")[...,0]/255.0
    #img_path = "testimages/CasoDD.bmp"
    img_path = "../../base de imagens/Ecocardiograma/Casos/Todos os Casos/"
    #img_name = "CasoID.bmp"
    img_name = "CasoQD.bmp"
    img_source = img_path + '/' + img_name

    img1 = imread(img_source)[..., 0] / 255.0

    #img[img <= (30/255)] = 0;
    #img[img >= (120/255)] = 1;
    img2 = cv2.imread(img_source, 0)[..., 0] / 255.0
    img = img2

    print(type(img1))
    print(type(img2))
    cv2.waitKey(0)
    #img = imread("testimages/CasoDD.bmp")[...,0]/255.0
    #img = imread("testimages/CasoDS.bmp")[...,0]/255.0
    #img = imread("testimages/CasoBD.bmp")[...,0]/255.0

    # g(I)
    #gI = morphsnakes.gborders(img, alpha=1000, sigma=5.48)
    gI = morphsnakes.gborders(img, alpha=1000, sigma=8)

    # Morphological GAC. Initialization of the level-set.
    #AM: mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.31, balloon=1)
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.50, balloon=1)
    #mgac.levelset = circle_levelset(img.shape, (100, 126), 20)

    #CasoAs.bmp
    #mgac.levelset = circle_levelset(img.shape, (230, 330), 20)
    #CasoAs.bmp
    iniPoint = getInitialPoint(img_source)
    #iniPoint = getInitialPoint(img)
    iniPointX = iniPoint[0]
    iniPointY = iniPoint[1]

    print('-> iniPoint[0]: ' + str(iniPointX))
    print('-> iniPoint[1]: ' + str(iniPointY))

    mgac.levelset = circle_levelset(img.shape, (iniPointY, iniPointX), 30)

    # Visual evolution.
    ppl.figure()
    #morphsnakes.evolve_visual(mgac, num_iters=45, background=img)
    morphsnakes.evolve_visual(mgac, num_iters=50, background=img)
Exemple #9
0
def test_starfish():
    # Load the image.
    imgcolor = imread("testimages/seastar2.png") / 255.0
    img = rgb2gray(imgcolor)

    # g(I)
    gI = morphsnakes.gborders(img, alpha=1000, sigma=2)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=2, threshold=0.3, balloon=-1)
    mgac.levelset = circle_levelset(img.shape, (163, 137), 135, scalerow=0.75)

    # Visual evolution.
    ppl.figure()
    morphsnakes.evolve_visual(mgac, num_iters=110, background=imgcolor)
Exemple #10
0
def test_starfish():
    # Load the image.
    imgcolor = imread("testimages/seastar2.png") / 255.0
    img = rgb2gray(imgcolor)

    # g(I)
    gI = morphsnakes.gborders(img, alpha=1000, sigma=2)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=2, threshold=0.3, balloon=-1)
    mgac.levelset = circle_levelset(img.shape, (163, 137), 135, scalerow=0.75)

    # Visual evolution.
    ppl.figure()
    morphsnakes.evolve_visual(mgac, num_iters=110, background=imgcolor)
Exemple #11
0
def test_mass():

    dataPath = 'C:/Tomosynthesis/localtest/'
    fileName = '5131R-recon08_45-1.tif'
    outputPath = 'C:/Tomosynthesis/localtest/res/'

    im = ImageIO.imReader(dataPath,fileName, 'tif',2)
    
    # padding borarders
    '''
    paddingrd = 10
    bordares = ((paddingrd,paddingrd),(paddingrd,paddingrd))
    paddingv = 10000
    bordarevs = ((paddingv,paddingv),(paddingv,paddingv))
    im = np.lib.pad(im.data[0], bordares, 'constant',constant_values = bordarevs)
    '''
    
    eqimg = histEqualization.histEqualization(im.data[0], 16)
    denoised = AT_denoising.DenoisingAW(eqimg)
    denoised = AT_denoising.DenoisingAW(denoised)
    denoised = AT_denoising.DenoisingAW(denoised)
    img = AT_denoising.DenoisingAW(denoised)
    tiffLib.imsave(outputPath + 'denoised.tif',img)

    # g(I)
    gI = morphsnakes.gborders(img, alpha=1, sigma=8)
    tiffLib.imsave(outputPath + 'gI.tif',np.float32(gI))
    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=2, threshold=0.035, balloon=-1)
    mgac.levelset = circle_levelset(img.shape, (img.shape[0]/2, img.shape[1]/2), 140, scalerow=0.75)
    
    # Visual evolution.
    ppl.figure()
    ls = morphsnakes.evolve_visual(mgac, num_iters=110, background=img)
    tiffLib.imsave(outputPath + 'ls.tif',np.float32(ls))
def test_pupil():
    global p_up, p_down, p_left, p_right
    # Load the image.
    img_lvl = imread(filename) / 255.0

    # g(I)
    gI = morphsnakes.gborders(img_lvl, alpha=2200, sigma=5.48)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.31, balloon=1)
    mgac.levelset = circle_levelset(img_lvl.shape, (cy, cx), (max_t * 0.3))

    # Visual evolution.
    ppl.figure()
    ij = morphsnakes.evolve_visual(mgac, num_iters=50, background=img_lvl)

    x_list = []
    y_list = []

    for i in range(w - 1):
        for j in range(h - 1):
            if ij[j][i] == 0:
                iris_bw[j][i] = (255, 0, 0)
            else:
                x_list.append(i)
                y_list.append(j)
                iris_bw[j][i] = (0, 0, 255)

    p_down = max(y_list)
    p_up = min(y_list)
    p_right = max(x_list)
    p_left = min(x_list)
def test_pupil():
    global p_up,p_down,p_left,p_right
    # Load the image.
    img_lvl = imread(filename)/255.0
    
    # g(I)
    gI = morphsnakes.gborders(img_lvl, alpha=2200, sigma=5.48)
    
    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.31, balloon=1)
    mgac.levelset = circle_levelset(img_lvl.shape, (cy, cx), (max_t*0.3))
    
    # Visual evolution.
    ppl.figure()
    ij = morphsnakes.evolve_visual(mgac, num_iters=50, background=img_lvl)
    
    x_list = []
    y_list = []
    
    for i in range(w-1):
        for j in range(h-1):
            if ij[j][i] == 0:
                iris_bw[j][i] = (255,0,0)
            else:
                x_list.append(i)
                y_list.append(j)
                iris_bw[j][i] = (0,0,255)
    
    p_down = max(y_list)
    p_up = min(y_list)
    p_right = max(x_list)
    p_left = min(x_list)
Exemple #14
0
def test_cord_GAC(img, prior, data_min, data_max):
    alpha = 1000
    sigma = 3
    smoothing = 5
    threshold = 1
    balloon = 1
    alpha = int(alpha)
    sigma = float(sigma)
    gI = morphsnakes.gborders(img, alpha, sigma)
    
    smoothing = int(smoothing)
    threshold = float(threshold)
    balloon = int(balloon)
    mgac = morphsnakes.MorphGAC(gI, int(smoothing), float(threshold), int(balloon))
    mgac.levelset = prior
    morphsnakes.evolve_visual(mgac, data_min, data_max, num_iters=45, background=img)
    return mgac.levelset
Exemple #15
0
def test_GAC(cont, img, p_alpha=1000, p_sigma=6, p_threshold=0.47,
             p_balloon=1, p_smoothing=1, p_num_iters=70
             ):
    gI = morphsnakes.gborders(img, alpha=p_alpha, sigma=p_sigma)
    mgac = morphsnakes.MorphGAC(gI, smoothing=p_smoothing, threshold=p_threshold, balloon=p_balloon)
    mgac.levelset = st.get_initial_point_lung(img, cont)
    ppl.figure()
    img = morphsnakes.evolve_visual(cont, mgac, num_iters=p_num_iters, background=img)
    return img
Exemple #16
0
def sinus_right(x, y, img):
    # g(I)
    gI = morphsnakes.gborders(img, alpha=2000, sigma=1.67)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.1, balloon=2)
    mgac.levelset = circle_levelset(img.shape, (y, x), 20)

    # Visual evolution.
    ppl.figure()
    return morphsnakes.evolve_visual(mgac, num_iters=200, background=img)
def morph_snake(img, ROI, radius):
    #Input: Image to apply snake to as well as a region of interest and radius for the snake to initialize from and extend to
    #Output: Image of applied snake (including pixel values for mass outline)

    img1 = img / 255.0
    # g(I)
    gI = morphsnakes.gborders(img1, alpha=1000, sigma=11)

    mgac = morphsnakes.MorphACWE(img1, smoothing=3, lambda1=1, lambda2=1)
    mgac.levelset = circle_levelset(img1.shape, ROI, radius)

    pylab.figure(figsize=(8, 6), dpi=400)
    return (morphsnakes.evolve_visual(mgac, num_iters=30,
                                      background=img1))  #110
    pylab.show()
def ac_inwards(imdata,visulization = False):
    """Propagation from outside toward the center."""
    
    #  calculate gradient information (I)
    gI = morphsnakes.gborders(imdata, alpha=0.5, sigma=2)
        
    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.035, balloon=-1)
    mgac.levelset = circle_levelset(imdata.shape, (imdata.shape[0]/2, imdata.shape[1]/2), 110, scalerow=1.0)
    
    # Visual evolution.
    if visulization ==  True:
        matplotlib.pyplot.figure()
    ls = morphsnakes.evolve_visual(mgac,visulization, num_iters=80, background=imdata)

    return ls
def ac_outwards(imdata,visulization = False):
    """Propagation from the center toward outside."""

    imdata = np.max(imdata) - imdata

    # calculate gradient information (I)
    gI = morphsnakes.gborders(imdata, alpha=1.3, sigma=2)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=2, threshold=0.028, balloon=1)
    mgac.levelset = circle_levelset(imdata.shape, (imdata.shape[0]/2, imdata.shape[1]/2), 15)
    
    # Visual evolution.
    if visulization ==  True:
        matplotlib.pyplot.figure()
    ls = morphsnakes.evolve_visual(mgac, visulization, num_iters=130, background=imdata)

    return ls
def snake_research(base_image, seed_image, per_size=0.25,smoothing=1, lambda1=1, lambda2=1):
    # the skimage snake implementation does not seems to be able to balloon
    # we try another implementation, maybe better, but pure research
     
    
    import sys
    sys.path.append('/home/remi/soft/morphsnakes')
      
    import morphsnakes
    from matplotlib import pyplot as ppl
    import time   
    from skimage.transform import resize  
    from PIL import Image 
    from skimage.morphology import label
    #    # Morphological ACWE. Initialization of the level-set.
    #    macwe = morphsnakes.MorphACWE(base_image, smoothing=smoothing, lambda1=lambda1, lambda2=lambda2)
    #    macwe.levelset = seed_image
    #    plt.imshow(macwe.levelset)
    #    
    #    # Visual evolution.
    #    ppl.figure()
    #    last_levelset = morphsnakes.evolve_visual(macwe, num_iters=10, background=jac)
    #    return last_levelset 
    down_sampled_jac = resize_image(base_image, per_size) 
    gI = morphsnakes.gborders(down_sampled_jac, alpha=1000, sigma=2)
    
    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=3, threshold=0.8, balloon=1.5)
    markers = label(seed_image)
    mgac.levelset = resize_image(markers>0, per_size) 
    
    # Visual evolution.
    ppl.figure()
    
      
    start_time = time.time()
    last_levelset = morphsnakes.evolve_visual(mgac, num_iters=50, background=down_sampled_jac )
    interval = time.time() - start_time  
    print('Total time in seconds:', interval)
    return last_levelset
def test_GAC(img,
             p_alpha=1000,
             p_auto_ini=True,
             p_x_ini=0,
             p_y_ini=0,
             p_sigma=6,
             p_threshold=0.47,
             p_balloon=1,
             p_smoothing=1,
             p_num_iters=70,
             p_raio=30):
    #p_threshold = 0.50 #(Estabilidade prematura)

    #####################################################################
    # AM: Definicao do criterio de parada baseado no gradiente
    # das fronteiras/bordas do objeto com função de suavizacao gaussiana
    # Onde:
    # - o parametro alpha funciona como um fator de peso para o gradiente.
    # Quanto maior for este valor maior a influencia da magnitude do
    # gradiente em regioes de diferentes contrastes
    # - o parametro sigma atua definindo a regiao de influencia da funcao
    # gaussiana, ou seja, define o desvio padrao. Quanto maior este valor,
    # maior sera a regiao de suavizacao da magnitude do gradiente, suavizando
    # o gradiente em regioes onde ha diferentes contrastes.
    #####################################################################
    # g(I)
    # gI = morphsnakes.gborders(img, alpha=1000, sigma=5.48)
    gI = morphsnakes.gborders(img, alpha=p_alpha, sigma=p_sigma)

    #ppl.title("Resultado da função gI")
    #ppl.imshow(gI)
    #return

    #####################################################################
    # AM: Inicializacao do level-set em toda a dimensão da imagem
    # smoothing: scalar
    #       Corresponde ao numero de repeticoes em que a suavizacao sera
    # aplicada a cada iteracao. Ou seja, a cada passo, serao aplicadas
    # smoothing vezes a funcao de suavizacao. Este procedimento e realizado
    # na funcao step da classe MorphGAC. Este representa o parametro µ.
    #
    # threshold : scalar
    #     The threshold that determines which areas are affected
    #     by the morphological balloon. This is the parameter θ.
    # balloon : scalar
    #     The strength of the morphological balloon. This is the parameter ν.
    ##################################################################

    #AM: mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.31, balloon=1)
    mgac = morphsnakes.MorphGAC(gI,
                                smoothing=p_smoothing,
                                threshold=p_threshold,
                                balloon=p_balloon)

    ##################################################################
    # AM: Calcula o ponto de inicialização da curva inicial do level-set
    ##################################################################
    if p_auto_ini:
        iniPoint = getInitialPoint(img_source)
        iniPointX = iniPoint[0]
        iniPointY = iniPoint[1]
    else:
        iniPointX = p_x_ini
        iniPointY = p_y_ini
    #print('-> iniPoint[0]: '+ str(iniPointX))
    #print('-> iniPoint[1]: '+ str(iniPointY))

    ##################################################################
    # AM: Define a função phi inicial no domínio completo da imagem
    # (img.shape). Cujo centro é definido pelos pontos (iniPointY, iniPointX)
    # O raio da função phi inicial é definido último parametro, ex: 30.
    # !!TODO!! -> Definir melhor o funcinamento desta função a luz do artigo
    ##################################################################
    mgac.levelset = getInitialPointLung(img)
    #mgac.levelset = circle_levelset(img.shape, (iniPointY, iniPointX), p_raio)

    ##################################################################
    # AM: Visualiza a evolução da curva e inicializa o máximo de interações
    ##################################################################
    ppl.figure()
    morphsnakes.evolve_visual(mgac, num_iters=p_num_iters, background=img)
Exemple #22
0
import sys

sys.path.append('/home/yuncong/morphsnakes')
import morphsnakes

import numpy as np
from skimage.io import imread
from matplotlib import pyplot as plt
from skimage.color import rgb2gray, rgb2hsv

imgcolor = imread("/home/yuncong/DavidData2015tifFlat/CC35_x0.3125/CC35_x0.3125_0281.tif")/255.
img = rgb2gray(imgcolor)

gI = morphsnakes.gborders(img, alpha=20000, sigma=1)

# Morphological GAC. Initialization of the level-set.
mgac = morphsnakes.MorphGAC(gI, smoothing=2, threshold=0.3, balloon=-3)
mgac.levelset = np.ones_like(img)
mgac.levelset[:3,:] = 0
mgac.levelset[-3:,:] = 0
mgac.levelset[:,:3] = 0
mgac.levelset[:,-3:] = 0

# Visual evolution.
plt.figure()
morphsnakes.evolve_visual(mgac, num_iters=500, background=imgcolor)
Exemple #23
0
def image_process(img_last_rgb, img_curr_rgb):
    global bb_x1, bb_y1, bb_x2, bb_y2
    global prev_bbx_x1, prev_bbx_y1, prev_bbx_x2, prev_bbx_y2
    global inputImg1, inputImg2, input_img_prev, input_img_curr
    global flag_predict_use_SURF, flag_predict_use_Dense

    flag_predict_use_SURF = False
    flag_predict_use_Dense = False
    flag_whole_imag_test = False
    # Get BBox Ground Truth by groudtruth
    #print index
    row = ground_truth_array[index]
    print('bbox_gt:', row)

    if len(img_curr_rgb.shape) < 3:
        inputImg1 = cv2.cvtColor(img_last_rgb, cv.CV_GRAY2RGB)
        inputImg2 = cv2.cvtColor(img_curr_rgb, cv.CV_GRAY2RGB)
        input_img_prev = img_last_rgb.copy()
        input_img_curr = img_curr_rgb.copy()
    else:
        inputImg1 = img_last_rgb.copy()
        inputImg2 = img_curr_rgb.copy()
        input_img_prev = cv2.cvtColor(img_last_rgb, cv2.COLOR_BGR2GRAY)
        input_img_curr = cv2.cvtColor(img_curr_rgb, cv2.COLOR_BGR2GRAY)

    if (flag_whole_imag_test == False):
        # Save All BBox file row to tmp variables
        tmp_x1 = int(row[0])
        tmp_y1 = int(row[1])
        tmp_x2 = int(row[2])
        tmp_y2 = int(row[3])
        tmp_x3 = int(row[4])
        tmp_y3 = int(row[5])
        tmp_x4 = int(row[6])
        tmp_y4 = int(row[7])
        print('eight variables', tmp_x1, tmp_y1, tmp_x2, tmp_y2, tmp_x3,
              tmp_y3, tmp_x4, tmp_y4)
        # Selecet the top-left and bottom-right points,
        # due to the different foramt(sequence) of the bbox file
        min_x = min(tmp_x1, tmp_x2, tmp_x3, tmp_x4)
        min_y = min(tmp_y1, tmp_y2, tmp_y3, tmp_y4)
        max_x = max(tmp_x1, tmp_x2, tmp_x3, tmp_x4)
        max_y = max(tmp_y1, tmp_y2, tmp_y3, tmp_y4)
        print('minX minY maxX maxY', min_x, min_y, max_x, max_y)
        bb_x1_gt = min_x
        bb_y1_gt = min_y
        bb_x2_gt = max_x
        bb_y2_gt = max_y
        width_gt = max_y - min_y
        height_gt = max_x - min_x
    else:
        img_rows, img_cols = input_img_prev.shape
        bb_x1_gt = 1
        bb_y1_gt = 1
        bb_x2_gt = img_rows
        bb_y2_gt = img_cols
        width_gt = img_cols
        height_gt = img_rows
    print('width_gt height_gt', width_gt, height_gt)
    print('bb_x1_gt, bb_y1_gt, bb_x2_gt, bb_y2_gt', bb_x1_gt, bb_y1_gt,
          bb_x2_gt, bb_y2_gt)
    # Choose current use bbox
    if ((flag_predict_use_SURF == False) and
        (flag_predict_use_Dense == False)) or (index < 2):
        bb_x1 = bb_x1_gt
        bb_y1 = bb_y1_gt
        bb_x2 = bb_x2_gt
        bb_y2 = bb_y2_gt
        width = width_gt
        height = height_gt
    else:
        bb_x1 = prev_bbx_x1
        bb_y1 = prev_bbx_y1
        bb_x2 = prev_bbx_x2
        bb_y2 = prev_bbx_y2
        width = bb_y2 - bb_y1
        height = bb_x2 - bb_x1

    #print ('bb', bb_x1, bb_y1, bb_x2, bb_y2)

    img_curr_rgb_clone = img_curr_rgb.copy()
    cv2.rectangle(img_curr_rgb_clone, (bb_x1, bb_y1), (bb_x2, bb_y2),
                  (0, 255, 0), 2)  # Draw ground truth bbx

    # create a CLAHE object (Arguments are optional).
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    cl1 = clahe.apply(input_img_prev)
    input_img_prev = cl1.copy()

    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    cl2 = clahe.apply(input_img_curr)
    input_img_curr = cl2.copy()

    # ------ Save BBox (x1, y1, x2, y2) with (h, w)
    img_rows, img_cols = input_img_prev.shape
    scale = 0.2
    bbox_x1 = int(max(0, bb_x1 - scale * height))  #refPt[0][1]
    bbox_x2 = int(min(bb_x2 + scale * height, img_cols))  #refPt[1][1]
    bbox_y1 = int(max(0, bb_y1 - scale * width))  #refPt[0][0]
    bbox_y2 = int(min(bb_y2 + scale * width, img_rows))  #refPt[1][0]
    refPt = np.empty([2, 2])
    refPt[0][1] = bbox_x1
    refPt[1][1] = bbox_x2
    refPt[0][0] = bbox_y1
    refPt[1][0] = bbox_y2
    # print bbox_x1, bbox_x2, bbox_y1, bbox_y2
    height = bbox_x2 - bbox_x1
    width = bbox_y2 - bbox_y1

    print('bbox', bbox_x1, bbox_x2, bbox_y1, bbox_y2)
    print('bbox_width*height', width, height)

    cv2.rectangle(img_curr_rgb_clone, (bbox_x1, bbox_y1), (bbox_x2, bbox_y2),
                  (0, 0, 255), 2)
    str_temp = 'Ground Truth'
    cv2.putText(img_curr_rgb_clone, str_temp, (10, 30), font, 0.5, (0, 255, 0),
                2)
    str_temp = '| BBox Extend'
    cv2.putText(img_curr_rgb_clone, str_temp, (130, 30), font, 0.5,
                (0, 0, 255), 2)
    cv2.namedWindow('Ground Truth', cv2.WINDOW_AUTOSIZE)
    total_frame = len(ground_truth_array)
    current_frame_str = 'Frame: '
    current_frame_str += str(index)
    current_frame_str += ' / '
    current_frame_str += str(total_frame)
    print('img_rows', img_rows)
    cv2.putText(img_curr_rgb_clone, current_frame_str,
                (10, int(img_rows - 20)), font, 0.5, (255, 255, 255), 2)
    cv2.imshow('Ground Truth', img_curr_rgb_clone)
    cv2.moveWindow('Ground Truth', 100, 100)
    #cv2.waitKey(0)
    #print bbox_x1, bbox_y1, bbox_x2, bbox_y2, height, width

    # ------------------ Active Countour -----------

    #-----Splitting the LAB image to different channels-------------------------
    img = img_curr_rgb.copy()

    lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(lab)
    # cv2.imshow('l_channel', l)
    # cv2.imshow('a_channel', a)
    # cv2.imshow('b_channel', b)

    #-----Applying CLAHE to L-channel-------------------------------------------
    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
    cl = clahe.apply(l)
    cv2.imshow('CLAHE output', cl)

    #-----Merge the CLAHE enhanced L-channel with the a and b channel-----------
    limg = cv2.merge((cl, a, b))

    img_countour = input_img_curr.copy()
    center_y = (bbox_y2 - bbox_y1) / 2
    center_x = (bbox_x2 - bbox_x1) / 2
    img_countour = img_countour[bbox_y1:bbox_y2, bbox_x1:bbox_x2]
    # img_countour = img_countour[center_y-10:center_y+10, center_x-10:center_x+10]

    # img_countour = exposure.equalize_hist(img_countour)

    img_countour_roi = img_countour.copy()
    cv2.namedWindow(' ROI', cv2.WINDOW_AUTOSIZE)
    cv2.imshow(' ROI', img_countour_roi)

    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (30, 30))

    img_closing = cv2.morphologyEx(img_countour_roi, cv2.MORPH_CLOSE, kernel)
    img_opening = cv2.morphologyEx(img_countour_roi, cv2.MORPH_OPEN, kernel)
    img_minus = img_closing - img_opening
    cv2.namedWindow('Active_Countour_Minus', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('Active_Countour_Minus', img_minus)

    # g(I)
    # gI = morphsnakes.gborders(img, alpha=1000, sigma=5.48)

    gI = morphsnakes.gborders(img_minus, alpha=500, sigma=1)
    cv2.namedWindow('GI', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('GI', gI)

    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI, smoothing=2, threshold=0.3, balloon=-1)
    # mgac.levelset = circle_levelset(img.shape, (159, 103), 50, scalerow=0.1) #
    rect_level_set = np.zeros(img_countour.shape)

    # # rect_level_set[243-10:276-10, 228+10:366+10] = 1
    [img_height, img_width] = img_countour.shape

    rect_level_set[3:img_height - 3, 3:img_width - 3] = 0.5
    # rect_level_set[center_y-10:center_y+10, center_x-10:center_x+10] = 1

    mgac.levelset = rect_level_set

    start_time = time.time()
    return_level_set = morphsnakes.evolve_visual(mgac,
                                                 num_iters=10,
                                                 background=img_countour)
    delta_time_contour = (time.time() - start_time)
    print("--- %s seconds ---" % delta_time_contour)

    # print('return_level_set',return_level_set)
    print('return_level_set', return_level_set.shape)

    img_draw_contour = img_countour.copy()
    # --- Using python plot show ----
    # ppl.figure(1)
    # fig = ppl.gcf()
    # fig.clf()
    # gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])

    # ax1 = ppl.subplot(1,2,2)
    # ax1.imshow(img_draw_contour, cmap=ppl.cm.gray)
    # ax1.contour(return_level_set, [0.5], colors='r')
    # fig.canvas.draw()
    # ax2 = ppl.subplot(1,2,1)
    # ax2.imshow(input_img_curr, cmap=ppl.cm.gray)
    # ppl.pause(0.05)

    return_level_set = cv2.resize(return_level_set, (160, 160),
                                  interpolation=cv2.INTER_CUBIC)
    contours, hierarchy = cv2.findContours(
        cv2.convertScaleAbs(return_level_set), cv2.RETR_TREE,
        cv2.CHAIN_APPROX_SIMPLE)

    img_curr_rgb_draw = img_curr_rgb.copy()
    img_curr_rgb_draw = img_curr_rgb_draw[bbox_y1:bbox_y2, bbox_x1:bbox_x2]
    img_curr_rgb_draw = cv2.resize(img_curr_rgb_draw, (160, 160),
                                   interpolation=cv2.INTER_CUBIC)

    cv2.drawContours(img_curr_rgb_draw, contours, -1, (0, 0, 250), 2)
    cv2.namedWindow('contour', cv2.WINDOW_NORMAL)

    cv2.imshow('contour', img_curr_rgb_draw)
    cv2.resizeWindow('contour', 160, 160)

    file_name = image_folder
    file_name += '_Contour_'
    file_name += str(index).zfill(3)
    file_name += '.jpg'
    cv2.imwrite(os.path.join(dirname, file_name), img_curr_rgb_draw)
    time_consume[index] = delta_time_contour
Exemple #24
0
def foreground_mask_morphsnakes_slide(img,
                                      levelset=None,
                                      max_iters=1000,
                                      num_section_per_slide=5,
                                      min_iters=300):

    # ls = pickle.load(open('/tmp/levelset.pkl', 'rb'))

    # img = denoise_bilateral(img, win_size=5, sigma_range=1, sigma_spatial=3, bins=10000, mode='constant', cval=0)

    # plt.imshow(img)
    # plt.show()

    gI = morphsnakes.gborders(img, alpha=15000, sigma=1)

    msnake = morphsnakes.MorphGAC(gI, smoothing=2, threshold=0.3, balloon=-3)

    if levelset is None:
        msnake.levelset = np.ones_like(img)
        msnake.levelset[:2, :] = 0
        msnake.levelset[-2:, :] = 0
        msnake.levelset[:, :2] = 0
        msnake.levelset[:, -2:] = 0
    else:
        msnake.levelset = levelset

    # for i in xrange(max_iters):
    # 	msnake.step()
    # 	if i > 0:
    # 		diff = np.count_nonzero(msnake.levelset - previous_levelset < 0)
    # 		print i, diff
    # 		if diff < 40 and i > min_iters: # oscillate
    # 			break
    # 	previous_levelset = msnake.levelset

    plt.figure()
    morphsnakes.evolve_visual(msnake, num_iters=max_iters, background=img)

    blob_labels, n_labels = label(msnake.levelset,
                                  neighbors=4,
                                  return_num=True,
                                  background=0)

    # pickle.dump(msnake.levelset, open('/tmp/levelset.pkl', 'wb'))

    # blob_labels, n_labels = label(ls, neighbors=4, return_num=True, background=0)

    blob_props = regionprops(blob_labels + 1)
    all_areas = np.array([p.area for p in blob_props])
    all_centers = np.array([p.centroid for p in blob_props])
    all_bboxes = np.array([p.bbox for p in blob_props])

    indices = np.argsort(all_areas)[::-1]
    largest_area = all_areas[indices[:2]].mean()

    valid = np.where(all_areas > largest_area * .45)[0]

    valid = valid[np.argsort(all_centers[valid, 1])]
    centers_x = all_centers[valid, 1]
    centers_y = all_centers[valid, 0]

    print 'valid', valid

    height, width = img.shape[:2]

    if len(valid) > num_section_per_slide:

        indices_close = np.where(np.diff(centers_x) < width * .1)[0]
        print indices_close

        ups = []
        downs = []

        if len(indices_close) > 0:
            for i in range(len(valid)):
                if i - 1 in indices_close:
                    continue
                elif i in indices_close:
                    if centers_y[i] > height * .5:
                        ups.append(valid[i + 1])
                        downs.append(valid[i])
                    else:
                        ups.append(valid[i])
                        downs.append(valid[i + 1])
                else:
                    if centers_y[i] > height * .5:
                        ups.append(-1)
                        downs.append(valid[i])
                    else:
                        ups.append(valid[i])
                        downs.append(-1)

        print ups, downs
        arrangement = np.r_[ups, downs]

    elif len(valid) < num_section_per_slide:
        snap_to_columns = (np.round(
            (centers_x / width + 0.1) * num_section_per_slide) - 1).astype(
                np.int)
        print 'snap_to_columns', snap_to_columns

        arrangement = -1 * np.ones((num_section_per_slide, ), dtype=np.int)
        arrangement[snap_to_columns] = valid
    else:
        arrangement = valid

    print 'arrangement', arrangement

    bboxes = []
    masks = []

    for i, j in enumerate(arrangement):
        if j == -1: continue

        minr, minc, maxr, maxc = all_bboxes[j]
        bboxes.append(all_bboxes[j])

        mask = np.zeros_like(img, dtype=np.bool)
        mask[blob_labels == j] = 1

        section_mask = mask[minr:maxr + 1, minc:maxc + 1]

        masks.append(section_mask)

    return masks, bboxes, arrangement > -1
Exemple #25
0
def test_GAC(img, p_alpha = 1000, p_auto_ini=True, p_x_ini=0, p_y_ini=0, p_sigma = 6, p_threshold = 0.47,
            p_balloon = 1, p_smoothing = 1, p_num_iters = 70, p_raio = 30, img_name=""):    
    #p_threshold = 0.50 #(Estabilidade prematura)

    #####################################################################
    # AM: Definicao do criterio de parada baseado no gradiente
    # das fronteiras/bordas do objeto com função de suavizacao gaussiana
    # Onde:
    # - o parametro alpha funciona como um fator de peso para o gradiente. 
    # Quanto maior for este valor maior a influencia da magnitude do 
    # gradiente em regioes de diferentes contrastes
    # - o parametro sigma atua definindo a regiao de influencia da funcao
    # gaussiana, ou seja, define o desvio padrao. Quanto maior este valor,  
    # maior sera a regiao de suavizacao da magnitude do gradiente, suavizando
    # o gradiente em regioes onde ha diferentes contrastes. 
    #####################################################################    
    # gI = morphsnakes.gborders(img, alpha=1000, sigma=5.48)
    levelset_zero,grad = getInitialPointLung(img);        
    gI = morphsnakes.gborders(img, alpha=p_alpha, sigma=p_sigma)
    

    ee = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])
    ee_1 =  np.eye(3)
    ee_2 = np.array([[1.0, 0, 0], [0, 1, 0],[0, 0, 1]])
    ee_3 = np.array([[0, 0, 1], [0, 1, 0],[1, 0, 0]])
    
    ee2 = np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])
    ee3 = np.array([[1, 1],[1, 1], [1, 1],[1, 1],[1, 1]])
    gI = cv2.dilate(gI, ee)    
    
    #####################################################################
    # AM: Fuzzy Frontier Detector - Fuzzy Adjust
    #####################################################################
    
    param_lambda = 0.30
    param_kappa = 0.50
    
    gI[gI<0.33] = 0.10 # g(I) function 
    gI[gI.all()>param_lambda and gI.all()<param_kappa] = 0.34 # g(I) fuzzy
    
    gI = cv2.erode(gI, ee) # local Min    
    gI = cv2.dilate(gI, ee)# local Max
    
    #####################################################################
    # AM: Inicializacao do level-set em toda a dimensão da imagem
    # smoothing: scalar
    #       Corresponde ao numero de repeticoes em que a suavizacao sera
    # aplicada a cada iteracao. Ou seja, a cada passo, serao aplicadas
    # smoothing vezes a funcao de suavizacao. Este procedimento e realizado 
    # na funcao step da classe MorphGAC. Este representa o parametro µ.    
    #
    # threshold : scalar
    #     The threshold that determines which areas are affected
    #     by the morphological balloon. This is the parameter θ.
    # balloon : scalar
    #     The strength of the morphological balloon. This is the parameter ν.
    ##################################################################

    #AM: mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.31, balloon=1)    
    mgac = morphsnakes.MorphGAC(gI, smoothing=p_smoothing, threshold=p_threshold, balloon=p_balloon)    

    ##################################################################
    # AM: Calcula o ponto de inicialização da curva inicial do level-set
    ##################################################################
    if p_auto_ini :
        iniPoint = getInitialPoint(img_source)    
        iniPointX = iniPoint[0]
        iniPointY = iniPoint[1]
    else:
        iniPointX = p_x_ini
        iniPointY = p_y_ini
    #print('-> iniPoint[0]: '+ str(iniPointX))
    #print('-> iniPoint[1]: '+ str(iniPointY))

    ##################################################################
    # AM: Define a função phi inicial no domínio completo da imagem 
    # (img.shape). Cujo centro é definido pelos pontos (iniPointY, iniPointX)
    # O raio da função phi inicial é definido último parametro, ex: 30.
    # !!TODO!! -> Definir melhor o funcinamento desta função a luz do artigo
    ##################################################################
    mgac.levelset = levelset_zero
    #mgac.levelset, img_norm = getInitialPointLung(img);
    #mgac.levelset = circle_levelset(img.shape, (iniPointY, iniPointX), p_raio)
    
    ##################################################################
    # AM: Visualiza a evolução da curva e inicializa o máximo de interações
    ##################################################################
    ppl.figure()
    morphsnakes.evolve_visual(mgac, num_iters=p_num_iters, background=img)    

    ##################################################################
    # AM: Executa sem visualizar a evoluçao da curva
    ##################################################################
    #mgac.run(iterations=p_num_iters)      
   
    return mgac.levelset;
    def snake(self, imgBGR, imgMask):
        # Load the image.
        #imgcolor = imread("../training/color_1.jpg")/255.0
        imgGray = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2GRAY)
        imgBGR = (imgBGR/255.0).astype(np.float)
        imgGray =(imgGray/255.0).astype(np.float)

        # g(I)
        gI = morphsnakes.gborders(imgGray, alpha=1000, sigma=2)

        # Morphological GAC. Initialization of the level-set.
        mgac = morphsnakes.MorphGAC(gI, smoothing=10, threshold=0.3, balloon=-1)
        _, imgMask = cv2.threshold(imgMask,127,255,cv2.THRESH_BINARY)
        mgac.levelset = imgMask

        # plot
        cv2.imshow('imgBGR', imgBGR)
        cv2.imshow('imgGray', imgGray)
        cv2.imshow('gI', gI)
        cv2.imshow('mgac.levelset', mgac.levelset)
        cv2.waitKey(0)

        # Visual evolution.
        plt.figure()
        b,g,r = cv2.split(imgBGR)
        imgRGB = cv2.merge([r,g,b])
        morphsnakes.evolve_visual(mgac, num_iters=10, background=imgRGB)
        return mgac.levelset