Skip to content

Commit

Permalink
fixing enforce_connectivity for maskSLIC
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Irving committed Sep 23, 2016
1 parent 13c8f52 commit f90ff9c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# maskSLIC
Simple linear iterative clustering (SLIC) in a region of interest

## Development
This code is still a work in progress. The following issues still need to be resolved:
- [ ] Fix the enforce connectivity method to work with maskSLIC

## Outline
This code demonstrates the adaption of SLIC for a defined region of interest.
The main contribution is in the placement of seed points within the ROI.
Expand All @@ -23,4 +19,5 @@ Figure 2: The final superpixel regions within the ROI

![superpixels](outputs/p2.png)


## Development
This code is still a work in progress.
2 changes: 1 addition & 1 deletion maskslic/_slic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
return np.asarray(nearest_segments)


def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments,
def _enforce_label_connectivity_cython(int[:, :, ::1] segments,
int[:, :, ::1] mask,
Py_ssize_t n_segments,
Py_ssize_t min_size,
Expand Down
19 changes: 10 additions & 9 deletions maskslic/slic_superpixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ def slic(image, n_segments=100, compactness=10., max_iter=10, sigma=0,
"""

if enforce_connectivity:
raise NotImplementedError("Enforce connectivity has not been implemented yet for maskSLIC.\n"
"Please set enforce connectivity to 'False' ")
# if enforce_connectivity:
# raise NotImplementedError("Enforce connectivity has not been implemented yet for maskSLIC.\n"
# "Please set enforce connectivity to 'False' ")

if slic_zero:
raise NotImplementedError("Slic zero has not been implemented yet for maskSLIC.")
Expand Down Expand Up @@ -404,14 +404,15 @@ def slic(image, n_segments=100, compactness=10., max_iter=10, sigma=0,
labels = _slic_cython(image, mask, segments, step, max_iter, spacing, slic_zero, only_dist=False)

if enforce_connectivity:
segment_size = depth * height * width / n_segments
if msk is None:
segment_size = depth * height * width / n_segments
else:
segment_size = msk.sum() / n_segments

min_size = int(min_size_factor * segment_size)
max_size = int(max_size_factor * segment_size)
labels = _enforce_label_connectivity_cython(labels,
mask,
n_segments,
min_size,
max_size)

labels = _enforce_label_connectivity_cython(labels, mask, n_segments, min_size, max_size)

if is_2d:
labels = labels[0]
Expand Down
28 changes: 28 additions & 0 deletions run_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# roi = img_as_float(chelsea_mask())
roi = roi[:, :, 3] > 0

# ~~~~~~~~~~~~ Example 1: maskSLIC ~~~~~~~~~~~~~

t1 = time.time()
# Note that compactness is defined differently because a grid is not used. Lower compactness for maskSLIC is equivalent
segments = seg.slic(img, compactness=10, seed_type='nplace', mask=roi, n_segments=12,
Expand All @@ -25,6 +27,8 @@
plt.contour(roi, contours=1, colors='red', linewidths=1)
plt.axis('off')

# ~~~~~~~~~~~ Example 2: SLIC ~~~~~~~~~~~~~~~~~

t1 = time.time()
segments = seg.slic(img, compactness=30, seed_type='grid', n_segments=80, plot_examples=False)
# segments[roi==0] = -1
Expand All @@ -37,3 +41,27 @@
plt.axis('off')
plt.show()


# ~~~~~~~~ Example 3: Enforcing connectivity ~~~~~~~~~~~~~

# without enforcing connectivity
# segments = seg.slic(img, compactness=4, seed_type='nplace', mask=roi, n_segments=12,
# recompute_seeds=True, enforce_connectivity=False)
#
# plt.figure()
# plt.title('maskSLIC without enforcing connectivity')
# plt.imshow(mark_boundaries(img, segments))
# plt.contour(roi, contours=1, colors='red', linewidths=1)
# plt.axis('off')
#
# # enforcing connectivity
# segments = seg.slic(img, compactness=4, seed_type='nplace', mask=roi, n_segments=12,
# recompute_seeds=True, enforce_connectivity=True)
#
# plt.figure()
# plt.title('maskSLIC enforcing connectivity')
# plt.imshow(mark_boundaries(img, segments))
# plt.contour(roi, contours=1, colors='red', linewidths=1)
# plt.axis('off')

plt.show()

0 comments on commit f90ff9c

Please sign in to comment.