Skip to content

Commit

Permalink
- Changed threshold up from >= to >
Browse files Browse the repository at this point in the history
  • Loading branch information
lessthanoptimal committed Jun 27, 2015
1 parent 1f90aa9 commit 07917c8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 54 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2013, Peter Abeles. All Rights Reserved.
* Copyright (c) 2011-2015, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -81,13 +81,13 @@ public void printAdaptive( AutoTypeImage imageIn ) {
public void printThreshold( AutoTypeImage imageIn ) {
out.print("\t/**\n" +
"\t * Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=\n" +
"\t * to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=\n" +
"\t * to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >\n" +
"\t * to 'threshold' are set to 1 and the others set to 0.\n" +
"\t *\n" +
"\t * @param input Input image. Not modified.\n" +
"\t * @param output (Optional) Binary output image. If null a new image will be declared. Modified.\n" +
"\t * @param threshold threshold value.\n" +
"\t * @param down If true then the inequality <= is used, otherwise if false then >= is used.\n" +
"\t * @param down If true then the inequality <= is used, otherwise if false then > is used.\n" +
"\t * @return Output image.\n" +
"\t */\n" +
"\tpublic static ImageUInt8 threshold( "+imageIn.getSingleBandName()+" input , ImageUInt8 output ,\n" +
Expand Down Expand Up @@ -117,7 +117,7 @@ public void printThreshold( AutoTypeImage imageIn ) {
"\t\t\t\tint end = indexIn + input.width;\n" +
"\n" +
"\t\t\t\tfor( ; indexIn < end; indexIn++ , indexOut++ ) {\n" +
"\t\t\t\t\tif( (input.data[indexIn]"+imageIn.getBitWise()+") >= threshold )\n" +
"\t\t\t\t\tif( (input.data[indexIn]"+imageIn.getBitWise()+") > threshold )\n" +
"\t\t\t\t\t\toutput.data[indexOut] = 1;\n" +
"\t\t\t\t\telse\n" +
"\t\t\t\t\t\toutput.data[indexOut] = 0;\n" +
Expand All @@ -139,7 +139,7 @@ public void printAdaptiveSquare( AutoTypeImage imageIn ) {
"\t * Thresholds the image using an adaptive threshold that is computed using a local square region centered\n" +
"\t * on each pixel. The threshold is equal to the average value of the surrounding pixels plus the bias.\n" +
"\t * If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise\n" +
"\t * b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1\n" +
"\t * b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1\n" +
"\t *\n" +
"\t * @param input Input image.\n" +
"\t * @param output (optional) Output binary image. If null it will be declared internally.\n" +
Expand Down Expand Up @@ -190,7 +190,7 @@ public void printAdaptiveSquare( AutoTypeImage imageIn ) {
"\t\t\t\tfor( ; indexIn < end; indexIn++ , indexOut++, indexMean++ ) {\n" +
"\t\t\t\t\t"+sumType+" threshold = (mean.data[indexMean]"+bitwise+") + bias;\n" +
"\n" +
"\t\t\t\t\tif( (input.data[indexIn]"+bitwise+") >= threshold )\n" +
"\t\t\t\t\tif( (input.data[indexIn]"+bitwise+") > threshold )\n" +
"\t\t\t\t\t\toutput.data[indexOut] = 1;\n" +
"\t\t\t\t\telse\n" +
"\t\t\t\t\t\toutput.data[indexOut] = 0;\n" +
Expand All @@ -212,7 +212,7 @@ public void printAdaptiveGaussian( AutoTypeImage imageIn ) {
"\t * Thresholds the image using an adaptive threshold that is computed using a local square region centered\n" +
"\t * on each pixel. The threshold is equal to the gaussian weighted sum of the surrounding pixels plus the bias.\n" +
"\t * If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise\n" +
"\t * b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1\n" +
"\t * b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1\n" +
"\t *\n" +
"\t * @param input Input image.\n" +
"\t * @param output (optional) Output binary image. If null it will be declared internally.\n" +
Expand Down Expand Up @@ -263,7 +263,7 @@ public void printAdaptiveGaussian( AutoTypeImage imageIn ) {
"\t\t\t\tfor( ; indexIn < end; indexIn++ , indexOut++, indexMean++ ) {\n" +
"\t\t\t\t\t"+sumType+" threshold = (blur.data[indexMean]"+bitwise+") + bias;\n" +
"\n" +
"\t\t\t\t\tif( (input.data[indexIn]"+bitwise+") >= threshold )\n" +
"\t\t\t\t\tif( (input.data[indexIn]"+bitwise+") > threshold )\n" +
"\t\t\t\t\t\toutput.data[indexOut] = 1;\n" +
"\t\t\t\t\telse\n" +
"\t\t\t\t\t\toutput.data[indexOut] = 0;\n" +
Expand Down
17 changes: 10 additions & 7 deletions main/ip/src/boofcv/alg/filter/binary/GThresholdImageOps.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2014, Peter Abeles. All Rights Reserved.
* Copyright (c) 2011-2015, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -66,11 +66,14 @@ public static int computeOtsu( ImageSingleBand input , int minValue , int maxVal
*/
// original code from http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html
// Dr. Andrew Greensted
// modifications to reduce overflow
public static int computeOtsu( int histogram[] , int length , int totalPixels ) {

double dlength = length;
double sum = 0;
for (int i=0 ; i< length ; i++)
sum += i*histogram[i];
sum += (i/dlength)*histogram[i];


double sumB = 0;
int wB = 0;
Expand All @@ -85,7 +88,7 @@ public static int computeOtsu( int histogram[] , int length , int totalPixels )
int wF = totalPixels - wB; // Weight Foreground
if (wF == 0) break;

sumB += i*histogram[i];
sumB += (i/dlength)*histogram[i];

double mB = sumB / wB; // Mean Background
double mF = (sum - sumB) / wF; // Mean Foreground
Expand Down Expand Up @@ -197,13 +200,13 @@ public static int computeEntropy( int histogram[] , int length , int totalPixels

/**
* Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >
* to 'threshold' are set to 1 and the others set to 0.
*
* @param input Input image. Not modified.
* @param output (Optional) Binary output image. If null a new image will be declared. Modified.
* @param threshold threshold value.
* @param down If true then the inequality <= is used, otherwise if false then >= is used.
* @param down If true then the inequality <= is used, otherwise if false then > is used.
* @return binary image.
*/
public static <T extends ImageSingleBand>
Expand Down Expand Up @@ -232,7 +235,7 @@ ImageUInt8 threshold( T input , ImageUInt8 output ,
* Thresholds the image using an adaptive threshold that is computed using a local square region centered
* on each pixel. The threshold is equal to the average value of the surrounding pixels plus the bias.
* If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise
* b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1
* b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1
* </p>
*
* <p>
Expand Down Expand Up @@ -269,7 +272,7 @@ ImageUInt8 adaptiveSquare( T input , ImageUInt8 output ,
* Thresholds the image using an adaptive threshold that is computed using a local square region centered
* on each pixel. The threshold is equal to the gaussian weighted sum of the surrounding pixels plus the bias.
* If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise
* b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1
* b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1
* </p>
*
* <p>
Expand Down
67 changes: 33 additions & 34 deletions main/ip/src/boofcv/alg/filter/binary/ThresholdImageOps.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2014, Peter Abeles. All Rights Reserved.
* Copyright (c) 2011-2015, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand All @@ -15,7 +15,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package boofcv.alg.filter.binary;

import boofcv.alg.InputSanityCheck;
Expand All @@ -37,13 +36,13 @@ public class ThresholdImageOps {

/**
* Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >
* to 'threshold' are set to 1 and the others set to 0.
*
* @param input Input image. Not modified.
* @param output Binary output image. If null a new image will be declared. Modified.
* @param output (Optional) Binary output image. If null a new image will be declared. Modified.
* @param threshold threshold value.
* @param down If true then the inequality <= is used, otherwise if false then >= is used.
* @param down If true then the inequality <= is used, otherwise if false then > is used.
* @return Output image.
*/
public static ImageUInt8 threshold( ImageFloat32 input , ImageUInt8 output ,
Expand Down Expand Up @@ -73,7 +72,7 @@ public static ImageUInt8 threshold( ImageFloat32 input , ImageUInt8 output ,
int end = indexIn + input.width;

for( ; indexIn < end; indexIn++ , indexOut++ ) {
if( (input.data[indexIn]) >= threshold )
if( (input.data[indexIn]) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -86,13 +85,13 @@ public static ImageUInt8 threshold( ImageFloat32 input , ImageUInt8 output ,

/**
* Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >
* to 'threshold' are set to 1 and the others set to 0.
*
* @param input Input image. Not modified.
* @param output Binary output image. If null a new image will be declared. Modified.
* @param output (Optional) Binary output image. If null a new image will be declared. Modified.
* @param threshold threshold value.
* @param down If true then the inequality <= is used, otherwise if false then >= is used.
* @param down If true then the inequality <= is used, otherwise if false then > is used.
* @return Output image.
*/
public static ImageUInt8 threshold( ImageFloat64 input , ImageUInt8 output ,
Expand Down Expand Up @@ -122,7 +121,7 @@ public static ImageUInt8 threshold( ImageFloat64 input , ImageUInt8 output ,
int end = indexIn + input.width;

for( ; indexIn < end; indexIn++ , indexOut++ ) {
if( (input.data[indexIn]) >= threshold )
if( (input.data[indexIn]) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -135,13 +134,13 @@ public static ImageUInt8 threshold( ImageFloat64 input , ImageUInt8 output ,

/**
* Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >
* to 'threshold' are set to 1 and the others set to 0.
*
* @param input Input image. Not modified.
* @param output Binary output image. If null a new image will be declared. Modified.
* @param output (Optional) Binary output image. If null a new image will be declared. Modified.
* @param threshold threshold value.
* @param down If true then the inequality <= is used, otherwise if false then >= is used.
* @param down If true then the inequality <= is used, otherwise if false then > is used.
* @return Output image.
*/
public static ImageUInt8 threshold( ImageUInt8 input , ImageUInt8 output ,
Expand Down Expand Up @@ -171,7 +170,7 @@ public static ImageUInt8 threshold( ImageUInt8 input , ImageUInt8 output ,
int end = indexIn + input.width;

for( ; indexIn < end; indexIn++ , indexOut++ ) {
if( (input.data[indexIn]& 0xFF) >= threshold )
if( (input.data[indexIn]& 0xFF) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -184,13 +183,13 @@ public static ImageUInt8 threshold( ImageUInt8 input , ImageUInt8 output ,

/**
* Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >
* to 'threshold' are set to 1 and the others set to 0.
*
* @param input Input image. Not modified.
* @param output Binary output image. If null a new image will be declared. Modified.
* @param output (Optional) Binary output image. If null a new image will be declared. Modified.
* @param threshold threshold value.
* @param down If true then the inequality <= is used, otherwise if false then >= is used.
* @param down If true then the inequality <= is used, otherwise if false then > is used.
* @return Output image.
*/
public static ImageUInt8 threshold( ImageSInt16 input , ImageUInt8 output ,
Expand Down Expand Up @@ -220,7 +219,7 @@ public static ImageUInt8 threshold( ImageSInt16 input , ImageUInt8 output ,
int end = indexIn + input.width;

for( ; indexIn < end; indexIn++ , indexOut++ ) {
if( (input.data[indexIn]) >= threshold )
if( (input.data[indexIn]) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -233,13 +232,13 @@ public static ImageUInt8 threshold( ImageSInt16 input , ImageUInt8 output ,

/**
* Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >
* to 'threshold' are set to 1 and the others set to 0.
*
* @param input Input image. Not modified.
* @param output Binary output image. If null a new image will be declared. Modified.
* @param output (Optional) Binary output image. If null a new image will be declared. Modified.
* @param threshold threshold value.
* @param down If true then the inequality <= is used, otherwise if false then >= is used.
* @param down If true then the inequality <= is used, otherwise if false then > is used.
* @return Output image.
*/
public static ImageUInt8 threshold( ImageUInt16 input , ImageUInt8 output ,
Expand Down Expand Up @@ -269,7 +268,7 @@ public static ImageUInt8 threshold( ImageUInt16 input , ImageUInt8 output ,
int end = indexIn + input.width;

for( ; indexIn < end; indexIn++ , indexOut++ ) {
if( (input.data[indexIn]& 0xFFFF) >= threshold )
if( (input.data[indexIn]& 0xFFFF) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -282,13 +281,13 @@ public static ImageUInt8 threshold( ImageUInt16 input , ImageUInt8 output ,

/**
* Applies a global threshold across the whole image. If 'down' is true, then pixels with values <=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >=
* to 'threshold' are set to 1 and the others set to 0. If 'down' is false, then pixels with values >
* to 'threshold' are set to 1 and the others set to 0.
*
* @param input Input image. Not modified.
* @param output Binary output image. If null a new image will be declared. Modified.
* @param output (Optional) Binary output image. If null a new image will be declared. Modified.
* @param threshold threshold value.
* @param down If true then the inequality <= is used, otherwise if false then >= is used.
* @param down If true then the inequality <= is used, otherwise if false then > is used.
* @return Output image.
*/
public static ImageUInt8 threshold( ImageSInt32 input , ImageUInt8 output ,
Expand Down Expand Up @@ -318,7 +317,7 @@ public static ImageUInt8 threshold( ImageSInt32 input , ImageUInt8 output ,
int end = indexIn + input.width;

for( ; indexIn < end; indexIn++ , indexOut++ ) {
if( (input.data[indexIn]) >= threshold )
if( (input.data[indexIn]) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -333,7 +332,7 @@ public static ImageUInt8 threshold( ImageSInt32 input , ImageUInt8 output ,
* Thresholds the image using an adaptive threshold that is computed using a local square region centered
* on each pixel. The threshold is equal to the average value of the surrounding pixels plus the bias.
* If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise
* b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1
* b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1
*
* @param input Input image.
* @param output (optional) Output binary image. If null it will be declared internally.
Expand Down Expand Up @@ -384,7 +383,7 @@ public static ImageUInt8 adaptiveSquare( ImageUInt8 input , ImageUInt8 output ,
for( ; indexIn < end; indexIn++ , indexOut++, indexMean++ ) {
int threshold = (mean.data[indexMean]& 0xFF) + bias;

if( (input.data[indexIn]& 0xFF) >= threshold )
if( (input.data[indexIn]& 0xFF) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -399,7 +398,7 @@ public static ImageUInt8 adaptiveSquare( ImageUInt8 input , ImageUInt8 output ,
* Thresholds the image using an adaptive threshold that is computed using a local square region centered
* on each pixel. The threshold is equal to the gaussian weighted sum of the surrounding pixels plus the bias.
* If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise
* b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1
* b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1
*
* @param input Input image.
* @param output (optional) Output binary image. If null it will be declared internally.
Expand Down Expand Up @@ -450,7 +449,7 @@ public static ImageUInt8 adaptiveGaussian( ImageUInt8 input , ImageUInt8 output
for( ; indexIn < end; indexIn++ , indexOut++, indexMean++ ) {
int threshold = (blur.data[indexMean]& 0xFF) + bias;

if( (input.data[indexIn]& 0xFF) >= threshold )
if( (input.data[indexIn]& 0xFF) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -465,7 +464,7 @@ public static ImageUInt8 adaptiveGaussian( ImageUInt8 input , ImageUInt8 output
* Thresholds the image using an adaptive threshold that is computed using a local square region centered
* on each pixel. The threshold is equal to the average value of the surrounding pixels plus the bias.
* If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise
* b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1
* b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1
*
* @param input Input image.
* @param output (optional) Output binary image. If null it will be declared internally.
Expand Down Expand Up @@ -516,7 +515,7 @@ public static ImageUInt8 adaptiveSquare( ImageFloat32 input , ImageUInt8 output
for( ; indexIn < end; indexIn++ , indexOut++, indexMean++ ) {
float threshold = (mean.data[indexMean]) + bias;

if( (input.data[indexIn]) >= threshold )
if( (input.data[indexIn]) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand All @@ -531,7 +530,7 @@ public static ImageUInt8 adaptiveSquare( ImageFloat32 input , ImageUInt8 output
* Thresholds the image using an adaptive threshold that is computed using a local square region centered
* on each pixel. The threshold is equal to the gaussian weighted sum of the surrounding pixels plus the bias.
* If down is true then b(x,y) = I(x,y) <= T(x,y) + bias ? 1 : 0. Otherwise
* b(x,y) = I(x,y) >= T(x,y) + bias ? 0 : 1
* b(x,y) = I(x,y) > T(x,y) + bias ? 0 : 1
*
* @param input Input image.
* @param output (optional) Output binary image. If null it will be declared internally.
Expand Down Expand Up @@ -582,7 +581,7 @@ public static ImageUInt8 adaptiveGaussian( ImageFloat32 input , ImageUInt8 outpu
for( ; indexIn < end; indexIn++ , indexOut++, indexMean++ ) {
float threshold = (blur.data[indexMean]) + bias;

if( (input.data[indexIn]) >= threshold )
if( (input.data[indexIn]) > threshold )
output.data[indexOut] = 1;
else
output.data[indexOut] = 0;
Expand Down

0 comments on commit 07917c8

Please sign in to comment.