Skip to content

Commit

Permalink
Add thresholding image functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
daid committed Sep 30, 2023
1 parent b7f409b commit 582dc3e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions include/sp2/graphics/image/threshold.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef SP2_GRAPHICS_IMAGE_THRESHOLD_H
#define SP2_GRAPHICS_IMAGE_THRESHOLD_H

#include <sp2/graphics/image.h>


namespace sp::image {

static inline sp::Image8 threshold(const sp::Image8& input, int threshold_value, int neg=0, int pos=255) {
sp::Image8 output{input.getSize()};
auto ptr_i = input.getPtr();
auto ptr_o = output.getPtr();
for(auto n=0; n<input.getSize().x*input.getSize().y;n++) {
if (*ptr_i >= threshold_value)
*ptr_o = pos;
else
*ptr_o = neg;
ptr_i++;
ptr_o++;
}
return output;
}

static inline sp::Image8 adaptiveThreshold(const sp::Image8& input, int blocksize, int offset, int neg=0, int pos=255) {
auto output = sp::image::gaussianBlur(input, blocksize);
auto ptr_i = input.getPtr();
auto ptr_o = output.getPtr();
for(auto n=0; n<input.getSize().x*input.getSize().y;n++) {
if (*ptr_i > *ptr_o + offset)
*ptr_o = pos;
else
*ptr_o = neg;
ptr_i++;
ptr_o++;
}
return output;
}

}//namespace sp::image


#endif//SP2_GRAPHICS_IMAGE_THRESHOLD_H

0 comments on commit 582dc3e

Please sign in to comment.