本文整理汇总了Python中scipy.ndimage.sobel方法的典型用法代码示例。如果您正苦于以下问题:Python ndimage.sobel方法的具体用法?Python ndimage.sobel怎么用?Python ndimage.sobel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.ndimage
的用法示例。
在下文中一共展示了ndimage.sobel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: image_gradient
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def image_gradient(image, horv):
"""apply a sobel filter to the image to approximate the gradient
Parameters
----------
image : ndarray(h, v)
image as an ndarray
horv : string
"h" or "v", direction of gradient.
Returns
-------
image_dir : ndarray(h, v)
directional gradient magnitude at each pixel
"""
axis = 1 if horv == 'h' else 0
grad = ndi.sobel(image, axis, mode='constant', cval=np.nan) / 8.0
return np.nan_to_num(grad)
示例2: test_sobel01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel01(self):
for type in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type)
t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
output = ndimage.sobel(array, 0)
assert_array_almost_equal(t, output)
示例3: test_sobel02
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel02(self):
for type in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type)
t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
output = numpy.zeros(array.shape, type)
ndimage.sobel(array, 0, output)
assert_array_almost_equal(t, output)
示例4: test_sobel03
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel03(self):
for type in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type)
t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1)
t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 0)
output = numpy.zeros(array.shape, type)
output = ndimage.sobel(array, 1)
assert_array_almost_equal(t, output)
示例5: test_sobel04
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel04(self):
for type in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type)
t = ndimage.sobel(array, -1)
output = ndimage.sobel(array, 1)
assert_array_almost_equal(t, output)
示例6: test_sobel01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel01(self):
for type_ in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type_)
t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
output = ndimage.sobel(array, 0)
assert_array_almost_equal(t, output)
示例7: test_sobel02
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel02(self):
for type_ in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type_)
t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
output = numpy.zeros(array.shape, type_)
ndimage.sobel(array, 0, output)
assert_array_almost_equal(t, output)
示例8: test_sobel03
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel03(self):
for type_ in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type_)
t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1)
t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 0)
output = numpy.zeros(array.shape, type_)
output = ndimage.sobel(array, 1)
assert_array_almost_equal(t, output)
示例9: test_sobel04
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_sobel04(self):
for type_ in self.types:
array = numpy.array([[3, 2, 5, 1, 4],
[5, 8, 3, 7, 1],
[5, 6, 9, 3, 5]], type_)
t = ndimage.sobel(array, -1)
output = ndimage.sobel(array, 1)
assert_array_almost_equal(t, output)
示例10: test_multiple_modes
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def test_multiple_modes():
# Test that the filters with multiple mode cababilities for different
# dimensions give the same result as applying a single mode.
arr = np.array([[1., 0., 0.],
[1., 1., 0.],
[0., 0., 0.]])
mode1 = 'reflect'
mode2 = ['reflect', 'reflect']
assert_equal(sndi.gaussian_filter(arr, 1, mode=mode1),
sndi.gaussian_filter(arr, 1, mode=mode2))
assert_equal(sndi.prewitt(arr, mode=mode1),
sndi.prewitt(arr, mode=mode2))
assert_equal(sndi.sobel(arr, mode=mode1),
sndi.sobel(arr, mode=mode2))
assert_equal(sndi.laplace(arr, mode=mode1),
sndi.laplace(arr, mode=mode2))
assert_equal(sndi.gaussian_laplace(arr, 1, mode=mode1),
sndi.gaussian_laplace(arr, 1, mode=mode2))
assert_equal(sndi.maximum_filter(arr, size=5, mode=mode1),
sndi.maximum_filter(arr, size=5, mode=mode2))
assert_equal(sndi.minimum_filter(arr, size=5, mode=mode1),
sndi.minimum_filter(arr, size=5, mode=mode2))
assert_equal(sndi.gaussian_gradient_magnitude(arr, 1, mode=mode1),
sndi.gaussian_gradient_magnitude(arr, 1, mode=mode2))
assert_equal(sndi.uniform_filter(arr, 5, mode=mode1),
sndi.uniform_filter(arr, 5, mode=mode2))
示例11: overlay_edges
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def overlay_edges(slice_one, slice_two, sharper=True):
"""
Makes a composite image with edges from second image overlaid on first.
It will be in colormapped (RGB format) already.
"""
if slice_one.shape != slice_two.shape:
raise ValueError("slices' dimensions do not match: "
" {} and {} ".format(slice_one.shape, slice_two.shape))
# simple filtering to remove noise, while supposedly keeping edges
slice_two = medfilt2d(slice_two, kernel_size=cfg.median_filter_size)
# extracting edges
edges = np.hypot(sobel(slice_two, axis=0, mode='constant'),
sobel(slice_two, axis=1, mode='constant'))
# trying to remove weak edges
if not sharper: # level of removal
edges = med_filter(max_filter(min_filter(edges)))
else:
edges = min_filter(min_filter(max_filter(min_filter(edges))))
edges_color_mapped = hot_cmap(edges, alpha=cfg.alpha_edge_overlay_alignment)
composite = gray_cmap(slice_one, alpha=cfg.alpha_background_slice_alignment)
composite[edges_color_mapped>0] = edges_color_mapped[edges_color_mapped>0]
# mask_rgba = np.dstack([edges>0] * 4)
# composite[mask_rgba] = edges_color_mapped[mask_rgba]
return composite
示例12: dwi_overlay_edges
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def dwi_overlay_edges(slice_one, slice_two):
"""
Makes a composite image with edges from second image overlaid on first.
It will be in colormapped (RGB format) already.
"""
if slice_one.shape != slice_two.shape:
raise ValueError("slices' dimensions do not match: "
" {} and {} ".format(slice_one.shape, slice_two.shape))
# simple filtering to remove noise, while supposedly keeping edges
slice_two = medfilt2d(slice_two, kernel_size=cfg.median_filter_size)
# extracting edges
edges = med_filter(np.hypot(sobel(slice_two, axis=0, mode='constant'),
sobel(slice_two, axis=1, mode='constant')))
edges_color_mapped = hot_cmap(edges, alpha=cfg.alpha_edge_overlay_alignment)
composite = gray_cmap(slice_one, alpha=cfg.alpha_background_slice_alignment)
composite[edges_color_mapped>0] = edges_color_mapped[edges_color_mapped>0]
# mask_rgba = np.dstack([edges>0] * 4)
# composite[mask_rgba] = edges_color_mapped[mask_rgba]
return composite
示例13: filter
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def filter(self, array, *args, **kwargs):
shp = array.shape
array = array.reshape((np.prod(shp[0:-2]),) + shp[-2:]) # reshape to (nch, ny, nx)
for k, arr in enumerate(np.abs(array)): # loop over channels
dx = ndimage.sobel(arr, 0) # horizontal derivative
dy = ndimage.sobel(arr, 1) # vertical derivative
array[k, ...] = np.hypot(dx, dy) # magnitude
array = array.reshape(shp) # back to original shape
return array
示例14: sobel
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def sobel(*args, **kwargs):
return Sobel(*args, **kwargs).run(**kwargs)
示例15: _init_coords
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import sobel [as 别名]
def _init_coords(self):
logging.info('peaks: starting')
# Edge detection.
edges = ndimage.generic_gradient_magnitude(
self.canvas.image.astype(np.float32),
ndimage.sobel)
# Adaptive thresholding.
sigma = 49.0 / 6.0
thresh_image = np.zeros(edges.shape, dtype=np.float32)
ndimage.gaussian_filter(edges, sigma, output=thresh_image, mode='reflect')
filt_edges = edges > thresh_image
del edges, thresh_image
# This prevents a border effect where the large amount of masked area
# screws up the distance transform below.
if (self.canvas.restrictor is not None and
self.canvas.restrictor.mask is not None):
filt_edges[self.canvas.restrictor.mask] = 1
logging.info('peaks: filtering done')
dt = ndimage.distance_transform_edt(1 - filt_edges).astype(np.float32)
logging.info('peaks: edt done')
# Use a specifc seed for the noise so that results are reproducible
# regardless of what happens before the policy is called.
state = np.random.get_state()
np.random.seed(42)
idxs = skimage.feature.peak_local_max(
dt + np.random.random(dt.shape) * 1e-4,
indices=True, min_distance=3, threshold_abs=0, threshold_rel=0)
np.random.set_state(state)
# After skimage upgrade to 0.13.0 peak_local_max returns peaks in
# descending order, versus ascending order previously. Sort ascending to
# maintain historic behavior.
idxs = np.array(sorted((z, y, x) for z, y, x in idxs))
logging.info('peaks: found %d local maxima', idxs.shape[0])
self.coords = idxs