Scikit-image扩展活动轮廓(蛇)

时间:2017-08-17 13:15:09

标签: image-processing scikit-image

我按照这个link的例子。但是,轮廓从初始点收缩。可以做出扩展的轮廓吗?我想要像显示的图像。左边的图像是它的样子,右边的图像是我想要的样子 - 展开而不是收缩。红色圆圈是起始点,蓝色轮廓是在n次迭代之后。有没有我可以设置的参数 - 我看了所有这些参数,但似乎没有设置它。另外,当提到' active-contour'时,它通常会假设轮廓收缩吗?我读了this paper并认为它既可以收缩也可以扩展。

img = data.astronaut()
img = rgb2gray(img)

s = np.linspace(0, 2*np.pi, 400)
x = 220 + 100*np.cos(s)
y = 100 + 100*np.sin(s)
init = np.array([x, y]).T

if not new_scipy:
    print('You are using an old version of scipy. '
          'Active contours is implemented for scipy versions '
          '0.14.0 and above.')

if new_scipy:
    snake = active_contour(gaussian(img, 3),
                           init, alpha=0.015, beta=10, gamma=0.001)

    fig = plt.figure(figsize=(7, 7))
    ax = fig.add_subplot(111)
    plt.gray()
    ax.imshow(img)
    ax.plot(init[:, 0], init[:, 1], '--r', lw=3)
    ax.plot(snake[:, 0], snake[:, 1], '-b', lw=3)
    ax.set_xticks([]), ax.set_yticks([])
    ax.axis([0, img.shape[1], img.shape[0], 0])

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要的是将气球力添加到Snakes。

Snake的算法定义为使其最小化3种能量-连续性,曲率和渐变,分别对应于代码中的alphabetagamma。当将点(曲线上)拉得越来越近(即收缩)时,前两个(统称为内部能量)会最小化。如果它们扩展了,那么能量就会增加,这是蛇算法所不允许的。

但是1987年提出的最初算法存在一些问题。问题之一是在平坦区域(梯度为零)中,算法无法收敛并且什么也不做。提出了几种修改方案来解决这个问题。有趣的解决方案是LD Cohen在1989年提出的“气球力”。

气球力在图像的非信息区域(即图像的梯度太小而无法将轮廓推向边界的区域)引导轮廓。负值将缩小轮廓,而正值将缩小轮廓。将此设置为零将禁用气球力。

另一个改进是-形态蛇,它在二进制数组上使用形态算子(例如膨胀或腐蚀),而不是在浮点数组上求解PDE,这是活动轮廓的标准方法。这使得形态蛇比传统蛇更快,并且在数值上更稳定。

使用上述两个改进的Scikit-image实现是morphological_geodesic_active_contour。它具有参数balloon

在没有任何真实原始图像的情况下,让我们创建一个玩具图像并对其进行处理:

import numpy as np
import matplotlib.pyplot as plt
from skimage.segmentation import morphological_geodesic_active_contour, inverse_gaussian_gradient
from skimage.color import rgb2gray
from skimage.util import img_as_float
from PIL import Image, ImageDraw
   
im = Image.new('RGB', (250, 250), (128, 128, 128))
draw = ImageDraw.Draw(im)
draw.polygon(((50, 200), (200, 150), (150, 50)), fill=(255, 255, 0), outline=(0, 0, 0))
im = np.array(im)
im = rgb2gray(im)
im = img_as_float(im)
plt.imshow(im, cmap='gray')

这给我们下面的图片

enter image description here

现在让我们创建一个函数来帮助我们存储迭代:

def store_evolution_in(lst):
    """Returns a callback function to store the evolution of the level sets in
    the given list.
    """

    def _store(x):
        lst.append(np.copy(x))

    return _store

此方法需要对图像进行预处理以突出显示轮廓。尽管用户可能想定义自己的版本,但是可以使用功能inverse_gaussian_gradient完成。 MorphGAC分割的质量很大程度上取决于此预处理步骤。

gimage = inverse_gaussian_gradient(im)

在下面定义起点-正方形

init_ls = np.zeros(im.shape, dtype=np.int8)
init_ls[120:-100, 120:-100] = 1

列出中间结果的列表,以绘制演变过程

evolution = []
callback = store_evolution_in(evolution)

现在具有气球膨胀的morphological_geodesic_active_contour所需的魔术线如下:

ls = morphological_geodesic_active_contour(gimage, 50, init_ls, 
                                           smoothing=1, balloon=1,
                                            threshold=0.7,
                                           iter_callback=callback)

现在让我们绘制结果:

fig, axes = plt.subplots(1, 2, figsize=(8, 8))
ax = axes.flatten()

ax[0].imshow(im, cmap="gray")
ax[0].set_axis_off()
ax[0].contour(ls, [0.5], colors='b')
ax[0].set_title("Morphological GAC segmentation", fontsize=12)

ax[1].imshow(ls, cmap="gray")
ax[1].set_axis_off()
contour = ax[1].contour(evolution[0], [0.5], colors='r')
contour.collections[0].set_label("Starting Contour")
contour = ax[1].contour(evolution[5], [0.5], colors='g')
contour.collections[0].set_label("Iteration 5")
contour = ax[1].contour(evolution[-1], [0.5], colors='b')
contour.collections[0].set_label("Last Iteration")
ax[1].legend(loc="upper right")
title = "Morphological GAC Curve evolution"
ax[1].set_title(title, fontsize=12)

plt.show()

enter image description here

红色正方形是我们的起点(初始轮廓),蓝色轮廓来自最终迭代。

答案 1 :(得分:-1)

如果查看Scikit active contour,您会看到您有以下参数:

w_line - 控制对亮度的吸引力。使用负值吸引暗区

所以在你的问题中你想扩展到黑暗边缘,因此我认为你应该使用负面的w_line。