Skip to content
Snippets Groups Projects
Commit e9f1c9a2 authored by jakubs's avatar jakubs
Browse files

SOB-117 BIS-395 SP-603 fix calculation of intensity rescaling levels

SVN: 28794
parent a6905304
No related branches found
No related tags found
No related merge requests found
......@@ -370,25 +370,38 @@ public class IntensityRescaling
public static Levels computeLevels(PixelHistogram histogram, float threshold)
{
final int intThreshold = Math.round(threshold * histogram.pixelCount);
int[] histogramArray = histogram.histogram;
return computeLevels(intThreshold, histogramArray);
}
public static Levels computeLevels(final int intThreshold, int[] histogramArray)
{
int min = -1;
int sum = 0;
while (sum <= intThreshold)
{
sum += histogram.histogram[++min];
}
if (min < 0)
{
min = 0;
}
int max = MAX_USHORT + 1;
sum = 0;
while (sum <= intThreshold)
int max = histogramArray.length;
int minSum = 0;
int maxSum = 0;
while (minSum <= intThreshold || maxSum <= intThreshold)
{
sum += histogram.histogram[--max];
if (minSum <= intThreshold)
{
minSum += histogramArray[++min];
}
if (min >= max - 1)
{
break;
}
if (maxSum <= intThreshold)
{
maxSum += histogramArray[--max];
}
}
if (max > MAX_USHORT)
if (max >= histogramArray.length)
{
max = MAX_USHORT;
max = histogramArray.length - 1;
}
return new Levels(min, max);
}
......@@ -452,8 +465,7 @@ public class IntensityRescaling
// normalize to [0, 1] and rescale to 8 bits
intensity = (int) (0.5 + (intensity * dynamicRange));
rescaledRaster
.setSample(x, y, channel.getBand(), intensity);
rescaledRaster.setSample(x, y, channel.getBand(), intensity);
}
}
return rescaledImage;
......
/*
* Copyright 2013 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.image;
import static org.testng.AssertJUnit.assertEquals;
import org.testng.annotations.Test;
import ch.systemsx.cisd.common.image.IntensityRescaling.Levels;
/**
* @author Jakub Straszewski
*/
@Test
public class IntensityRescalingTest
{
@Test
public void testBorderCaseOfIntensityRescaling()
{
Levels result = IntensityRescaling.computeLevels(14, new int[]
{ 1098, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 });
assertEquals(0, result.minLevel);
assertEquals(1, result.maxLevel);
}
@Test
public void testBorderCaseOfIntensityRescalingOtherWay()
{
Levels result = IntensityRescaling.computeLevels(14, new int[]
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19876 });
assertEquals(11, result.minLevel);
assertEquals(12, result.maxLevel);
}
}
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