Skip to content

Commit

Permalink
Add manhattan spatial distance option
Browse files Browse the repository at this point in the history
  • Loading branch information
Algy committed Aug 29, 2019
1 parent 02dbd21 commit 4769bfd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cfast_slic.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions cfast_slic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 11 additions & 3 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ namespace fslic {
void BaseContext<DistType>::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));
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions fast_slic/base_slic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 4769bfd

Please sign in to comment.