root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / enhancement / EcualizationFilter.java @ 19491
History | View | Annotate | Download (8.87 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.grid.filter.enhancement; |
20 |
|
21 |
import org.gvsig.raster.buffer.RasterBuffer; |
22 |
import org.gvsig.raster.dataset.FileNotOpenException; |
23 |
import org.gvsig.raster.dataset.IBuffer; |
24 |
import org.gvsig.raster.dataset.Params; |
25 |
import org.gvsig.raster.dataset.io.RasterDriverException; |
26 |
import org.gvsig.raster.dataset.properties.DatasetListStatistics; |
27 |
import org.gvsig.raster.datastruct.Histogram; |
28 |
import org.gvsig.raster.datastruct.HistogramException; |
29 |
import org.gvsig.raster.grid.filter.RasterFilter; |
30 |
/**
|
31 |
* Clase base para los filtros de ecualizaci?n de histograma.
|
32 |
*
|
33 |
* @version 31/05/2007
|
34 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
35 |
*/
|
36 |
public class EcualizationFilter extends RasterFilter { |
37 |
protected DatasetListStatistics stats = null; |
38 |
protected double[] minBandValue = null; |
39 |
protected double[] maxBandValue = null; |
40 |
protected int nbands = 3; |
41 |
protected int[] renderBands = null; |
42 |
protected int[] k = null; |
43 |
public static String[] names = new String[] {"ecualization"}; |
44 |
protected Histogram histogram = null; |
45 |
protected long nPixels = 0; |
46 |
protected int[][] resValues = null; |
47 |
protected long[][] lahe = null; |
48 |
protected long[][] laheNegative = null; |
49 |
protected int nElements = 0; |
50 |
protected int[] ecualizedBands = null; |
51 |
|
52 |
/**
|
53 |
* Construye un LinearEnhancementFilter
|
54 |
*/
|
55 |
public EcualizationFilter() {
|
56 |
setName(names[0]);
|
57 |
} |
58 |
|
59 |
/*
|
60 |
* (non-Javadoc)
|
61 |
* @see org.gvsig.raster.grid.filter.RasterFilter#pre()
|
62 |
*/
|
63 |
public void pre() { |
64 |
raster = (IBuffer) params.get("raster");
|
65 |
stats = (DatasetListStatistics) params.get("stats");
|
66 |
histogram = (Histogram) params.get("histogram");
|
67 |
|
68 |
if(histogram == null) |
69 |
return;
|
70 |
|
71 |
renderBands = (int[]) params.get("renderBands"); |
72 |
if(renderBands != null && renderBands.length < raster.getBandCount()) { |
73 |
int[] newRenderBands = new int[raster.getBandCount()]; |
74 |
for (int i = 0; i < renderBands.length; i++) |
75 |
newRenderBands[i] = renderBands[i]; |
76 |
for (int i = renderBands.length; i < newRenderBands.length; i++) |
77 |
newRenderBands[i] = i; |
78 |
renderBands = newRenderBands; |
79 |
} |
80 |
if(renderBands == null) |
81 |
renderBands = new int[]{0, 1, 2}; |
82 |
|
83 |
ecualizedBands = (int[]) params.get("ecualizedBands"); |
84 |
if(ecualizedBands == null) |
85 |
ecualizedBands = renderBands; |
86 |
|
87 |
height = raster.getHeight(); |
88 |
width = raster.getWidth(); |
89 |
nPixels = height * width; |
90 |
|
91 |
try {
|
92 |
stats.calcFullStatistics(); |
93 |
} catch (FileNotOpenException e) {
|
94 |
exec = false;
|
95 |
} catch (RasterDriverException e) {
|
96 |
exec = false;
|
97 |
} catch (InterruptedException e) { |
98 |
exec = false;
|
99 |
} |
100 |
|
101 |
if(raster.getDataType() == IBuffer.TYPE_BYTE) {
|
102 |
minBandValue = stats.getMinRGB(); |
103 |
maxBandValue = stats.getMaxRGB(); |
104 |
} |
105 |
if(minBandValue[0] == 0 && maxBandValue[0] == 0) { |
106 |
minBandValue = new double[raster.getBandCount()]; |
107 |
maxBandValue = new double[raster.getBandCount()]; |
108 |
for (int i = 0; i < minBandValue.length; i++) { |
109 |
minBandValue[i] = 0;
|
110 |
maxBandValue[i] = 255;
|
111 |
} |
112 |
//minBandValue = stats.getMin();
|
113 |
//maxBandValue = stats.getMax();
|
114 |
} |
115 |
|
116 |
//Histogram rgbHistogram = Histogram.convertHistogramToRGB(histogram);
|
117 |
Histogram rgbHistogram; |
118 |
try {
|
119 |
rgbHistogram = Histogram.convertHistogramToRGB(raster.getHistogram()); |
120 |
} catch (HistogramException e) {
|
121 |
return;
|
122 |
} catch (InterruptedException e) { |
123 |
return;
|
124 |
} |
125 |
double[][] accumNormalize = Histogram.convertTableToNormalizeAccumulate(rgbHistogram.getTable()); |
126 |
double[][] accumNormalizeNeg = Histogram.convertTableToNormalizeAccumulate(rgbHistogram.getNegativeTable()); |
127 |
|
128 |
lahe = lahe(accumNormalize, 255);
|
129 |
laheNegative = lahe(accumNormalizeNeg, 255);
|
130 |
nElements = (laheNegative[0].length - 1); |
131 |
|
132 |
nbands = stats.getBandCount(); |
133 |
rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, raster.getWidth(), raster.getHeight(), raster.getBandCount(), true);
|
134 |
} |
135 |
|
136 |
/**
|
137 |
* M?todo lahe para la ecualizaci?n. Cada posici?n del array resultante tendr? el valor de salida para
|
138 |
* un valor de entrada dado.
|
139 |
* @param accumNorm Histograma acumulado
|
140 |
* @param value Valor m?ximo
|
141 |
* @return
|
142 |
*/
|
143 |
private long[][] lahe(double[][] accumNorm, int value) { |
144 |
long[][] res = new long[accumNorm.length][accumNorm[0].length]; |
145 |
for (int i = 0; i < res.length; i++) |
146 |
for (int j = 0; j < res[i].length; j++) |
147 |
res[i][j] = Math.round(accumNorm[i][j] * value);
|
148 |
return res;
|
149 |
} |
150 |
|
151 |
/**
|
152 |
* Consulta si la ecualizaci?n est? activa para una banda o no
|
153 |
* @param band N?mero de banda a consultar si se ha activado la ecualizaci?n
|
154 |
* @return true si est? activa para esa banda y false si no lo est?.
|
155 |
*/
|
156 |
protected boolean ecualizationActive(int band) { |
157 |
for (int i = 0; i < ecualizedBands.length; i++) { |
158 |
if(band == ecualizedBands[i])
|
159 |
return true; |
160 |
} |
161 |
return false; |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Obtiene una distribuci?n de probabilidad de niveles de gris.
|
166 |
* @param bands N?mero de bandas
|
167 |
* @param nValues Valores a distribuir. Tipicamente 255
|
168 |
* @return
|
169 |
*/
|
170 |
/*private double[][] getProbabilityDistribution(int bands, int nValues) {
|
171 |
double[][] res = new double[bands][nValues];
|
172 |
for (int i = 0; i < res.length; i++)
|
173 |
for (int j = 0; j < res[i].length; j++)
|
174 |
res[i][j] += (double)((double)j / 255D);
|
175 |
return res;
|
176 |
}*/
|
177 |
|
178 |
/**
|
179 |
* Calcula un histograma aproximado ecualizado.
|
180 |
* @param accumNorm
|
181 |
* @param distrib
|
182 |
* @return
|
183 |
*/
|
184 |
/*private double[][] calcApproximation(double[][] accumNorm, double[][] distrib) {
|
185 |
double[][] res = new double[accumNorm.length][accumNorm[0].length];
|
186 |
resValues = new int[accumNorm.length][accumNorm[0].length];
|
187 |
for (int i = 0; i < accumNorm.length; i++) {
|
188 |
for (int j = 0; j < accumNorm[i].length; j++) {
|
189 |
double[] v = searchProbability(distrib[i], accumNorm[i][j]);
|
190 |
if(v != null) {
|
191 |
res[i][j] = v[0];
|
192 |
resValues[i][j] = (int)v[1];
|
193 |
}
|
194 |
}
|
195 |
}
|
196 |
return res;
|
197 |
}*/
|
198 |
|
199 |
/**
|
200 |
* En un array de distribuci?n de probabilidades busca la m?s cercana
|
201 |
* al valor pasado
|
202 |
* @param distrib Array de probabilidades
|
203 |
* @param value Valor a buscar en el array
|
204 |
* @return valor m?s cercano en el array
|
205 |
*/
|
206 |
/*private double[] searchProbability(double[] distrib, double value) {
|
207 |
double dif = Math.abs(distrib[0] - value);
|
208 |
for (int i = 1; i < distrib.length; i++) {
|
209 |
double newDif = Math.abs(distrib[i] - value);
|
210 |
if(newDif < dif)
|
211 |
dif = newDif;
|
212 |
else
|
213 |
return new double[]{dif, i - 1};
|
214 |
}
|
215 |
return null;
|
216 |
}*/
|
217 |
|
218 |
/*
|
219 |
* (non-Javadoc)
|
220 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
|
221 |
*/
|
222 |
public int getOutRasterDataType() { |
223 |
return IBuffer.TYPE_BYTE;
|
224 |
} |
225 |
|
226 |
/*
|
227 |
* (non-Javadoc)
|
228 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String)
|
229 |
*/
|
230 |
public Object getResult(String name) { |
231 |
if (name.equals("raster")) |
232 |
if(!exec)
|
233 |
return (Object) this.raster; |
234 |
else
|
235 |
return (Object) this.rasterResult; |
236 |
return null; |
237 |
} |
238 |
|
239 |
/*
|
240 |
* (non-Javadoc)
|
241 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getGroup()
|
242 |
*/
|
243 |
public String getGroup() { |
244 |
return "radiometricos"; |
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getUIParams()
|
250 |
*/
|
251 |
public Params getUIParams(String nameFilter) { |
252 |
Params params = new Params();
|
253 |
return params;
|
254 |
} |
255 |
|
256 |
/*
|
257 |
* (non-Javadoc)
|
258 |
* @see org.gvsig.raster.grid.filter.RasterFilter#post()
|
259 |
*/
|
260 |
public void post() { |
261 |
// En caso de que nadie apunte a raster, se liberar? su memoria.
|
262 |
raster = null;
|
263 |
} |
264 |
|
265 |
/*
|
266 |
* (non-Javadoc)
|
267 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
|
268 |
*/
|
269 |
public int getInRasterDataType() { |
270 |
return 0; |
271 |
} |
272 |
|
273 |
/*
|
274 |
* (non-Javadoc)
|
275 |
* @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int)
|
276 |
*/
|
277 |
public void process(int x, int y) { |
278 |
} |
279 |
|
280 |
/*
|
281 |
* (non-Javadoc)
|
282 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getNames()
|
283 |
*/
|
284 |
public String[] getNames() { |
285 |
return names;
|
286 |
} |
287 |
|
288 |
/*
|
289 |
* (non-Javadoc)
|
290 |
* @see org.gvsig.raster.grid.filter.RasterFilter#isVisible()
|
291 |
*/
|
292 |
public boolean isVisible() { |
293 |
return false; |
294 |
} |
295 |
} |