Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / segmentation / FirstDerivativeDoubleFilter.java @ 8026

History | View | Annotate | Download (8.26 KB)

1
package org.cresques.filter.segmentation;
2

    
3
import org.cresques.filter.RasterFilter;
4
import org.cresques.io.data.RasterBuf;
5

    
6
public class FirstDerivativeDoubleFilter extends FirstDerivativeFilter {
7

    
8
        /**
9
         * Constructor sin par?metros. Este es usado principalmente desde controlTypes
10
         * ya que se instancia con newInstance y sin par?metros. Poseriormente le aplica
11
         * el nombre al filtro con la funci?n setFilterName de RasterFilter.
12
         */
13
        public FirstDerivativeDoubleFilter() {}
14
        
15
        /**
16
         * Constructor para la asignaci?n del identificador del filtro
17
         * @param fName Cadena que representa el identificador del filtro 
18
         */
19
        public FirstDerivativeDoubleFilter(String fName) {
20
                super(fName);
21
        }
22
        
23
        public void process(int col, int line) {
24
                double[] pxOut = new double[4];
25
                double[] px = new double[4];
26
                int convoResult[][] = new int[3][4];
27
                int ladoVentana = 3;
28
                int semiLado = (ladoVentana-1)>>1;
29
                
30
                double ventanaR[][]=new double[ladoVentana][ladoVentana];
31
                double ventanaG[][]=new double[ladoVentana][ladoVentana];
32
                double ventanaB[][]=new double[ladoVentana][ladoVentana];
33
                
34
                Kernel kernelRGB[]=new Kernel[3];
35

    
36
                raster.getElemDouble(line, col, px); 
37
                
38
                if((col-semiLado >= 0) && (line-semiLado >= 0) &&(col+semiLado < width)&&(line+semiLado < height)) 
39
                {
40
                                // Obtener el vector con la ventanas de muestras (una por componente RGB)
41
                        for (int i=-semiLado;i<=semiLado;i++)
42
                                for(int j=-semiLado;j<=semiLado;j++)
43
                                {
44
                                        raster.getElemDouble(line+j, col+i,px);
45
                                        ventanaR[i+semiLado][j+semiLado] =px[0];
46
                                        ventanaG[i+semiLado][j+semiLado] =px[1];
47
                                        ventanaB[i+semiLado][j+semiLado] =px[2];
48
                                }
49
                                kernelRGB[0]=new Kernel(ventanaR);
50
                                kernelRGB[1]=new Kernel(ventanaG);
51
                                kernelRGB[2]=new Kernel(ventanaB);
52
                                
53
                                convoResult[0][0]=(int)operatorH.convolution(kernelRGB[0]);
54
                                convoResult[0][1]=(int)operatorV.convolution(kernelRGB[0]);
55
                                convoResult[1][0]=(int)operatorH.convolution(kernelRGB[1]);
56
                                convoResult[1][1]=(int)operatorV.convolution(kernelRGB[1]);
57
                                convoResult[2][0]=(int)operatorH.convolution(kernelRGB[2]);
58
                                convoResult[2][1]=(int)operatorV.convolution(kernelRGB[2]);
59
                                
60
                                
61
                                if (compare){
62
                                        if (convoResult[0][0]>convoResult[0][1]) pxOut[0]= convoResult[0][0];
63
                                        else pxOut[0]= convoResult[0][1];
64
                                        
65
                                        if (convoResult[1][0]>convoResult[1][1]) pxOut[1]= convoResult[1][0];
66
                                        else pxOut[1]= convoResult[1][1];
67
                                        
68
                                        if (convoResult[2][0]>convoResult[2][1]) pxOut[2]= convoResult[2][0];
69
                                        else pxOut[2]= convoResult[2][1];
70
                                        
71
                                }
72
                                else{
73
                                        pxOut[0]=(int)Math.sqrt(Math.pow(convoResult[0][0],2)+Math.pow(convoResult[0][1],2));
74
                                        pxOut[1]=(int)Math.sqrt(Math.pow(convoResult[1][0],2)+Math.pow(convoResult[1][1],2));
75
                                        pxOut[2]=(int)Math.sqrt(Math.pow(convoResult[2][0],2)+Math.pow(convoResult[2][1],2));
76
                                }
77
                                
78
                                if (umbral>0){
79
                                        if(pxOut[0]>=umbral)pxOut[0]=255;
80
                                        else pxOut[0]=0;
81
                                        if(pxOut[1]>=umbral)pxOut[1]=255;
82
                                        else pxOut[1]=0;
83
                                        if(pxOut[2]>=umbral)pxOut[2]=255;
84
                                        else pxOut[2]=0;
85
                                }
86
                                else{
87
                                        if (pxOut[0]<0) pxOut[0]=0;
88
                                        else if (pxOut[0]>255) pxOut[0]=255;
89
                                        if (pxOut[1]<0) pxOut[1]=0;
90
                                        else if (pxOut[1]>255) pxOut[1]=255;
91
                                        if (pxOut[2]<0) pxOut[2]=0;
92
                                        else if (pxOut[2]>255) pxOut[2]=255;
93
                                }
94
                                
95
                        rasterResult.setElemDouble(line, col,pxOut);
96
                }
97
                else rasterResult.setElemDouble(line, col,pxOut);
98

    
99
        }
100

    
101
        public void processSuperSampling(int col, int line) {
102
                double[] px = new double[4];
103
                double[] pxOut = new double[4];
104
                int convoResult[][] = new int[3][4];
105
                int ladoVentana = 3;
106
                RasterFilter.Kernel kernelRGB[];
107

    
108
                raster.getElemDouble(line, col,px); 
109
                
110
                //        Obtener el vector con la ventanas de muestras (una por componente RGB)
111
                kernelRGB=extractSubRaster(ladoVentana,col,line);
112
                if(kernelRGB!=null)
113
                {
114
                        convoResult[0][0]=(int)operatorH.convolution(kernelRGB[0]);
115
                        convoResult[0][1]=(int)operatorV.convolution(kernelRGB[0]);
116
                        convoResult[1][0]=(int)operatorH.convolution(kernelRGB[1]);
117
                        convoResult[1][1]=(int)operatorV.convolution(kernelRGB[1]);
118
                        convoResult[2][0]=(int)operatorH.convolution(kernelRGB[2]);
119
                        convoResult[2][1]=(int)operatorV.convolution(kernelRGB[2]);
120
                        
121
                        if (compare){
122
                                if (convoResult[0][0]>convoResult[0][1]) pxOut[0]= convoResult[0][0];
123
                                else pxOut[0]= convoResult[0][1];
124
                                
125
                                if (convoResult[1][0]>convoResult[1][1]) pxOut[1]= convoResult[1][0];
126
                                else pxOut[1]= convoResult[1][1];
127
                                
128
                                if (convoResult[2][0]>convoResult[2][1]) pxOut[2]= convoResult[2][0];
129
                                else pxOut[2]= convoResult[2][1];
130
                                
131
                        }
132
                        else{
133
                                pxOut[0]=(int)Math.sqrt(Math.pow(convoResult[0][0],2)+Math.pow(convoResult[0][1],2));
134
                                pxOut[1]=(int)Math.sqrt(Math.pow(convoResult[1][0],2)+Math.pow(convoResult[1][1],2));
135
                                pxOut[2]=(int)Math.sqrt(Math.pow(convoResult[2][0],2)+Math.pow(convoResult[2][1],2));
136
                        }
137
                        
138
                        if (umbral>0){
139
                                if(pxOut[0]>=umbral)pxOut[0]=255;
140
                                else pxOut[0]=0;
141
                                if(pxOut[1]>=umbral)pxOut[1]=255;
142
                                else pxOut[1]=0;
143
                                if(pxOut[2]>=umbral)pxOut[2]=255;
144
                                else pxOut[2]=0;
145
                        }
146
                        else{
147
                                
148
                                if (pxOut[0]<0) pxOut[0]=0;
149
                                else if (pxOut[0]>255) pxOut[0]=255;
150
                                if (pxOut[1]<0) pxOut[1]=0;
151
                                else if (pxOut[1]>255) pxOut[1]=255;
152
                                if (pxOut[2]<0) pxOut[2]=0;
153
                                else if (pxOut[2]>255) pxOut[2]=255;
154
                        }
155
                        
156
                        for(int i = col; i < width && i < (col + stepX[contX + 1]); i++)
157
                                for(int j = line; j < height && j < (line + stepY[contY + 1]); j++)        
158
                                        rasterResult.setElemDouble(j, i,pxOut);
159
                }
160
                else
161
                for(int i = col; i < rasterResult.getWidth() && i < (col + stepX[contX + 1]); i++)
162
                        for(int j = line; j < rasterResult.getHeight() && j < (line + stepY[contY + 1]); j++)        
163
                                rasterResult.setElemDouble(j, i,px);
164
        }
165

    
166
        public void processLine(int y) {
167
                // TODO Auto-generated method stub
168

    
169
        }
170

    
171
        public int getInRasterDataType() {
172
                return RasterBuf.TYPE_DOUBLE;
173
        }
174

    
175
        public int getOutRasterDataType() {
176
                return RasterBuf.TYPE_DOUBLE;
177
        }
178

    
179
        public Object getResult(String name) {
180
                if (name.equals("raster")) {
181
            return (Object) this.rasterResult;
182
        } else {
183
            return null;
184
        }
185
        }
186

    
187
        public void pre() {
188
                exec = true;
189
                this.raster = (RasterBuf)params.get("raster");
190
                height = raster.getHeight();
191
        width = raster.getWidth();
192
                rasterResult = new RasterBuf(raster.getDataType(),width,height,raster.getBandCount(),true);
193
                
194
                super.pre();
195
        }
196
        
197
        /**
198
         * @param ladoVentana  Lado del subraster a extraer
199
         * @param x  Coordenada x del pixel central
200
         * @param y  Coordenada y del pixel central
201
         * @return
202
         */
203
        public Kernel[] extractSubRaster(int ladoVentana,int x,int y){
204
                        
205
                        int k,indiceX,indiceY,origenX,origenY;
206
                        double[]                 px = new double[4];
207
                        int semiLado = (ladoVentana-1)>>1;
208
                        int offsetX=0;
209
                        int offsetY=0;
210
                        
211
                        Kernel kernelRGB[] = null;
212
                        
213
                        // Obtener el vector con las ventanas de muestras (una por componente RGB) **************************
214
                        // Me situo en la esquina superior izquierda del "kernel"
215
                        if ((contX+1-semiLado>=0) && (contY+1-semiLado>=0)&& (contX+1+semiLado<stepX.length) && (contY+1+semiLado<stepY.length)){
216
                                
217
                                // Calcular el alcance del kernel cuyo centro es (x,y)
218
                                for (int i=0;i<semiLado;i++){
219
                                        offsetX=offsetX+stepX[contX+i+1];
220
                                        offsetY=offsetY+stepY[contY+i+1];
221
                                }
222
                                if((x+offsetX<width)&&(y+offsetY<height)){
223
                                        
224
                                        double ventanaR[][] = new double[ladoVentana][ladoVentana];
225
                                        double ventanaG[][] = new double[ladoVentana][ladoVentana];
226
                                        double ventanaB[][] = new double[ladoVentana][ladoVentana];
227
                                        kernelRGB = new Kernel[3];
228
                                        
229
                                        origenX=x;
230
                                        origenY=y;
231
                                        for (int i=0;i<semiLado;i++){
232
                                                origenX=origenX-stepX[contX-i];
233
                                                origenY=origenY-stepY[contY-i];        
234
                                        }
235
                                        //Recorro el kernel seg?n los step
236
                                        k=0;
237
                                        indiceX=origenX;
238
                                        for (int i=-semiLado;i<=semiLado;i++){
239
                                                indiceY=origenY;
240
                                                for(int j=-semiLado;j<=semiLado;j++){        
241
                                                        raster.getElemDouble(indiceY,indiceX,px);        
242
                                                        ventanaR[i+semiLado][j+semiLado] =px[0];
243
                                                        ventanaG[i+semiLado][j+semiLado] =px[1];
244
                                                        ventanaB[i+semiLado][j+semiLado] =px[2];
245
                                                        indiceY=indiceY+stepY[contY+j+1];
246
                                                        k++;
247
                                                }
248
                                                indiceX=indiceX+stepX[contX+i+1];
249
                                        }
250
                                        //**************************************************************************************************
251
                                        kernelRGB[0]= new Kernel(ventanaR);
252
                                        kernelRGB[1]= new Kernel(ventanaG);
253
                                        kernelRGB[2]= new Kernel(ventanaB);
254
                                }
255
                        }
256
                        return kernelRGB;
257
                }
258

    
259

    
260
}