From 4769bfde813b526838e9de5ac800ca8f476e2b2c Mon Sep 17 00:00:00 2001 From: Algy Date: Thu, 29 Aug 2019 09:36:57 +0900 Subject: [PATCH] Add manhattan spatial distance option --- cfast_slic.pxd | 7 ++++++- cfast_slic.pyx | 3 +++ context.cpp | 14 +++++++++++--- context.h | 1 + fast_slic/base_slic.py | 6 ++++-- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/cfast_slic.pxd b/cfast_slic.pxd index 7a460d0..6aec287 100644 --- a/cfast_slic.pxd +++ b/cfast_slic.pxd @@ -39,6 +39,8 @@ cdef extern from "context.h" namespace "fslic": bool preemptive float preemptive_thres + bool manhattan_spatial_dist + Context(int H, int W, int K, const uint8_t* image, Cluster *clusters) except + void initialize_clusters() nogil void initialize_state() nogil @@ -55,6 +57,8 @@ cdef extern from "context.h" namespace "fslic": bool preemptive float preemptive_thres + bool manhattan_spatial_dist + ContextRealDist(int H, int W, int K, const uint8_t* image, Cluster *clusters) except + void initialize_clusters() nogil void initialize_state() nogil @@ -108,8 +112,9 @@ cdef class SlicModel: cdef public object real_dist cdef public object real_dist_type cdef public object convert_to_lab - cdef public object preemptive + cdef public object preemptive cdef public float preemptive_thres + cdef public object manhattan_spatial_dist cpdef void initialize(self, const uint8_t [:, :, ::1] image) cpdef iterate(self, const uint8_t [:, :, ::1] image, int max_iter, float compactness, float min_size_factor, uint8_t subsample_stride) diff --git a/cfast_slic.pyx b/cfast_slic.pyx index b157c0b..1b549fd 100644 --- a/cfast_slic.pyx +++ b/cfast_slic.pyx @@ -37,6 +37,7 @@ cdef class SlicModel: self.initialized = False self.preemptive = False self.preemptive_thres = 0.05 + self.manhattan_spatial_dist = True def copy(self): result = SlicModel(self.num_components) @@ -177,6 +178,7 @@ cdef class SlicModel: context.convert_to_lab = self.convert_to_lab context.preemptive = self.preemptive context.preemptive_thres = self.preemptive_thres + context.manhattan_spatial_dist = self.manhattan_spatial_dist with nogil: context.initialize_state() context.iterate( @@ -222,6 +224,7 @@ cdef class SlicModel: context_real_dist.convert_to_lab = self.convert_to_lab context_real_dist.preemptive = self.preemptive context_real_dist.preemptive_thres = self.preemptive_thres + context_real_dist.manhattan_spatial_dist = self.manhattan_spatial_dist with nogil: context_real_dist.initialize_state() context_real_dist.iterate( diff --git a/context.cpp b/context.cpp index f61de91..cb098c8 100644 --- a/context.cpp +++ b/context.cpp @@ -36,9 +36,17 @@ namespace fslic { void BaseContext::set_spatial_patch() { float coef = 1.0f / ((float)S / compactness); int16_t S_2 = 2 * S; - for (int16_t i = 0; i <= S_2; i++) { - for (int16_t j = 0; j <= S_2; j++) { - spatial_dist_patch.get(i, j) = (DistType)(coef * (fast_abs(i - S) + fast_abs(j - S))); + if (manhattan_spatial_dist) { + for (int16_t i = 0; i <= S_2; i++) { + for (int16_t j = 0; j <= S_2; j++) { + spatial_dist_patch.get(i, j) = (DistType)(coef * (fast_abs(i - S) + fast_abs(j - S))); + } + } + } else { + for (int16_t i = 0; i <= S_2; i++) { + for (int16_t j = 0; j <= S_2; j++) { + spatial_dist_patch.get(i, j) = (DistType)(coef * hypotf(i - S, j - S)); + } } } } diff --git a/context.h b/context.h index b25dddf..e3284ad 100644 --- a/context.h +++ b/context.h @@ -31,6 +31,7 @@ namespace fslic { bool preemptive = false; float preemptive_thres = 0.01; + bool manhattan_spatial_dist = true; protected: int H, W, K; const uint8_t* image; diff --git a/fast_slic/base_slic.py b/fast_slic/base_slic.py index 1e7e5d2..24f0b0c 100644 --- a/fast_slic/base_slic.py +++ b/fast_slic/base_slic.py @@ -9,9 +9,10 @@ def __init__(self, compactness=20, min_size_factor=0.05, subsample_stride=3, - convert_to_lab=False, + convert_to_lab=True, preemptive=False, - preemptive_thres=0.05): + preemptive_thres=0.05, + manhattan_spatial_dist=True): self.compactness = compactness self.subsample_stride = subsample_stride self.min_size_factor = min_size_factor @@ -21,6 +22,7 @@ def __init__(self, self.convert_to_lab = convert_to_lab self._slic_model.preemptive = preemptive self._slic_model.preemptive_thres = preemptive_thres + self._slic_model.manhattan_spatial_dist = manhattan_spatial_dist @property def convert_to_lab(self):