mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-02 13:03:48 +08:00
MISC move felzenszwalb_cy.pyx to _felzenszwalb_cy.pyx, don't use xrange when not necessary
This commit is contained in:
@@ -12,7 +12,7 @@ a basis for more sophisticated algorithms such as CRFs.
|
||||
|
||||
Felzenszwalb's efficient graph based segmentation
|
||||
-------------------------------------------------
|
||||
This fast 2d image segmentation algorithm, proposed in [1]_ is popular in the
|
||||
This fast 2D image segmentation algorithm, proposed in [1]_ is popular in the
|
||||
computer vision community.
|
||||
The algorithm has a single ``scale`` parameter that influences the segment
|
||||
size. The actual size and number of segments can vary greatly, depending on
|
||||
@@ -25,9 +25,9 @@ local contrast.
|
||||
Quickshift image segmentation
|
||||
-----------------------------
|
||||
|
||||
Quickshift is a relatively recent 2d image segmentation algorithm, based on an
|
||||
Quickshift is a relatively recent 2D image segmentation algorithm, based on an
|
||||
approximation of kernelized mean-shift. Therefore it belongs to the family of
|
||||
local mode-seeking algorithms and is applied to the 5d space consisting of
|
||||
local mode-seeking algorithms and is applied to the 5D space consisting of
|
||||
color information and image location [2]_.
|
||||
|
||||
One of the benefits of quickshift is that it actually computes a
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import warnings
|
||||
import numpy as np
|
||||
|
||||
from .felzenszwalb_cy import _felzenszwalb_grey
|
||||
from ._felzenszwalb_cy import _felzenszwalb_grey
|
||||
|
||||
|
||||
def felzenszwalb(image, scale=1, sigma=0.8, min_size=20):
|
||||
@@ -60,7 +60,7 @@ def felzenszwalb(image, scale=1, sigma=0.8, min_size=20):
|
||||
" wanted?" % image.shape[2])
|
||||
segmentations = []
|
||||
# compute quickshift for each channel
|
||||
for c in xrange(n_channels):
|
||||
for c in range(n_channels):
|
||||
channel = np.ascontiguousarray(image[:, :, c])
|
||||
s = _felzenszwalb_grey(channel, scale=scale, sigma=sigma,
|
||||
min_size=min_size)
|
||||
|
||||
@@ -45,7 +45,7 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1,
|
||||
"""
|
||||
image = np.atleast_3d(image)
|
||||
if image.shape[2] != 3:
|
||||
ValueError("Only 3-channel 2d images are supported.")
|
||||
ValueError("Only 3-channel 2D images are supported.")
|
||||
image = ndimage.gaussian_filter(img_as_float(image), [sigma, sigma, 0])
|
||||
if convert2lab:
|
||||
image = rgb2lab(image)
|
||||
@@ -82,21 +82,21 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1,
|
||||
cdef np.float_t* current_distance
|
||||
cdef np.float_t* current_pixel
|
||||
cdef double tmp
|
||||
for i in xrange(max_iter):
|
||||
for i in range(max_iter):
|
||||
distance.fill(np.inf)
|
||||
changes = 0
|
||||
current_mean = <np.float_t*> means.data
|
||||
# assign pixels to means
|
||||
for k in xrange(n_means):
|
||||
for k in range(n_means):
|
||||
# compute windows:
|
||||
y_min = int(max(current_mean[0] - 2 * step, 0))
|
||||
y_max = int(min(current_mean[0] + 2 * step, height))
|
||||
x_min = int(max(current_mean[1] - 2 * step, 0))
|
||||
x_max = int(min(current_mean[1] + 2 * step, width))
|
||||
for y in xrange(y_min, y_max):
|
||||
for y in range(y_min, y_max):
|
||||
current_pixel = &image_p[5 * (y * width + x_min)]
|
||||
current_distance = &distance_p[y * width + x_min]
|
||||
for x in xrange(x_min, x_max):
|
||||
for x in range(x_min, x_max):
|
||||
mean_entry = current_mean
|
||||
dist_mean = 0
|
||||
for c in range(5):
|
||||
@@ -117,7 +117,7 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1,
|
||||
break
|
||||
# recompute means:
|
||||
means_list = [np.bincount(nearest_mean.ravel(),
|
||||
image_yx[:, :, j].ravel()) for j in xrange(5)]
|
||||
image_yx[:, :, j].ravel()) for j in range(5)]
|
||||
in_mean = np.bincount(nearest_mean.ravel())
|
||||
in_mean[in_mean == 0] = 1
|
||||
means = (np.vstack(means_list) / in_mean).T.copy("C")
|
||||
|
||||
@@ -11,8 +11,8 @@ def configuration(parent_package='', top_path=None):
|
||||
|
||||
config = Configuration('segmentation', parent_package, top_path)
|
||||
|
||||
cython(['felzenszwalb_cy.pyx'], working_path=base_path)
|
||||
config.add_extension('felzenszwalb_cy', sources=['felzenszwalb_cy.c'],
|
||||
cython(['_felzenszwalb_cy.pyx'], working_path=base_path)
|
||||
config.add_extension('_felzenszwalb_cy', sources=['_felzenszwalb_cy.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
cython(['_quickshift.pyx'], working_path=base_path)
|
||||
config.add_extension('_quickshift', sources=['_quickshift.c'],
|
||||
|
||||
Reference in New Issue
Block a user