Skip to content
Snippets Groups Projects
Commit 6d8d0318 authored by Eric Wait's avatar Eric Wait
Browse files

moved otsu helper

parent b798d9b0
No related branches found
No related tags found
No related merge requests found
......@@ -2,9 +2,9 @@
double* createCircleKernel(int rad, Vec<unsigned int>& kernelDims)
{
kernelDims.x = rad%2==0 ? rad+1 : rad;
kernelDims.y = rad%2==0 ? rad+1 : rad;
kernelDims.z = rad%2==0 ? rad+1 : rad;
kernelDims.x = rad*2+1;
kernelDims.y = rad*2+1;
kernelDims.z = rad*2+1;
double* kernel = new double[kernelDims.product()];
memset(kernel,0,sizeof(double)*kernelDims.product());
......@@ -15,46 +15,21 @@ double* createCircleKernel(int rad, Vec<unsigned int>& kernelDims)
mid.z = kernelDims.z/2+1;
Vec<unsigned int> cur(0,0,0);
for (cur.x=0; cur.x < kernelDims.x ; ++cur.x)
for (cur.z=0; cur.z<kernelDims.z ; ++cur.z)
{
for (cur.y=0; cur.y < kernelDims.y ; ++cur.y)
for (cur.y=0; cur.y<kernelDims.y ; ++cur.y)
{
for (cur.z=0; cur.z < kernelDims.z ; ++cur.z)
for (cur.x=0; cur.x<kernelDims.x ; ++cur.x)
{
if (cur.EuclideanDistanceTo(mid)<=rad)
kernel[kernelDims.linearAddressAt(cur)] = 1;
double dist = cur.EuclideanDistanceTo(mid);
double dist2 = sqrt((double)((cur.x-mid.x)*(cur.x-mid.x)+(cur.y-mid.y)*(cur.y-mid.y)+(cur.z-mid.z)*(cur.z-mid.z)));
if (dist!=dist2)
kernel[0]=1e6;
if (dist<=rad)
kernel[kernelDims.linearAddressAt(cur)] = 1.0f;
}
}
}
return kernel;
}
int calcOtsuThreshold(const double* normHistogram, int numBins)
{
//code modified from http://www.dandiggins.co.uk/arlib-9.html
double totalMean = 0.0f;
for (int i=0; i<numBins; ++i)
totalMean += i*normHistogram[i];
double class1Prob=0, class1Mean=0, temp1, curThresh;
double bestThresh = 0.0;
int bestIndex = 0;
for (int i=0; i<numBins; ++i)
{
class1Prob += normHistogram[i];
class1Mean += i * normHistogram[i];
temp1 = totalMean * class1Prob - class1Mean;
curThresh = (temp1*temp1) / (class1Prob*(1.0f-class1Prob));
if(curThresh>bestThresh)
{
bestThresh = curThresh;
bestIndex = i;
}
}
return bestIndex;
}
\ No newline at end of file
......@@ -4,6 +4,4 @@
#include <memory.h>
#include <complex>
double* createCircleKernel(int rad, Vec<unsigned int>& kernelDims);
int calcOtsuThreshold(const double* normHistogram, int numBins);
\ No newline at end of file
double* createCircleKernel(int rad, Vec<unsigned int>& kernelDims);
\ No newline at end of file
......@@ -187,4 +187,32 @@ Vec<unsigned int> createGaussianKernel(Vec<float> sigma, float* kernel, Vec<int>
return kernelDims;
}
int calcOtsuThreshold(const double* normHistogram, int numBins)
{
//code modified from http://www.dandiggins.co.uk/arlib-9.html
double totalMean = 0.0f;
for (int i=0; i<numBins; ++i)
totalMean += i*normHistogram[i];
double class1Prob=0, class1Mean=0, temp1, curThresh;
double bestThresh = 0.0;
int bestIndex = 0;
for (int i=0; i<numBins; ++i)
{
class1Prob += normHistogram[i];
class1Mean += i * normHistogram[i];
temp1 = totalMean * class1Prob - class1Mean;
curThresh = (temp1*temp1) / (class1Prob*(1.0f-class1Prob));
if(curThresh>bestThresh)
{
bestThresh = curThresh;
bestIndex = i;
}
}
return bestIndex;
}
\ No newline at end of file
......@@ -107,4 +107,6 @@ struct Lock
Vec<unsigned int> createGaussianKernel(Vec<float> sigma, float* kernel, int& iterations);
Vec<unsigned int> createGaussianKernel(Vec<float> sigma, float* kernel, Vec<int>& iterations);
\ No newline at end of file
Vec<unsigned int> createGaussianKernel(Vec<float> sigma, float* kernel, Vec<int>& iterations);
int calcOtsuThreshold(const double* normHistogram, int numBins);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment