Ejemplo n.º 1
0
def example_nodule(pt):
    #     logging.info('Running: example_nodule (MorphGAC)...')

    # Load the image.
    # 用quan完之後的圖用會很糟
    img = imread(OUTPUT_CUTHIST) / 255.0

    # img2為背景圖
    img2 = imread(OUTPUT_BLACK) / 255.0

    # g(I)
    gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=5.48)

    # Initialization of the level-set.
    init_ls = ms.circle_level_set(img.shape, pt, 15)

    # Callback for visual plotting
    callback = visual_callback_2d(img2)

    # MorphGAC.
    ms.morphological_geodesic_active_contour(gimg,
                                             iterations=45,
                                             init_level_set=init_ls,
                                             smoothing=1,
                                             threshold=0.29,
                                             balloon=1,
                                             iter_callback=callback)
def test_morphsnakes_incorrect_image_shape():
    img = np.zeros((10, 10, 3))
    ls = np.zeros((10, 9))

    with pytest.raises(ValueError):
        morphological_chan_vese(img, iterations=1, init_level_set=ls)
    with pytest.raises(ValueError):
        morphological_geodesic_active_contour(img, iterations=1,
                                              init_level_set=ls)
Ejemplo n.º 3
0
def find_contour(img):
    img = img/255
    gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=5.48)
    init_ls = ms.circle_level_set(img.shape, (100, 126), 20)
    callback = visual_callback_2d(img)
    ms.morphological_geodesic_active_contour(gimg, iterations=45,
                                             init_level_set=init_ls,
                                             smoothing=1, threshold=0.31,
                                             balloon=1, iter_callback=callback)
Ejemplo n.º 4
0
def moneyImage(image):
    inverseGaussianGradient = morphsnakes.inverse_gaussian_gradient(image)
    initLevelSet = np.zeros(image.shape, dtype=np.int8)
    initLevelSet[10:-10, 10:-10] = 1
    morphsnakes.morphological_geodesic_active_contour(
        inverseGaussianGradient,
        iterations=230,
        init_level_set=initLevelSet,
        smoothing=20,
        threshold=0.69,
        balloon=-1,
        iter_callback=plot_2d(image))
def example_tiger():

    imgage_color = imread(PATH_IMG_TIGER) / 255.0
    img = rgb2gray(imgage_color)
    gradient_inverse = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=2)
    init_ls = ms.create_circle(img.shape, None, 150)
    callback = visual_callback_2d(imgage_color)
    ms.morphological_geodesic_active_contour(gradient_inverse,
                                             iterations=150,
                                             init_level_set=init_ls,
                                             smoothing=2,
                                             threshold=0.3,
                                             balloon=-1,
                                             iter_callback=callback)
def example_trefle():

    img = imread(PATH_IMG_TREFLE)[..., 0] / 255.0
    gradient_inverse = ms.inverse_gaussian_gradient(img,
                                                    alpha=1000,
                                                    sigma=5.48)
    init_ls = ms.create_circle(img.shape, None, 20)
    callback = visual_callback_2d(img)
    ms.morphological_geodesic_active_contour(gradient_inverse,
                                             iterations=100,
                                             init_level_set=init_ls,
                                             smoothing=1,
                                             threshold=0.32,
                                             balloon=1,
                                             iter_callback=callback)
Ejemplo n.º 7
0
def seastarImage(image, imageRgp):
    inverseGaussianGradient = morphsnakes.inverse_gaussian_gradient(image,
                                                                    alpha=1000,
                                                                    sigma=2)
    initLevelSet = morphsnakes.circle_level_set(image.shape,
                                                center=(163, 137),
                                                radius=135)
    morphsnakes.morphological_geodesic_active_contour(
        inverseGaussianGradient,
        iterations=150,
        init_level_set=initLevelSet,
        smoothing=20,
        threshold=0.3,
        balloon=-1,
        iter_callback=plot_2d(imageRgp))
Ejemplo n.º 8
0
def seaImg(image):
    inverseGaussianGradient = morphsnakes.inverse_gaussian_gradient(image,
                                                                    alpha=1000,
                                                                    sigma=5.48)
    initLevelSet = morphsnakes.circle_level_set(image.shape,
                                                center=(100, 126),
                                                radius=30)
    morphsnakes.morphological_geodesic_active_contour(
        inverseGaussianGradient,
        iterations=65,
        init_level_set=initLevelSet,
        smoothing=20,
        threshold=0.31,
        balloon=1,
        iter_callback=plot_2d(image))
