コード例 #1
0
def foreground_mask_morphsnakes(img):

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

	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

	num_iters = 1000
	for i in xrange(num_iters):
	    msnake.step()
	    if np.sum(msnake.levelset - previous_levelset) < 3:
	    	break
	    previous_levelset = msnake.levelset

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

	blob_props = regionprops(blob_labels + 1)
	largest_blob = np.argmax([p.area for p in blob_props])

	mask = np.zeros_like(msnake.levelset, dtype=np.bool)
	mask[blob_labels == largest_blob] = 1

	min_size = 40
	mask = remove_small_objects(mask, min_size=min_size, connectivity=1, in_place=False)

	return mask
コード例 #2
0
def foreground_mask_morphsnakes(img, levelset=None, max_iters=1000, scaling=1):

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

    gI = morphsnakes.gborders(img, alpha=1000, sigma=3)

    msnake = morphsnakes.MorphGAC(gI,
                                  smoothing=1,
                                  threshold=.8,
                                  balloon=-1,
                                  scaling=scaling)

    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

    # min_iters = min(min_iters, max_iters)

    prev_levelset = levelset

    diffs = []
    for i in xrange(max_iters):

        # print i
        # c = time.time()
        msnake.step()
        # print time.time() - c
        diff = np.count_nonzero(msnake.levelset - prev_levelset != 0)

        diffs.append(diff)
        if i > 10:
            q = np.abs(np.mean(diffs[-5:]) - np.mean(diffs[-10:-5]))
            if q < 2:
                break
        prev_levelset = msnake.levelset

    sys.stderr.write('iter %d\n' % i)

    mask = msnake.levelset.astype(np.bool)
    mask = remove_small_objects(mask, min_size=40)

    # previous_levelset = levelset

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

    rows, cols = np.where(mask)
    bbox = np.array([rows.min(), rows.max(), cols.min(), cols.max()])

    return mask, bbox
コード例 #3
0
    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
コード例 #4
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()
コード例 #5
0
ファイル: tests_ac.py プロジェクト: sapresearch/Tomo
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))
コード例 #6
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)
コード例 #7
0
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)
コード例 #8
0
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)
コード例 #9
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
コード例 #10
0
def test_gac(img, p_alpha=1000, p_sigma=4, p_threshold=0.45,
             p_balloon=1, p_smoothing=1, p_num_iters=70):
    from roi.startPointDoubleLung import get_initial_point_lung
    import morphsnakes

    # 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)
    levelset_zero, grad = get_initial_point_lung(img)
    g_i = morphsnakes.gborders(img, p_alpha, p_sigma)

    ee_3x3 = np.ones((4, 4), dtype=int)

    g_i = cv2.dilate(g_i, ee_3x3)

    g_i[g_i < 0.33] = 0.10
    g_i[0.30 < g_i.all() < 0.50] = 0.34
    ###################
    g_i = cv2.erode(g_i, ee_3x3)
    g_i = cv2.dilate(g_i, ee_3x3)

    # 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 ν.
    mgac = morphsnakes.MorphGAC(g_i, p_smoothing, p_threshold, p_balloon)

    # 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.
    mgac.levelset = levelset_zero
    # AM: Visualiza a evolução da curva e inicializa o máximo de interações
    mgac.run(p_num_iters)
    return mgac.levelset
コード例 #11
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)
コード例 #12
0
ファイル: tests.py プロジェクト: yiyizheliu/morphsnakes
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)
コード例 #13
0
ファイル: tests.py プロジェクト: RabeiAultra/SDCML
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)
コード例 #14
0
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)
コード例 #15
0
ファイル: tests.py プロジェクト: sjbraga/level-set-morph
def test_morph_obj():
    img = imread("testimages/twoObj.bmp") / 255.0

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

    # ppl.hist(gI, normed=True)

    mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.28, balloon=-1)
    mgac.levelset = circle_levelset(img.shape, (40, 40), 40)

    ppl.figure()
    return visual.evolve_visual(mgac,
                                num_iters=40,
                                background=img,
                                save_every_iter=10)
コード例 #16
0
ファイル: tests.py プロジェクト: yiyizheliu/morphsnakes
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)
コード例 #17
0
def test_1():
    # Load the image.
    img = imread("output.jpg")[..., 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.41, balloon=1)
    mgac.levelset = circle_levelset(img.shape, (148, 200), 20)

    # Visual evolution.
    plt.figure()
    evolve_visual(mgac, img, num_iters=150, background=img)
    mpimg.imsave('crop.jpg', res)
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()
コード例 #19
0
ファイル: tests.py プロジェクト: RabeiAultra/SDCML
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)
コード例 #20
0
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
コード例 #21
0
def faz_level_set(img, poligono, it):
    gmin, gmax = 0.0, 1.0
    fmin, fmax = img.min(), img.max()

    nimg = ((gmax - gmin) / (fmax - fmin)) * (img - fmin) + gmin

    gI = morphsnakes.gborders(nimg, alpha=1, sigma=2)

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

    for i in xrange(it):
        mgac.step()

        yield mgac.levelset
