ArrayFire: image_processing/adaptive_thresholding.cpp (original) (raw)
#include
#include
#include
using namespace af;
using std::abs;
typedef enum { MEAN = 0, MEDIAN, MINMAX_AVG } LocalThresholdType;
array threshold(const array &in, float thresholdValue) {
int channels = in.dims(2);
if (channels > 1) ret_val = colorSpace(in, AF_GRAY, AF_RGB);
ret_val =
(ret_val < thresholdValue) * 0.0f + 255.0f * (ret_val > thresholdValue);
return ret_val;
}
array adaptiveThreshold(const array &in, LocalThresholdType kind,
int window_size, int constnt) {
int wr = window_size;
if (kind == MEAN) {
ret_val = (diff < constnt) * 0.f + 255.f * (diff > constnt);
} else if (kind == MEDIAN) {
array diff = medf - ret_val;
ret_val = (diff < constnt) * 0.f + 255.f * (diff > constnt);
} else if (kind == MINMAX_AVG) {
ret_val = (diff < constnt) * 0.f + 255.f * (diff > constnt);
}
ret_val = 255.f - ret_val;
return ret_val;
}
array iterativeThreshold(const array &in) {
float T = mean(ret_val);
bool isContinue = true;
while (isContinue) {
array region1 = (ret_val > T) * ret_val;
array region2 = (ret_val <= T) * ret_val;
float r1_avg = mean(region1);
float r2_avg = mean(region2);
float tempT = (r1_avg + r2_avg) / 2.0f;
if (abs(tempT - T) < 0.01f) { break; }
T = tempT;
}
return threshold(ret_val, T);
}
int main(int argc, char **argv) {
try {
int device = argc > 1 ? atoi(argv[1]) : 0;
loadImage(ASSETS_DIR "/examples/images/sudoku.jpg", true);
array mnt = adaptiveThreshold(sudoku, MEAN, 37, 10);
array mdt = adaptiveThreshold(sudoku, MEDIAN, 7, 4);
array mmt = adaptiveThreshold(sudoku, MINMAX_AVG, 11, 4);
array itt = 255.0f - iterativeThreshold(sudoku);
af::Window wnd("Adaptive Thresholding Algorithms");
printf("Press ESC while the window is in focus to exit\n");
while (!wnd.close()) {
wnd.grid(2, 3);
wnd(0, 0).image(sudoku / 255, "Input");
wnd(1, 0).image(mnt, "Adap. Threshold(Mean)");
wnd(0, 1).image(mdt, "Adap. Threshold(Median)");
wnd(1, 1).image(mmt, "Adap. Threshold(Avg. Min,Max)");
wnd(0, 2).image(itt, "Iterative Threshold");
wnd.show();
}
fprintf(stderr, "%s\n", e.what());
throw;
}
return 0;
}
Window object to render af::arrays.
A multi dimensional data container.
dim4 dims() const
Get dimensions of the array.
array copy() const
Perform deep copy of the array.
An ArrayFire exception class.
virtual const char * what() const
Returns an error message for the exception in a string format.
AFAPI array abs(const array &in)
C++ Interface to calculate the absolute value.
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
C++ Interface to generate an array with elements set to a specified value.
AFAPI void setDevice(const int device)
Sets the current device.
AFAPI array colorSpace(const array &image, const CSpace to, const CSpace from)
C++ Interface wrapper for colorspace conversion.
AFAPI array maxfilt(const array &in, const dim_t wind_length=3, const dim_t wind_width=3, const borderType edge_pad=AF_PAD_ZERO)
C++ Interface for maximum filter.
AFAPI array medfilt(const array &in, const dim_t wind_length=3, const dim_t wind_width=3, const borderType edge_pad=AF_PAD_ZERO)
C++ Interface for median filter.
AFAPI array minfilt(const array &in, const dim_t wind_length=3, const dim_t wind_width=3, const borderType edge_pad=AF_PAD_ZERO)
C++ Interface for minimum filter.
AFAPI array loadImage(const char *filename, const bool is_color=false)
C++ Interface for loading an image.
AFAPI array convolve(const array &signal, const array &filter, const convMode mode=AF_CONV_DEFAULT, const convDomain domain=AF_CONV_AUTO)
C++ Interface for convolution any(one through three) dimensional signals.
AFAPI array mean(const array &in, const dim_t dim=-1)
C++ Interface for mean.