root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / correction / MedianImageFilter.java @ 8026
History | View | Annotate | Download (6.2 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.correction; |
21 |
|
22 |
import java.awt.Image; |
23 |
import java.awt.image.BufferedImage; |
24 |
import java.util.Arrays; |
25 |
|
26 |
import org.cresques.io.data.RasterBuf; |
27 |
|
28 |
/**
|
29 |
* Filtro de Mediana que se aplica en la imagen. Toma como entrada la imagen
|
30 |
* y el lado de la ventana de filtrado.
|
31 |
*
|
32 |
* @author Diego Guerrero Sevilla <diego.guerrero@uclm.es>
|
33 |
*
|
34 |
*/
|
35 |
|
36 |
public class MedianImageFilter extends MedianFilter { |
37 |
|
38 |
public MedianImageFilter(){
|
39 |
super();
|
40 |
} |
41 |
|
42 |
public void pre(){ |
43 |
exec = true;
|
44 |
this.image = (Image) params.get("raster"); |
45 |
height = image.getHeight(null);
|
46 |
width = image.getWidth(null);
|
47 |
this.ladoVentana = ((Integer) params.get("ladoVentana")).intValue(); |
48 |
imageResult = new BufferedImage(image.getWidth(null),image.getHeight(null), BufferedImage.TYPE_INT_ARGB); |
49 |
|
50 |
super.pre();
|
51 |
} |
52 |
|
53 |
public void process(int col, int line) { |
54 |
|
55 |
int px;
|
56 |
ladoVentana = Math.abs(ladoVentana); // El lado de la ventana debe |
57 |
if(ladoVentana % 2 == 0) ladoVentana++; // ser positivo e impar. |
58 |
int tamVentana = ladoVentana*ladoVentana;
|
59 |
int semiLado = (ladoVentana-1)>>1; |
60 |
|
61 |
int [] ventanaR = new int[tamVentana]; |
62 |
int [] ventanaG = new int[tamVentana]; |
63 |
int [] ventanaB = new int[tamVentana]; |
64 |
|
65 |
px = ((BufferedImage) image).getRGB(col, line);
|
66 |
int alpha = (px & 0xff000000); //Extraigo el alpha para mantenerlo. |
67 |
|
68 |
// Obtener el vector con la ventanas de muestras (una por componente RGB)
|
69 |
int k=0; |
70 |
for (int i=-semiLado;i<=semiLado;i++) |
71 |
for(int j=-semiLado;j<=semiLado;j++) |
72 |
{ |
73 |
if((col+i >= 0) && (line+j >= 0) &&(col+i < width)&&(line+j < height)){ |
74 |
px = ((BufferedImage) image).getRGB(col+i, line+j);
|
75 |
ventanaR[k] =(px & 0x00ff0000) >> 16; |
76 |
ventanaG[k] =(px & 0x0000ff00) >> 8; |
77 |
ventanaB[k] =(px & 0x000000ff);
|
78 |
k++; |
79 |
} |
80 |
} |
81 |
|
82 |
// Ordenar los valores de las ventanas
|
83 |
// se supone que usa quickSort.
|
84 |
Arrays.sort(ventanaR,0,k); |
85 |
Arrays.sort(ventanaG,0,k); |
86 |
Arrays.sort(ventanaB,0,k); |
87 |
|
88 |
//Extraer los elementos centrales y asignarselos al pixel (x,y)
|
89 |
((BufferedImage) imageResult).setRGB(col, line, alpha |
|
90 |
((ventanaR[k>>1] << 16) & 0x00ff0000) | |
91 |
((ventanaG[k>>1] << 8) & 0x0000ff00) | |
92 |
(ventanaB[k>>1] & 0x000000ff)); |
93 |
} |
94 |
|
95 |
public void processLine(int y) { |
96 |
// TODO Auto-generated method stub
|
97 |
|
98 |
} |
99 |
|
100 |
public int getInRasterDataType() { |
101 |
return RasterBuf.TYPE_IMAGE;
|
102 |
} |
103 |
|
104 |
public int getOutRasterDataType() { |
105 |
return RasterBuf.TYPE_IMAGE;
|
106 |
} |
107 |
|
108 |
public Object getResult(String name) { |
109 |
if (name.equals("raster")) { |
110 |
return (Object) this.imageResult; |
111 |
} else {
|
112 |
return null; |
113 |
} |
114 |
} |
115 |
|
116 |
public void processSuperSampling(int col, int line){ |
117 |
int px;
|
118 |
ladoVentana = Math.abs(ladoVentana); // El lado de la ventana debe |
119 |
if(ladoVentana % 2 == 0) ladoVentana++; // ser positivo e impar. |
120 |
int tamVentana = ladoVentana*ladoVentana;
|
121 |
int semiLado = (ladoVentana-1)>>1; |
122 |
int indiceX,indiceY,origenX,origenY;
|
123 |
|
124 |
int [] ventanaR = new int[tamVentana]; |
125 |
int [] ventanaG = new int[tamVentana]; |
126 |
int [] ventanaB = new int[tamVentana]; |
127 |
|
128 |
px = ((BufferedImage) image).getRGB(col, line);
|
129 |
int alpha = (px & 0xff000000); //Extraigo el alpha para mantenerlo. |
130 |
|
131 |
if ((contX+1-semiLado>=0) && (contY+1-semiLado>=0)&& (contX+1+semiLado<stepX.length) && (contY+1+semiLado<stepY.length)){ |
132 |
|
133 |
// Calcular el alcance del kernel cuyo centro es (x,y)
|
134 |
int offsetX=0; |
135 |
int offsetY=0; |
136 |
for (int i=0;i<semiLado;i++){ |
137 |
offsetX=offsetX+stepX[contX+i+1];
|
138 |
offsetY=offsetY+stepY[contY+i+1];
|
139 |
} |
140 |
if((col+offsetX<width)&&(line+offsetY<height)){
|
141 |
// Obtener el vector con las ventanas de muestras (una por componente RGB) **************************
|
142 |
// Me situo en la esquina superior izquierda del "kernel"
|
143 |
origenX=col; |
144 |
origenY=line; |
145 |
for (int i=0;i<semiLado;i++){ |
146 |
origenX=origenX-stepX[contX-i]; |
147 |
origenY=origenY-stepY[contY-i]; |
148 |
} |
149 |
//Recorro el kernel seg�n los step
|
150 |
int k=0; |
151 |
indiceX=origenX; |
152 |
for (int i=-semiLado;i<=semiLado;i++){ |
153 |
indiceY=origenY; |
154 |
for(int j=-semiLado;j<=semiLado;j++){ |
155 |
px = ((BufferedImage) image).getRGB(indiceX, indiceY);
|
156 |
ventanaR[k] =(px & 0x00ff0000) >> 16; |
157 |
ventanaG[k] =(px & 0x0000ff00) >> 8; |
158 |
ventanaB[k] =(px & 0x000000ff);
|
159 |
indiceY=indiceY+stepY[contY+j+1];
|
160 |
k++; |
161 |
} |
162 |
indiceX=indiceX+stepX[contX+i+1];
|
163 |
} |
164 |
//**************************************************************************************************
|
165 |
|
166 |
// Ordenar los valores de las ventanas
|
167 |
// se supone que usa quickSort.
|
168 |
Arrays.sort(ventanaR);
|
169 |
Arrays.sort(ventanaG);
|
170 |
Arrays.sort(ventanaB);
|
171 |
|
172 |
//Extraer los elementos centrales y asignarselos al pixel (x,y)
|
173 |
for(int i = col; i < width && i < (col + stepX[contX + 1]); i++) |
174 |
for(int j = line; j < height && j < (line + stepY[contY + 1]); j++) |
175 |
((BufferedImage) imageResult).setRGB(i, j, alpha |
|
176 |
((ventanaR[(tamVentana-1)/2] << 16) & 0x00ff0000) | |
177 |
((ventanaG[(tamVentana-1)/2] << 8) & 0x0000ff00) | |
178 |
(ventanaB[(tamVentana-1)/2] & 0x000000ff)); |
179 |
return;
|
180 |
} |
181 |
} |
182 |
|
183 |
// Si la ventana se sale de la imagen dejar el pixeles original
|
184 |
for(int i = col; i < width && i < (col + stepX[contX + 1]); i++) |
185 |
for(int j = line; j < height && j < (line + stepY[contY + 1]); j++) |
186 |
((BufferedImage) imageResult).setRGB(i, j, px);
|
187 |
} |
188 |
} |