コード例 #22
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
コード例 #23
0
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
コード例 #24
0
def morph_snakes(data,
                 mask,
                 slice=None,
                 scale=0.5,
                 alpha=1000,
                 sigma=1,
                 smoothing_ls=1,
                 threshold=0.3,
                 balloon=1,
                 max_iters=50,
                 show=False,
                 show_now=True):
    data_o = data.copy()
    mask_o = mask.copy()
    if scale != 1:
        # data = skitra.rescale(data, scale=scale, preserve_range=True).astype(np.uint8)
        # mask = skitra.rescale(mask, scale=scale, preserve_range=True).astype(np.bool)
        data = tools.resize3D(data, scale, sliceId=0)
        mask = tools.resize3D(mask, scale, sliceId=0)

    gI = morphsnakes.gborders(data, alpha=alpha, sigma=sigma)
    # Morphological GAC. Initialization of the level-set.
    mgac = morphsnakes.MorphGAC(gI,
                                smoothing=smoothing_ls,
                                threshold=threshold,
                                balloon=balloon)
    mgac.levelset = mask
    mgac.run(iterations=max_iters)
    seg = mgac.levelset

    if scale != 1:
        # data = tools.resize_ND(data, shape=orig_shape)
        # mask = tools.resize_ND(mask, shape=orig_shape)
        seg = tools.resize_ND(seg, shape=data_o.shape)

    if show:
        tools.visualize_seg(data_o,
                            seg,
                            mask_o,
                            slice=slice,
                            title='morph snakes',
                            show_now=show_now)
    return data, mask, seg
コード例 #25
0
def test_2():
    for image_path in os.listdir(path):
        input_path = os.path.join(path, image_path)
        # Load the image.
        imgcolor = imread(input_path) / 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, (200, 250),
                                        210,
                                        scalerow=0.75)

        # Visual evolution.
        plt.figure()
        res = evolve_visual(mgac, img, num_iters=320, background=imgcolor)
        fullpath = os.path.join(outPath, 'f' + image_path)
        mpimg.imsave(fullpath, res)
コード例 #26
0
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
コード例 #27
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):
    #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)
