【问题标题】:scipy sobel edge detection, extract outer pixelsscipy sobel边缘检测,提取外部像素
【发布时间】:2017-05-01 05:07:16
【问题描述】:

尝试在其自己的区域内提取边缘内外的像素,目前我正在应用 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)

目前的结果是:

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

如何提取 sobel 过滤器内外区域的数组?

【问题讨论】:

  • 您必须定义外部内部。从视觉上看,它可能看起来很简单,因为您知道要提取什么(一个人),但图像是计算机的一组数字,它不知道更高级别的连接性。因此,对于该特定示例,您可以逐行并从左侧和右侧屏蔽像素,直到您在 sobel 渐变中选择一个 peak。但是,如果你想要一个通用的方法,它并不像你想象的那么简单,也不会像对sobel过滤器做一些magic那么简单。这是xkcd.com/1425的一个例子
  • 如果您的图像简单易行,请尝试在opencvscikit-image 中使用轮廓的东西
  • 您正在尝试解决图像“分割”问题,这通常是一个非常困难的问题,并且多年来一直是一个非常活跃的研究领域。但是,由于图像中的背景非常简单(只是一堵白墙),也许一些简单的方法会起作用。在网上找到一些好的图像分割代码可能是最简单的。
  • 你需要这个做什么?手动标记一些前景和背景像素是否可以接受?
  • @ImanolLuengo - 不幸的是,图像轮廓无法对图像进行半准确的呈现。

标签: python numpy scipy sobel


【解决方案1】:

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

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

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2015-08-09
    • 2016-10-03
    • 2011-02-25
    • 2021-06-02
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多