#include <cmath>
#include <cstdio>
#include <cstdlib>
arraythreshold(
const array& in,
floatthresholdValue) {
intchannels = in.
dims(2);
ret_val =
(ret_val < thresholdValue) * 0.0f + 255.0f * (ret_val > thresholdValue);
return ret_val;
}
intchannels = in.
dims(2);
if (channels > 1)
else
gray = in;
arrayhist = histogram(gray, 256, 0.0f, 255.0f);
arraysumB = accum(wts * hist);
arraymeanB = sumB / wtB;
float lastElemInSumB;
sumB(
seq(255, 255, 1)).
host((
void*)&lastElemInSumB);
arraymeanF = (lastElemInSumB - sumB) / wtF;
arraymDiff = meanB - meanF;
arrayinterClsVar = wtB * wtF * mDiff * mDiff;
float max = af::max<float>(interClsVar);
floatthreshold2 = where(interClsVar == max).
scalar<
unsigned>();
arraythreshIdx = where(interClsVar >= max);
float threshold1 =
threshIdx.elements() > 0 ? threshIdx.scalar<unsigned>() : 0.0f;
return threshold(gray, (threshold1 + threshold2) / 2.0f);
}
int main(int argc, char** argv) {
try {
int device = argc > 1 ? atoi(argv[1]) : 0;
loadImage(ASSETS_DIR
"/examples/images/noisy_square.png",
false);
bimodal =
resize(0.75f, bimodal);
arraybt = threshold(bimodal, 180.0f);
arrayot = otsu(bimodal);
af::Windowwnd(1536, 1024,
"Binary Thresholding Algorithms");
printf("Press ESC while the window is in focus to proceed to exit\n");
wnd.grid(3, 3);
wnd(0, 1).setAxesTitles("Bins", "Frequency");
wnd(1, 1).setAxesTitles("Bins", "Frequency");
wnd(2, 1).setAxesTitles("Bins", "Frequency");
while (!wnd.close()) {
wnd(0, 0).image(bimodal / 255, "Input Image");
wnd(1, 0).image(bimodal / 255, "Input Image");
wnd(2, 0).image(smooth / 255, "Input Smoothed by Gaussian Filter");
wnd(0, 1).hist(bimodHist, 0, 255, "Input Histogram");
wnd(1, 1).hist(bimodHist, 0, 255, "Input Histogram");
wnd(2, 1).hist(smoothHist, 0, 255, "Smoothed Input Histogram");
wnd(0, 2).image(bt, "Simple Binary threshold");
wnd(1, 2).image(ot, "Otsu's Threshold");
wnd(2, 2).image(otsu(smooth), "Otsu's Threshold on Smoothed Image");
wnd.show();
}
fprintf(stderr,
"%s\n", e.
what());
throw;
}
return 0;
}
Window object to render af::arrays.
A multi dimensional data container.
T scalar() const
Get the first element of the array as a scalar.
dim4 dims() const
Get dimensions of the array.
T * host() const
Copy array data to host and return host pointer.
dim_t elements() const
Get the total number of elements across all 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.
seq is used to create sequences for indexing af::array
AFAPI void setDevice(const int device)
Sets the current device.
AFAPI array gaussianKernel(const int rows, const int cols, const double sig_r=0, const double sig_c=0)
C++ Interface for generating gausian kernels.
AFAPI array histogram(const array &in, const unsigned nbins, const double minval, const double maxval)
C++ Interface for histogram.
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 resize(const array &in, const dim_t odim0, const dim_t odim1, const interpType method=AF_INTERP_NEAREST)
C++ Interface for resizing an image to specified dimensions.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4