コード例 #28
0
ファイル: MICOS.py プロジェクト: TLKline/PyCysticImage
def MICOS(fileNAME):

	tic = time.clock()

	selfz =0
	selfz1 = 0
	selfz2 = 0
	selfrotD = -90
	imgObj2= nib.load(str(fileNAME))
	imgObj1 = imgObj2
	im = imgObj2
	selfaffine2 = imgObj2.get_affine()  
	selfheaderdtype = imgObj2.get_data_dtype()
	selfPSx = imgObj1.get_header()['pixdim'][1]
	selfPSy = imgObj1.get_header()['pixdim'][2]
	selfPSz = imgObj1.get_header()['pixdim'][3]
	(x,y,z) = orx.aff2axcodes(selfaffine2)
	selfOrx = x
	selfOry = y
	selfOrz = z
	ornt = orx.axcodes2ornt((x,y,z))  
	refOrnt = orx.axcodes2ornt(('R','S','A')) #was 'R', 'A', 'S'
	newOrnt = orx.ornt_transform(ornt,refOrnt)
	selfornt = ornt
	selfrefOrnt = refOrnt

	selfimg_data2 = imgObj2.get_data()       

	selfimg_data2 = orx.apply_orientation(selfimg_data2,newOrnt)

	selfimg_data2 = np.fliplr(np.rot90(selfimg_data2,1))
	im_data = selfimg_data2

	[x_si,y_si,z_si] = np.shape(im_data)

	#do 99% norm to 1000 
	im_data = np.array(im_data,dtype='float')
	im_data = im_data * 1000/np.percentile(im_data,99)
	#print np.shape(im_data)
	initialSeg = im_data.copy() * 0

	#begin user roi drawing...
	#go from middle up...
	for i in xrange(np.round(z_si/2),z_si,3):

		img = (im_data[:,:,i])

		# show the image
		if i > np.round(z_si/2):
			plt.figure(figsize=(ROI1.figwidth,ROI1.figheight))
		plt.imshow(img,cmap='gray')
		plt.colorbar()
		plt.title("outline one kidney, slice = " + str(i))

		# let user draw first ROI
		ROI1 = polydraw(roicolor='r') #let user draw first ROI

		# show the image with the first ROI
		plt.figure(figsize=(ROI1.figwidth,ROI1.figheight))
		plt.imshow(img,cmap='gray')
		plt.colorbar()
		ROI1.displayROI()
		plt.title("outline other kidney, slice = " + str(i))

		# let user draw second ROI
		ROI2 = polydraw(roicolor='b') #let user draw ROI

		initialSeg[:,:,i] = ROI1.getMask(img) + ROI2.getMask(img)


	#go from middle up...
	for i in xrange(np.round(z_si/2)-1,0,-3):

		img = (im_data[:,:,i])

		# show the image
		plt.figure(figsize=(ROI1.figwidth,ROI1.figheight))
		plt.imshow(img,cmap='gray')
		plt.colorbar()
		plt.title("outline one kidney, slice = " + str(i))

		# let user draw first ROI
		ROI1 = polydraw(roicolor='r') #let user draw first ROI

		# show the image with the first ROI
		plt.figure(figsize=(ROI1.figwidth,ROI1.figheight))
		plt.imshow(img,cmap='gray')
		plt.colorbar()
		ROI1.displayROI()
		plt.title("outline other kidney, slice = " + str(i))

		# let user draw second ROI
		ROI2 = polydraw(roicolor='b') #let user draw ROI

		initialSeg[:,:,i] = ROI1.getMask(img) + ROI2.getMask(img)

	toc = time.clock()


	#save out drawn polygon
	aff = selfaffine2
	outImage = deepcopy(initialSeg)#np.rot90(np.fliplr(self.segImg),-1)
	[x_si,y_si,z_si] = np.shape(outImage)
	#print np.shape(outImage)
	#This method works (for fastsegs)... but need more robust
	#for i in range(0,z_si):
	#    outImage[:,:,i] = np.rot90(self.segImg[:,:,z_si-1-i],-1)

	#try new method (more robust to header and affine mix-ups)
	ornt = orx.axcodes2ornt((selfOrx,selfOry,selfOrz))  
	refOrnt = orx.axcodes2ornt(('R','S','A'))
	newOrnt = orx.ornt_transform(refOrnt,ornt) #reversed these
	outImage= orx.apply_orientation(np.rot90(np.fliplr(outImage),-1),newOrnt)  
	#outImage = orx.apply_orientation(outImage,newOrnt)    
	#outImage = np.rot90(np.fliplr(outImage),-1)
	#print np.shape(outImage)
	#outImage = np.array(outImage,dtype=selfheaderdtype)
	new_image = nib.Nifti1Image(outImage,aff)

	nib.save(new_image,fileNAME[:-7]+'_polygon_MICOS.nii.gz')

	# Dilate and fill in missing slices
	initialSeg = dilation(initialSeg,iterations = 1)
	finalSeg = initialSeg.copy() * 0

	# now try convex hull method instead to better approximate missing slices (previous method is above)
	# This works but way too long. Also, would likely need to do object finding first, so compute
	# Convex hull for each kidney separately.
	while 0:
		xlist,ylist,zlist = find_3D_object_voxel_list(initialSeg)
		voxlist = np.zeros(shape=(np.shape(xlist)[0],3),dtype='int16')
		voxlist[:,0] = xlist
		voxlist[:,1] = ylist
		voxlist[:,2] = zlist
		tri = dtri(voxlist)

		# construct full voxel list
		xxlist,yylist,zzlist = find_3D_object_voxel_list((initialSeg+1)>0)

		fullvoxlist = np.zeros(shape=(np.shape(xxlist)[0],3),dtype='int16')
		fullvoxlist[:,0] = xxlist
		fullvoxlist[:,1] = yylist
		fullvoxlist[:,2] = zzlist

		finalSeg = np.array(in_hull(fullvoxlist,tri),dtype=float)
		finalSeg = np.reshape(finalSeg,(x_si,y_si,z_si))

	# Now do gaussian blur of polygon to smooth
	initialSeg = (filt.gaussian_filter(initialSeg.copy()*255,sigma=[3,3,1])) > 100

	#Begin optimized method...
	for i in xrange(0,z_si):
		img = (im_data[:,:,i])
		if np.max(initialSeg[:,:,i]>0):

			mgac = []
			gI = msnake.gborders(img,alpha=1E5,sigma=3.0) # increasing sigma allows more changes in contour
			mgac = msnake.MorphGAC(gI,smoothing=3,threshold=0.01,balloon=0.0) #was 2.5
			mgac.levelset = initialSeg[:,:,i]>0.5
			for ijk123 in xrange(100):
				mgac.step()
			finalSeg[:,:,i] = mgac.levelset
		#print i

	# Now do gaussian blur and threshold to finalize segmentation...
	finalSeg = (filt.gaussian_filter(finalSeg.copy()*255,sigma=[3,3,1])) > 100
	#using this helps with single slice errors of the active contour

	# Try adding now narrow band sobel/watershed technique.

	for i in xrange(0,z_si):
		img = (im_data[:,:,i])
		segslice = finalSeg[:,:,i]
		if np.max(finalSeg[:,:,i]>0):

			erodeimg = erosion(segslice.copy(),iterations=1)
			dilateimg = dilation(segslice.copy(),iterations=1)

			seeds = img * 0
			seeds[:] = 1
			seeds[dilateimg>0] = 0
			seeds[erodeimg>0] = 2
			sobelFilt = sobel(np.array(img.copy(),dtype='int16'))
			mgac = watershed(sobelFilt,seeds)>1

			finalSeg[:,:,i] = mgac>0

	#save out segmentation
	aff = selfaffine2
	outImage = deepcopy(finalSeg)#np.rot90(np.fliplr(self.segImg),-1)
	outImage = np.array(outImage,dtype='float')
	[x_si,y_si,z_si] = np.shape(outImage)

	#This method works (for fastsegs)... but need more robust
	#for i in range(0,z_si):
	#    outImage[:,:,i] = np.rot90(self.segImg[:,:,z_si-1-i],-1)

	#try new method (more robust to header and affine mix-ups)
	ornt = orx.axcodes2ornt((selfOrx,selfOry,selfOrz))  
	refOrnt = orx.axcodes2ornt(('R','S','A'))
	newOrnt = orx.ornt_transform(refOrnt,ornt) #reversed these
	outImage= orx.apply_orientation(np.rot90(np.fliplr(outImage),-1),newOrnt)  
	#outImage = orx.apply_orientation(outImage,newOrnt)    
	#outImage = np.rot90(np.fliplr(outImage),-1)
	new_image = nib.Nifti1Image(outImage,aff)
	nib.save(new_image,fileNAME[:-7]+'_FASTseg_MICOS.nii.gz')

	print 'time = '
	print toc - tic

	return (fileNAME[:-7]+'_FASTseg_MICOS.nii.gz')
