root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / imageAnalysis / contrastStretching / ContrastStretchingAlgorithm.java @ 59
History | View | Annotate | Download (3.1 KB)
1 |
package es.unex.sextante.imageAnalysis.contrastStretching; |
---|---|
2 |
|
3 |
import java.awt.image.DataBuffer; |
4 |
|
5 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
6 |
import es.unex.sextante.core.Sextante; |
7 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
8 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
9 |
import es.unex.sextante.imageAnalysis.pixelOpsBase.PixelOpsBaseAlgorithm; |
10 |
|
11 |
|
12 |
public class ContrastStretchingAlgorithm |
13 |
extends
|
14 |
PixelOpsBaseAlgorithm { |
15 |
|
16 |
public static final String LOWER = "LOWER"; |
17 |
public static final String UPPER = "UPPER"; |
18 |
|
19 |
private int m_iUpper, m_iLower; |
20 |
private int[] m_Histogram; |
21 |
private int m_iSize; |
22 |
private double m_dUpper, m_dLower; |
23 |
private double m_dRange; |
24 |
|
25 |
|
26 |
@Override
|
27 |
public void defineCharacteristics() { |
28 |
|
29 |
super.defineCharacteristics();
|
30 |
|
31 |
setName(Sextante.getText("Contrast_stretching"));
|
32 |
|
33 |
try {
|
34 |
m_Parameters.addNumericalValue(LOWER, Sextante.getText("Lower_percentile"),
|
35 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 0, 0, 99); |
36 |
m_Parameters.addNumericalValue(UPPER, Sextante.getText("Upper_percentile"),
|
37 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 100, 1, 100); |
38 |
} |
39 |
catch (final RepeatedParameterNameException e) { |
40 |
Sextante.addErrorToLog(e); |
41 |
} |
42 |
|
43 |
} |
44 |
|
45 |
|
46 |
@Override
|
47 |
protected void setValues() throws GeoAlgorithmExecutionException { |
48 |
|
49 |
super.setValues();
|
50 |
|
51 |
m_Histogram = m_Image.getAccumulatedHistogram(); |
52 |
final double dMin = m_dLower = m_Image.getMinValue(); |
53 |
final double dMax = m_dUpper = m_Image.getMaxValue(); |
54 |
final double dRange = dMax - dMin; |
55 |
m_iLower = m_Parameters.getParameterValueAsInt(LOWER); |
56 |
m_iUpper = m_Parameters.getParameterValueAsInt(UPPER); |
57 |
if (m_iLower > m_iUpper) {
|
58 |
final int iSwap = m_iUpper; |
59 |
m_iUpper = m_iLower; |
60 |
m_iLower = iSwap; |
61 |
} |
62 |
m_iSize = (int) (Math.pow(2., DataBuffer.getDataTypeSize(m_Image.getDataType())) / 2.) - 1; |
63 |
m_iLower = (int) (m_Histogram[m_Histogram.length - 1] * (double) m_iLower / 100.); |
64 |
m_iUpper = (int) (m_Histogram[m_Histogram.length - 1] * (double) m_iUpper / 100.); |
65 |
for (int i = 0; i < m_Histogram.length - 1; i++) { |
66 |
if ((m_Histogram[i] < m_iLower) && (m_Histogram[i + 1] > m_iLower)) { |
67 |
m_dLower = dMin + (i + 1) * dRange / m_iSize;
|
68 |
} |
69 |
if ((m_Histogram[i] < m_iUpper) && (m_Histogram[i + 1] > m_iUpper)) { |
70 |
m_dUpper = dMin + (i + 1) * dRange / m_iSize;
|
71 |
} |
72 |
} |
73 |
m_dRange = m_dUpper - m_dLower; |
74 |
|
75 |
} |
76 |
|
77 |
|
78 |
@Override
|
79 |
protected double getValueAt(final int x, |
80 |
final int y) { |
81 |
|
82 |
final double dValue = m_Image.getCellValueAsDouble(x, y); |
83 |
double dRet = (dValue - m_dLower) / m_dRange * m_iSize;
|
84 |
|
85 |
if (dRet > m_iSize) {
|
86 |
dRet = m_iSize; |
87 |
} |
88 |
if (dRet < 0) { |
89 |
dRet = 0;
|
90 |
} |
91 |
|
92 |
return dRet;
|
93 |
|
94 |
} |
95 |
|
96 |
} |