# HG changeset patch # User Carnë Draug # Date 1412882786 -3600 # Node ID 0d49dd1a084cf77bf226a518dd7f96e8d0ee1f51 # Parent fd72bb9e1e9fcbf71dd20729aecb973cdd6a02ed imerode.cc: specialization for boolean dilation and erosion * imerode.cc: rather than check in the loop if image is boolean, have a separate function to handle them. Not much difference for binary images but almost twice as fast for other types. diff --git a/src/imerode.cc b/src/imerode.cc --- a/src/imerode.cc +++ b/src/imerode.cc @@ -128,7 +128,7 @@ // argument, it's all done at compile time so we get better performance. // If erosion is false, we perform dilation instead template -static void +inline static void erode_line (const P* in, P* out, const octave_idx_type* offsets, const P* height, const octave_idx_type& nnz, const octave_idx_type& line_length) { @@ -141,20 +141,12 @@ if (erosion) { if (in[offsets[nnz_idx]] < out[line_idx]) - { - out[line_idx] = in[offsets[nnz_idx]]; - if (typeid (P) == typeid (bool)) - break; - } + out[line_idx] = in[offsets[nnz_idx]]; } else { if (in[offsets[nnz_idx]] > out[line_idx]) - { - out[line_idx] = in[offsets[nnz_idx]]; - if (typeid (P) == typeid (bool)) - break; - } + out[line_idx] = in[offsets[nnz_idx]]; } } else @@ -179,6 +171,39 @@ } } +// For the specific case of boolean dilation/erosion, we may be able to +// break from the loop sooner. Also, there is non-flat binary erosion +// and dilation. +template +inline static void +erode_line (const bool* in, bool* out, const octave_idx_type* offsets, const bool* height, + const octave_idx_type& nnz, const octave_idx_type& line_length) +{ + for (octave_idx_type line_idx = 0; line_idx < line_length; line_idx++) + { + for (octave_idx_type nnz_idx = 0; nnz_idx < nnz; nnz_idx++) + { + if (erosion) + { + if (! in[offsets[nnz_idx]]) + { + out[line_idx] = false; + break; + } + } + else + { + if (in[offsets[nnz_idx]]) + { + out[line_idx] = true; + break; + } + } + } + in++; + } +} + template static void erode_nd (const P* in, const dim_vector& in_cd,