コード例 #29
0
def foreground_mask_morphsnakes(img, num_iters=1000):

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

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

    msnake.levelset = np.ones_like(img)
    msnake.levelset[:3, :] = 0
    msnake.levelset[-3:, :] = 0
    msnake.levelset[:, :3] = 0
    msnake.levelset[:, -3:] = 0

    for i in xrange(num_iters):
        msnake.step()

        if i > 0:
            if i > 1:
                previous_diff = diff
            diff = np.count_nonzero(msnake.levelset - previous_levelset)
            # print i, diff
            if i > 1:
                if diff == previous_diff and diff < 40 and i > 500:  # oscillate
                    break

        previous_levelset = msnake.levelset

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

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

    blob_props = regionprops(blob_labels + 1)
    all_areas = [p.area for p in blob_props]
    indices = np.argsort(all_areas)[::-1]
    largest_blob = indices[0]

    n_sections = 1

    if len(all_areas) > 1:
        second_blob = indices[1]
        if all_areas[second_blob] / all_areas[largest_blob] > 0.6:
            print 'two sections in this image'
            n_sections = 2

    masks = []
    for sec_i in range(n_sections):

        mask = np.zeros_like(msnake.levelset, dtype=np.bool)
        mask[blob_labels == indices[sec_i]] = 1

        min_size = 40
        mask = remove_small_objects(mask,
                                    min_size=min_size,
                                    connectivity=1,
                                    in_place=False)

        masks.append(mask)

    return masks
コード例 #30
0
ファイル: stereo2seg.py プロジェクト: TLKline/PyCysticImage
                    seeds = np.array(labels.copy()*0,dtype = 'uint8')
                    seeds[labelsD==1] = 1
                    seeds[labelsE==1] = 2
                    seeds[labelsE*labelsD] = 0
                    seeds[labelsE2==1] = 1


                    sobelFilt = sobel(mrdata[:,:,i])
                    #output[:,:,i] = watershed(mrdata[:,:,i],seeds)>1 # _WS4 used this
                    output[:,:,i] = watershed(sobelFilt,seeds)>1 #_withSobel used this

                    #Add active contour
                    while 0:
                        mgac = []
                        gI = msnake.gborders(mrdata[:,:,i],alpha=1E5,sigma=1.0) # increasing sigma allows more changes in contour
                        mgac = msnake.MorphGAC(gI,smoothing=3,threshold=0.01,balloon=0.0)
                        mgac.levelset = output[:,:,i]>0.5
                        for ijk123 in xrange(3):
                            mgac.step()
                        output[:,:,i] = mgac.levelset


                else:
                    output[:,:,i] = output[:,:,i] * 0
                #print i

            #_WS4 used this
            finalSeg = np.array(output,dtype='float')
            finalSeg = (filt.gaussian_filter(finalSeg*255,sigma=[3,3,1])) > 100
            output = mrdata.copy() * 0
