python - scipy sobel边缘检测,提取外部像素

标签 python numpy scipy sobel

尝试提取其自身区域内边缘外部和内部的像素,目前我正在应用 scipy Sobel 过滤器,如下所示:

im = scipy.misc.imread(filename)
im = im.astype('int32')
dx = ndimage.sobel(im, axis=0)
dy = ndimage.sobel(im, axis=1)

mag = np.hypot(dx, dy)  
mag *= 255.0 / np.max(mag)

scipy.misc.imsave('sobel.jpg', mag)

目前结果是:

enter image description here enter image description here

这个想法是获取边缘检测之外的像素,例如这些区域:

enter image description here

如何提取索贝尔滤波器外部和内部区域的数组?

最佳答案

这是一种使用交互式图像分割的方法。在这种方法中,您必须手动标记一些前景像素和一些背景像素,如下所示:

Labeled image

(我在 MS Paint 中进行了标记。)下面的代码使用函数 skimage.segmentation.random_walker 进行图像分割,并生成此分割图像:

enter image description here

(这种方法还可以处理具有更复杂背景区域的图像。)代码如下:

import skimage
import skimage.viewer
import skimage.segmentation
import skimage.data
import skimage.io
import matplotlib.pyplot as plt
import numpy as np

img = skimage.io.imread("D:/Users/Pictures/img.jpg")
imgLabeled = skimage.io.imread("D:/Users/Pictures/imgLabeled.jpg")

redChannel = imgLabeled[:,:,0]
greenChannel = imgLabeled[:,:,1]
blueChannel = imgLabeled[:,:,2]
markers = np.zeros(img.shape,dtype=np.uint)
markers[(redChannel < 20) & (greenChannel > 210) & (blueChannel < 20)] = 1
markers[(redChannel < 20) & (greenChannel < 20) & (blueChannel > 210)] = 2
plt.imshow(markers)

labels = skimage.segmentation.random_walker(img, markers, beta=1000, mode='cg')

seg1 = np.copy(img)
seg1[labels==2] = 0
seg2 = np.copy(img)
seg2[labels==1] = 0

# plt.imsave("D:/Users/Pictures/imgSeg.png",seg1)

plt.figure()
plt.imshow(seg1)
plt.figure()
plt.imshow(seg2)

关于python - scipy sobel边缘检测,提取外部像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41166829/

相关文章:

python - 如何更改 numpy recarray 某些列的数据类型?

python - 计算 numpy 数组中一列中相同值的数量

python - Scipy:数组中的稀疏指示矩阵

python - 使用python的并行数值积分

python - numpy svd : is there a way to find only the first singular vectors instead of doing full svd?

Python 元组到 JSON 输出

python - 如何使用嵌套的 for 循环在 python 中打印出以下模式?

python - ndarray 上的任意一维切片(沿轴的元素) - NumPy

python - 同一属性的 2 个名称

python - 无法在 Canvas 上进行垂直滚动