Skip to content

Commit 94b4f36

Browse files
blowekamphjmjohnson
authored andcommitted
BUG: Fix Li threshold calculator for negative image values
The histogram values are shifted by the minimum bin value to prevent taking a log of a negative number. Change-Id: I779df41f4d65e433a4c5e1613e3b9b2f40b6c95f
1 parent 9cfdb90 commit 94b4f36

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Modules/Filtering/Thresholding/include/itkLiThresholdCalculator.hxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ LiThresholdCalculator<THistogram, TOutput>
6060
double tolerance; // threshold tolerance
6161
double temp;
6262

63+
const double bin_min = histogram->GetBinMin(0,0);
64+
6365
tolerance = 0.5;
6466
num_pixels = histogram->GetTotalFrequency();
6567

@@ -82,6 +84,12 @@ LiThresholdCalculator<THistogram, TOutput>
8284
typename HistogramType::IndexType local_index;
8385
histogram->GetIndex(ot, local_index);
8486
histthresh = local_index[0];
87+
88+
if( histogram->IsIndexOutOfBounds(local_index) )
89+
{
90+
itkWarningMacro("Unexpected histogram index out of bounds!");
91+
break;
92+
}
8593
}
8694

8795
// Calculate the means of background and object pixels
@@ -114,6 +122,11 @@ LiThresholdCalculator<THistogram, TOutput>
114122
//
115123
//#define IS_NEG( x ) ( ( x ) < -DBL_EPSILON )
116124
//
125+
126+
// Shift the mean by the minimum to have the range start at zero,
127+
// and void the log of a negative value.
128+
mean_back -= bin_min;
129+
mean_obj -= bin_min;
117130
temp = ( mean_back - mean_obj ) / ( std::log ( mean_back ) - std::log ( mean_obj ) );
118131

119132
double epsilon = itk::NumericTraits<double>::epsilon();
@@ -127,6 +140,11 @@ LiThresholdCalculator<THistogram, TOutput>
127140
}
128141
// Stop the iterations when the difference between the new and old threshold
129142
// values is less than the tolerance
143+
144+
// Shift the result back.
145+
new_thresh += bin_min;
146+
147+
130148
}
131149
while ( std::abs ( new_thresh - old_thresh ) > tolerance );
132150

0 commit comments

Comments
 (0)