コード例 #31
0
def ac(
    NAME,
    priorSeg,
    followUpScan,
    followUpSegName,
    alpha=1e5,
    sigma=3,
    smoothing=3,
    threshold=0.01,
    balloon=1.0,
    iters=45,
):
    "Perform active contour on initial warped follow-up segmentation (to finalize segmentation)."
    print "Performing active contour finalization of follow-up segmentation..."
    input_fixed = followUpScan
    gray = nib.load(input_fixed)
    graydata = gray.get_data()

    # do 99% norm to 1000
    origDATA = np.array(graydata, dtype=float)
    origDATA = origDATA * 1000 / np.percentile(origDATA, 99)

    input_seg = NAME + "/" + os.path.basename(priorSeg[:-7]) + "0to1.nii.gz"
    seg = nib.load(input_seg)
    segdata = seg.get_data()

    affine = seg.get_affine()
    new_image_preAC = nib.Nifti1Image(segdata, affine)
    nib.save(new_image_preAC, followUpSegName[:-7] + "_preAC.nii.gz")

    img = segdata > 0.2

    [L, M, N] = np.shape(img)

    # Active Contour 2D
    finalseg2 = img.copy() * 0

    for n in range(0, N):
        mgac = []

        img2 = origDATA[:, :, n]
        gI = msnake.gborders(img2, alpha, sigma)

        # Morphological GAC. Initialization of the level-set.
        mgac = msnake.MorphGAC(gI, smoothing, threshold, balloon)
        mgac.levelset = img[:, :, n] > 0.2

        for ijk123 in xrange(iters):  # num iterations
            mgac.step()

        finalseg2[:, :, n] = mgac.levelset

    # Light filtering
    finalSeg = (gaussian_filter(finalseg2.copy() * 255, sigma=[3, 3, 1])) > 100
    im_data = origDATA.copy()

    # Add now narrow band sobel/watershed technique.
    for i in xrange(0, N):
        img = im_data[:, :, i]
        segslice = finalSeg[:, :, i]
        if np.max(finalSeg[:, :, i] > 0):

            erodeimg = erosion(segslice.copy(), iterations=1)
            dilateimg = dilation(segslice.copy(), iterations=1)

            seeds = img * 0
            seeds[:] = 1
            seeds[dilateimg > 0] = 0
            seeds[erodeimg > 0] = 2
            sobelFilt = sobel(np.array(img.copy(), dtype="int16"))
            mgac = watershed(sobelFilt, seeds) > 1

            finalSeg[:, :, i] = mgac > 0

            # Write the final output
    affine = seg.get_affine()
    image1 = np.uint8(finalSeg)
    new_image = nib.Nifti1Image(image1, affine)
    nib.save(new_image, followUpSegName)
コード例 #32
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)
コード例 #33
0
sig = 5.3
alph = 2500
smooth = 1
thres = 0.395
ball = 1.6#1.48
iter = 250




dst = cv2.GaussianBlur(imbw,(5,5),0)
dst = imbw
img = dst/255.0

gI = morphsnakes.gborders(img, alpha=alph, sigma=sig)
# cv2.imshow("Cost", gI)

mgac = morphsnakes.MorphGAC(gI, smoothing=smooth, threshold=thres, balloon=ball)
# mgac.levelset = circle_levelset(img.shape, (300,160), 15)
mgac.levelset = circle_levelset(img.shape, (302,175), 15)

# macwe = morphsnakes.MorphACWE(img, smoothing=1, lambda1=1, lambda2=5)
# macwe.levelset = circle_levelset(img.shape, (255, 255), 25)

mask1, edges = morphsnakes.evolve(mgac, num_iters=iter, animate=True, background=dst)
# cv2.waitKey(0)

mgac = morphsnakes.MorphGAC(gI, smoothing=smooth, threshold=thres, balloon=ball)
mgac.levelset = circle_levelset(img.shape, (300,353), 15)
コード例 #34
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
コード例 #35
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
コード例 #36
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;
コード例 #37
-1
    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