python通过抵消轮廓/缩小多边形来分离圆形粒子

时间:2012-10-07 19:00:36

标签: python opencv numpy matplotlib

我是python的新手并且卡住了..

我想创建一个python脚本,允许我将图像上的相邻粒子分开,如下所示:

BEFORE

进入这样的不同区域:

AFTER

我被建议使用分水岭方法,据我所知它会给我这样的东西:

Watershed in 3D

编辑 实际上发现这是距离变换而不是分水岭

然后我可以使用阈值来分隔它们。遵循这个openCV watershed guide但它只能切割粒子。无法“转换”代码以执行我想要的操作。

然后我采取了另一种方法。试图使用openCV contours,这给了我很好的粒子轮廓。然后我一直在寻找一种简单的方法来执行多边形偏移,以便缩小边缘,如下所示:

Edge offset

使用偏移轮廓(多边形)的中心应该给出粒子的数量。但是我只能找到一种简单的方法来使用python进行边缘偏移/多边形收缩。

1 个答案:

答案 0 :(得分:7)

这是一个使用numpy,scipy和scikit-image(又名skimage)的脚本。它利用局部最大值提取和水分加标记(即连通分量提取)。

import numpy as np
import scipy.misc
import scipy.ndimage
import skimage.feature
import skimage.morphology

# parameters
THRESHOLD = 128

# read image
im = scipy.misc.imread("JPh65.png")
# convert to gray image
im = im.mean(axis=-1)
# find peaks
peak = skimage.feature.peak_local_max(im, threshold_rel=0.9, min_distance=10)
# make an image with peaks at 1
peak_im = np.zeros_like(im)
for p in peak:
    peak_im[p[0], p[1]] = 1
# label peaks
peak_label, _ = scipy.ndimage.label(peak_im)
# propagate peak labels with watershed
labels = skimage.morphology.watershed(255 - im, peak_label)
# limit watershed labels to area where the image is intense enough
result = labels * (im > THRESHOLD)