Ejemplo n.º 9
0
def test_init_level_sets():

    image = np.zeros((6, 6))
    checkerboard_ls = morphological_chan_vese(image, 0, 'checkerboard')
    checkerboard_ref = np.array([[0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [1, 1, 1, 1, 1, 0]], dtype=np.int8)

    circle_ls = morphological_geodesic_active_contour(image, 0, 'circle')
    circle_ref = np.array([[0, 0, 0, 0, 0, 0],
                           [0, 0, 1, 1, 1, 0],
                           [0, 1, 1, 1, 1, 1],
                           [0, 1, 1, 1, 1, 1],
                           [0, 1, 1, 1, 1, 1],
                           [0, 0, 1, 1, 1, 0]], dtype=np.int8)

    ellipsoid_ls = morphological_chan_vese(np.zeros((7, 9)), 0, 'ellipsoid')
    ellipsoid_ref = np.array(
        [[0, 0, 1, 1, 1, 1, 1, 0, 0],
         [0, 1, 1, 1, 1, 1, 1, 1, 0],
         [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, 1, 1],
         [0, 1, 1, 1, 1, 1, 1, 1, 0],
         [0, 0, 1, 1, 1, 1, 1, 0, 0]],
        dtype=np.uint8
    )

    assert_array_equal(checkerboard_ls, checkerboard_ref)
    assert_array_equal(circle_ls, circle_ref)
    assert_array_equal(ellipsoid_ls, ellipsoid_ref)
Ejemplo n.º 10
0
def make_acwe(patch: Patch, params: dict):
    """
    Active Contour

    (Float layer -> Float layer)

    """
    from skimage import exposure

    edge_map = 1.0 - patch.image_layers["Main"]
    edge_map -= np.min(edge_map)
    edge_map = edge_map / np.max(edge_map)
    # edge_map = exposure.adjust_sigmoid(edge_map, cutoff=1.0)
    logger.debug("Calculating ACWE")
    import morphsnakes as ms

    seg1 = ms.morphological_geodesic_active_contour(
        edge_map,
        iterations=params["iterations"],
        init_level_set=patch.image_layers["total_mask"],
        smoothing=params["smoothing"],
        threshold=params["threshold"],
        balloon=params["balloon"],
    )

    outer_mask = ((seg1 * 1.0) > 0) * 2.0

    # inner_mask = ((seg2 * 1.0) > 0) * 1.0
    # outer_mask = outer_mask * (1.0 - inner_mask)
    # anno = outer_mask + inner_mask

    patch.image_layers["acwe"] = outer_mask
    # show_images([outer_mask[outer_mask.shape[0] // 2, :]], figsize=(12, 12))

    return patch
def test_morphsnakes_simple_shape_geodesic_active_contour():

    img = np.float_(circle_level_set((11, 11), (5, 5), 3.5))
    gimg = inverse_gaussian_gradient(img, alpha=10.0, sigma=1.0)
    ls = circle_level_set(img.shape, (5, 5), 6)

    ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                    [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                    [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                    [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
                    [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                    [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                    [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                   dtype=np.int8)

    gac_ls = morphological_geodesic_active_contour(gimg, iterations=10,
                                                   init_level_set=ls,
                                                   balloon=-1)

    assert_array_equal(gac_ls, ref)

    assert gac_ls.dtype == np.int8
Ejemplo n.º 12
0
def gac2d(img, coord, iterations, smoothing, balloon, threshold):
    print('Running: snake_2d (MorphGAC)...')

    range_img = img[:, :, coord[0]]
    range_init_ls = ms.circle_level_set(range_img.shape, (coord[2], coord[1]),
                                        5)
    range_gimage = inverse_gaussian_gradient(range_img)
    range_ls = ms.morphological_geodesic_active_contour(
        range_gimage,
        iterations=iterations,
        init_level_set=range_init_ls,
        smoothing=smoothing,
        balloon=balloon,
        threshold=threshold)
    # save_img(range_img, range_ls, "gac_2d_y_slice")

    slices = []
    i = 0
    for row in range_ls:
        for x in row:
            if x == 1:
                slices.append([i, middle_of_line(row)])
                break
        i += 1

    middle = int((slices[-1][0] + slices[0][0]) / 2)
    result = np.zeros(img.shape, dtype=np.uint8)
    for line in slices:
        image_part = img[line[0]]
        init_ls = ms.circle_level_set(image_part.shape, (line[1], coord[0]), 5)

        gimage = inverse_gaussian_gradient(image_part)
        ls = ms.morphological_geodesic_active_contour(gimage,
                                                      iterations=iterations,
                                                      init_level_set=init_ls,
                                                      smoothing=smoothing,
                                                      balloon=balloon,
                                                      threshold=threshold)
        result[line[0]] = ls
        # if i == middle:
        #     save_img(image_part, ls, "gac_2d_slice")

    return result
Ejemplo n.º 13
0
def example_coins():
    logging.info('Running: example_coins (MorphGAC)...')
    
    # Load the image.
    img = imread(PATH_IMG_COINS) / 255.0
    
    # g(I)
    gimg = ms.inverse_gaussian_gradient(img)
    
    # Manual initialization of the level set
    init_ls = np.zeros(img.shape, dtype=np.int8)
    init_ls[10:-10, 10:-10] = 1
    
    # Callback for visual plotting
    callback = visual_callback_2d(img)
    
    # MorphGAC. 
    ms.morphological_geodesic_active_contour(gimg, 230, init_ls,
                                             smoothing=1, threshold=0.69,
                                             balloon=-1, iter_callback=callback)
Ejemplo n.º 14
0
def example_nodule():
    logging.info('Running: example_nodule (MorphGAC)...')

    # Load the image.
    img = imread(PATH_IMG_NODULE)[..., 0] / 255.0

    # g(I)
    gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=5.48)

    # Initialization of the level-set.
    init_ls = ms.circle_level_set(img.shape, (100, 126), 20)

    # Callback for visual plotting
    callback = visual_callback_2d(img)

    # MorphGAC.
    ms.morphological_geodesic_active_contour(gimg, iterations=45,
                                             init_level_set=init_ls,
                                             smoothing=1, threshold=0.31,
                                             balloon=1, iter_callback=callback)
Ejemplo n.º 15
0
def example_nodule():
    logging.info('Running: example_nodule (MorphGAC)...')
    
    # Load the image.
    img = imread(PATH_IMG_NODULE)[..., 0] / 255.0
    
    # g(I)
    gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=5.48)
    
    # Initialization of the level-set.
    init_ls = ms.circle_level_set(img.shape, (100, 126), 20)
    
    # Callback for visual plotting
    callback = visual_callback_2d(img)
    
    # MorphGAC. 
    ms.morphological_geodesic_active_contour(gimg, iterations=45, 
                                             init_level_set=init_ls,
                                             smoothing=1, threshold=0.31,
                                             balloon=1, iter_callback=callback)
Ejemplo n.º 16
0
def example_coins():
    logging.info('Running: example_coins (MorphGAC)...')

    # Load the image.
    img = imread(PATH_IMG_COINS) / 255.0

    # g(I)
    gimg = ms.inverse_gaussian_gradient(img)

    # Manual initialization of the level set
    init_ls = np.zeros(img.shape, dtype=np.int8)
    init_ls[10:-10, 10:-10] = 1

    # Callback for visual plotting
    callback = visual_callback_2d(img)

    # MorphGAC.
    ms.morphological_geodesic_active_contour(gimg, 230, init_ls,
                                             smoothing=1, threshold=0.69,
                                             balloon=-1, iter_callback=callback)
Ejemplo n.º 17
0
def example_starfish():
    logging.info('Running: example_starfish (MorphGAC)...')
    
    # Load the image.
    imgcolor = imread(PATH_IMG_STARFISH) / 255.0
    img = rgb2gray(imgcolor)
    
    # g(I)
    gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=2)
    
    # Initialization of the level-set.
    init_ls = ms.circle_level_set(img.shape, (163, 137), 135)
    
    # Callback for visual plotting
    callback = visual_callback_2d(imgcolor)
    
    # MorphGAC. 
    ms.morphological_geodesic_active_contour(gimg, iterations=100, 
                                             init_level_set=init_ls,
                                             smoothing=2, threshold=0.3,
                                             balloon=-1, iter_callback=callback)
Ejemplo n.º 18
0
def example_starfish():
    logging.info('Running: example_starfish (MorphGAC)...')

    # Load the image.
    imgcolor = imread(PATH_IMG_STARFISH) / 255.0
    img = rgb2gray(imgcolor)

    # g(I)
    gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=2)

    # Initialization of the level-set.
    init_ls = ms.circle_level_set(img.shape, (163, 137), 135)

    # Callback for visual plotting
    callback = visual_callback_2d(imgcolor)

    # MorphGAC.
    ms.morphological_geodesic_active_contour(gimg, iterations=100,
                                             init_level_set=init_ls,
                                             smoothing=2, threshold=0.3,
                                             balloon=-1, iter_callback=callback)
def test_morphsnakes_black():

    img = np.zeros((11, 11))
    ls = circle_level_set(img.shape, (5, 5), 3)

    ref_zeros = np.zeros(img.shape, dtype=np.int8)
    ref_ones = np.ones(img.shape, dtype=np.int8)

    acwe_ls = morphological_chan_vese(img, iterations=6, init_level_set=ls)
    assert_array_equal(acwe_ls, ref_zeros)

    gac_ls = morphological_geodesic_active_contour(img, iterations=6,
                                                   init_level_set=ls)
    assert_array_equal(gac_ls, ref_zeros)

    gac_ls2 = morphological_geodesic_active_contour(img, iterations=6,
                                                    init_level_set=ls,
                                                    balloon=1, threshold=-1,
                                                    smoothing=0)
    assert_array_equal(gac_ls2, ref_ones)

    assert acwe_ls.dtype == gac_ls.dtype == gac_ls2.dtype == np.int8
Ejemplo n.º 20
0
def gac3d(img, coord, iterations, smoothing, balloon, threshold):
    print('Running: snake_3d (MorphGAC)...')

    init_ls = ms.circle_level_set(img.shape, (coord[2], coord[1], coord[0]), 5)

    gimage = inverse_gaussian_gradient(img)
    ls = ms.morphological_geodesic_active_contour(gimage,
                                                  iterations=iterations,
                                                  init_level_set=init_ls,
                                                  smoothing=smoothing,
                                                  balloon=balloon,
                                                  threshold=threshold)
    return ls
Ejemplo n.º 21
0
def example_tumor():
    logging.info('Running: example_tumor (MorphGAC)...')

    # Load the image.
    #img = imread('images/project_2乳腺肿瘤视频.avi_000000.000.png')[..., 0] / 255.0
    img = imread('images/project_2乳腺肿瘤视频.avi_000004.850.png')[..., 0] / 255.0

    # g(I)
    gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=10)

    # Initialization of the level-set.
    init_ls = ms.circle_level_set(img.shape, (274, 501), 20)

    # Callback for visual plotting
    callback = visual_callback_2d(img)

    # MorphGAC.
    ms.morphological_geodesic_active_contour(gimg,
                                             iterations=160,
                                             init_level_set=init_ls,
                                             smoothing=1,
                                             threshold=0.45,
                                             balloon=1,
                                             iter_callback=callback)
Ejemplo n.º 22
0
    def getContours(self,iterations,threshold=0.69):
        
        # Morphological GAC
        image = img_as_float(self.image)
        gimage = inverse_gaussian_gradient(image)

        # Initial level set
        init_ls = np.zeros(image.shape, dtype=np.int8)
        init_ls[10:-10, 10:-10] = 1
        # List with intermediate results for plotting the evolution
        evolution = []
        callback = self.store_evolution_in(evolution)
        ls = morphological_geodesic_active_contour(gimage, iterations, init_ls,
                                                   smoothing=1, balloon=-1,
                                                   threshold=threshold,
                                                   iter_callback=callback)
        # before returning the snakes, put them in the event
        self.contours = ls
        return ls
Ejemplo n.º 23
0
def test_init_level_sets():

    image = np.zeros((6, 6))
    checkerboard_ls = morphological_chan_vese(image, 0, 'checkerboard')
    checkerboard_ref = np.array([[0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [0, 0, 0, 0, 0, 1],
                                 [1, 1, 1, 1, 1, 0]], dtype=np.int8)

    circle_ls = morphological_geodesic_active_contour(image, 0, 'circle')
    circle_ref = np.array([[0, 0, 0, 0, 0, 0],
                           [0, 0, 1, 1, 1, 0],
                           [0, 1, 1, 1, 1, 1],
                           [0, 1, 1, 1, 1, 1],
                           [0, 1, 1, 1, 1, 1],
                           [0, 0, 1, 1, 1, 0]], dtype=np.int8)

    assert_array_equal(checkerboard_ls, checkerboard_ref)
    assert_array_equal(circle_ls, circle_ref)
Ejemplo n.º 24
0
def morph_snakes_segmentation(snapshot,
                              start_point: Tuple[int, int],
                              start_radius: int,
                              num_iterations: int,
                              z=None):
    """
    Now only works with 2D image, thus always set z for now
    # TODO: do for 3D snapshot
    """
    assert snapshot.ndim == 3

    img = snapshot[z]
    # get the gaussian gradient for higher contrast
    gimg = ms.inverse_gaussian_gradient(img, alpha=5.0, sigma=5.0)

    # level_set is the starting
    level_set = ms.circle_level_set(img.shape, start_point, start_radius)
    mask = ms.morphological_geodesic_active_contour(gimg.astype('float64'),
                                                    num_iterations, level_set)
    # repeat the 2D mask so that it is the same size as the image
    mask = np.repeat(mask[np.newaxis, :, :], snapshot.shape[0], axis=0)

    return mask
Ejemplo n.º 25
0
##########


# b. Use morphsnakes to segment the objects in the image. 
#    Create the mask as shown in 'wks3_1_b.jpg'.

kernel = np.ones((5,5),np.uint8)
noiserem = cv2.morphologyEx(np.uint8(imgcolor),
                            cv2.MORPH_OPEN,
                            kernel)
cv2plt(noiserem)

gray = cv2.cvtColor(noiserem,cv2.COLOR_BGR2GRAY)
cv2plt(gray)
gray.shape

gray01 = gray/255.0
invg = ms.inverse_gaussian_gradient(gray01, alpha=700, sigma=3)
ls0 = ms.circle_level_set(gray01.shape, (200, 250), 250)

callback = ms.visual2d(cv2.cvtColor(imgcolor,cv2.COLOR_BGR2RGB))
lsf = ms.morphological_geodesic_active_contour(invg,
                                               iterations=500,
                                               init_level_set=ls0,
                                               smoothing=1,
                                               threshold=0.5,
                                               balloon=-1,
                                               iter_callback=callback)

cv2plt(lsf*255)
Ejemplo n.º 26
0
        gimg = ms.inverse_gaussian_gradient(img, alpha=1000, sigma=2)

        init_ls = np.zeros(img.shape, dtype=np.int8)
        cv2.fillPoly(
            init_ls, np.array([np.array(refPt, dtype=np.int32)],
                              dtype=np.int32), 255)

        # Callback for visual plotting
        callback = visual_callback_2d(imgcolor)

        # MorphGAC.
        final_level_set = ms.morphological_geodesic_active_contour(
            gimg,
            iterations=10000,
            init_level_set=init_ls,
            smoothing=3,
            threshold=0.4,
            balloon=-1,
            iter_callback=callback)

        plt.close('all')
        plt.imsave("contour_images/output.png", final_level_set)

        # np.set_printoptions(threshold='nan')

        mask = np.array(final_level_set, dtype=np.uint8) * 255

        # cv2.imshow("Final Mask", final_level_set * 255.)
        # cv2.waitKey(0)

        input_image = imread(args['image'])
Ejemplo n.º 27
0
startTime = time.time()
#np.hstack((frameLeft, frameRight))#在水平方向上拼接两帧图片
while (cap.isOpened()):
    ret, frame = cap.read()
    if frame is None:
        break
    frame = frame[132:600, 191:794]
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #开始处理
    gimg = ms.inverse_gaussian_gradient(frame, alpha=1000, sigma=5.48)
    init_ls = ms.circle_level_set(frame.shape, (100, 126), 20)
    callback = visual_callback_2d(frame)
    ms.morphological_geodesic_active_contour(gimg,
                                             iterations=45,
                                             init_level_set=init_ls,
                                             smoothing=1,
                                             threshold=0.31,
                                             balloon=1,
                                             iter_callback=callback)

    ##    scharrx=cv2.Scharr(frame,cv2.CV_64F,dx=1,dy=0)
    ##    scharrx=cv2.convertScaleAbs(scharrx)
    ##    scharry=cv2.Scharr(frame,cv2.CV_64F,dx=0,dy=1)
    ##    scharry=cv2.convertScaleAbs(scharry)
    ##    processedFrame=cv2.addWeighted(scharrx,0.5,scharry,0.5,0)

    ##    ret, binary = cv2.threshold(processedFrame,127,255,cv2.THRESH_BINARY)
    ##    contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    ##    cv2.drawContours(processedFrame,contours,-1,(0,0,255),3)
    ##    processedFrame=cv2.adaptiveThreshold(frame,255,\
    ##                cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,3,5)