root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / statistics / HistogramImageFilter.java @ 8026
History | View | Annotate | Download (3.93 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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 |
|
20 |
package org.cresques.filter.statistics; |
21 |
|
22 |
import java.awt.Image; |
23 |
import java.awt.image.BufferedImage; |
24 |
import java.util.Vector; |
25 |
|
26 |
import org.cresques.io.data.RasterBuf; |
27 |
/**
|
28 |
* Filtro para crear histogramas de brillo para cada una de las bandas de la imagen.
|
29 |
* Toma como entrada la imagen y da como salida un histograma para cada una de las
|
30 |
* bandas representado mediante una matriz.
|
31 |
* @author Miguel ?ngel Querol Carratal? <querol_mig@gva.es>
|
32 |
*
|
33 |
*/
|
34 |
|
35 |
public class HistogramImageFilter extends HistogramFilter { |
36 |
|
37 |
|
38 |
/**
|
39 |
* Matriz donde se van a almacenar los histogramas.
|
40 |
*/
|
41 |
int histoRed[] = new int[256]; |
42 |
int histoGreen[] = new int[256]; |
43 |
int histoBlue[] = new int[256]; |
44 |
|
45 |
|
46 |
/**
|
47 |
* Constructor
|
48 |
*
|
49 |
*/
|
50 |
|
51 |
|
52 |
public HistogramImageFilter(){
|
53 |
super();
|
54 |
} |
55 |
|
56 |
public void pre(){ |
57 |
this.exec=true; |
58 |
this.image = (Image) params.get("raster"); |
59 |
this.width = image.getWidth(null); |
60 |
this.height = image.getHeight(null); |
61 |
|
62 |
for(int i = 0 ; i <= 255 ; i++){ |
63 |
histoRed[i]= 0;
|
64 |
histoGreen[i] = 0;
|
65 |
histoBlue[i] = 0;
|
66 |
} |
67 |
|
68 |
super.pre();
|
69 |
} |
70 |
|
71 |
|
72 |
public void post() { |
73 |
} |
74 |
|
75 |
public void process(int col, int line) { |
76 |
int px = ((BufferedImage) image).getRGB(col, line); |
77 |
int px3[] = {(px & 0xff0000) >> 16, (px & 0xff00) >> 8, (px & 0xff)}; |
78 |
|
79 |
this.histoRed[px3[0]]+=1; //Histograma para la banda R |
80 |
this.histoGreen[px3[1]]+=1; //Histograma para la banda G |
81 |
this.histoBlue[px3[2]]+=1; //Histograma para la banda B |
82 |
pixel++; //Cuenta de los pixels de la imagen
|
83 |
} |
84 |
|
85 |
/* (non-Javadoc)
|
86 |
* @see org.cresques.io.raster.IRasterFilter#processSuperSampling(int, int)
|
87 |
*/
|
88 |
public void processSuperSampling(int col, int line) { |
89 |
process(col, line); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Metodo para obtener el valor medio de brillo para una banda de color.
|
94 |
* @param vector
|
95 |
* @return
|
96 |
*/
|
97 |
public int calculaMedia(int []vector){ |
98 |
int media = 0; |
99 |
int suma = 0; |
100 |
|
101 |
for (int i = 1 ; i <= 255 ; i++){ |
102 |
suma += vector[i]*i; |
103 |
} |
104 |
media = suma/pixel; |
105 |
|
106 |
return media;
|
107 |
} |
108 |
|
109 |
public int getPixel(){ |
110 |
return this.pixel; |
111 |
} |
112 |
|
113 |
public void processLine(int y) { |
114 |
// TODO Auto-generated method stub
|
115 |
|
116 |
} |
117 |
|
118 |
public int getInRasterDataType() { |
119 |
return RasterBuf.TYPE_IMAGE;
|
120 |
} |
121 |
|
122 |
public int getOutRasterDataType() { |
123 |
return RasterBuf.TYPE_IMAGE;
|
124 |
} |
125 |
|
126 |
public Object getResult(String name) { |
127 |
if (name.equals("raster")) { |
128 |
return (Object) this.image; |
129 |
} else {
|
130 |
return null; |
131 |
} |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Metodos para acceder al los valores medios de brillo para cada banda
|
136 |
* @return Media de brillo para la banda de color correspondiente.
|
137 |
*/
|
138 |
|
139 |
public int getMediaRed(){ |
140 |
return calculaMedia(this.histoRed); |
141 |
} |
142 |
|
143 |
public int getMediaGreen(){ |
144 |
return calculaMedia(this.histoGreen); |
145 |
} |
146 |
|
147 |
public int getMediaBlue(){ |
148 |
return calculaMedia(this.histoBlue); |
149 |
} |
150 |
|
151 |
|
152 |
/**
|
153 |
* Metodos para acceder desde fuera a los histogramas calculados por el filtro
|
154 |
* @return Histogramas de cada una de las bandas de color.
|
155 |
*/
|
156 |
public Object getRedHistogram(){ |
157 |
return (Object) this.histoRed; |
158 |
} |
159 |
|
160 |
public Object getGreenHistogram(){ |
161 |
return (Object) this.histoGreen; |
162 |
} |
163 |
|
164 |
public Object getBlueHistogram(){ |
165 |
return (Object) this.histoBlue; |
166 |
} |
167 |
|
168 |
|
169 |
} |