尝试在其自己的区域内提取边缘外部和内部的像素,目前我正在应用这样的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)
目前的结果是:
这个想法是让像素在边缘检测之外,例如这些区域:
如何在索贝尔过滤器外部和内部提取区域数组?
答案 0 :(得分: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)