#include <cmath>
#include <cstdio>
#include <cstdlib>
array threshold(
const array &in,
float thresholdValue)
{
int channels = in.
dims(2);
if (channels>1)
ret_val = (ret_val<thresholdValue)*0.0f + 255.0f*(ret_val>thresholdValue);
return ret_val;
}
{
int channels = in.
dims(2);
if (channels>1)
else
gray = in;
array meanB = sumB / wtB;
float lastElemInSumB;
sumB(
seq(255, 255, 1)).
host((
void*)&lastElemInSumB);
array meanF = (lastElemInSumB - sumB) / wtF;
array mDiff = meanB - meanF;
array interClsVar = wtB * wtF * mDiff * mDiff;
float max = af::max<float>(interClsVar);
float threshold2 =
where(interClsVar == max).
scalar<
unsigned>();
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;
array bimodal =
loadImage(ASSETS_DIR
"/examples/images/noisy_square.png",
false);
bimodal =
resize(0.75f, bimodal);
array bt = threshold(bimodal, 180.0f);
array ot = otsu(bimodal);
std::cout << "Press ESC while the window is in focus to proceed to exit" << std::endl;
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");
}
}
fprintf(stderr,
"%s\n", e.
what());
throw;
}
return 0;
}