Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extRemoteSensing / src-test / org / gvsig / remotesensing / processtest / TFusionPCProcess.java @ 26348

History | View | Annotate | Download (5.94 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Iba?ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.remotesensing.processtest;
42

    
43
import junit.framework.TestCase;
44

    
45

    
46
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
47
import org.gvsig.raster.RasterLibrary;
48
import org.gvsig.raster.buffer.BufferFactory;
49
import org.gvsig.raster.buffer.RasterBuffer;
50
import org.gvsig.raster.buffer.RasterBufferInvalidException;
51
import org.gvsig.raster.grid.Grid;
52
import org.gvsig.remotesensing.principalcomponents.PCImageProcess;
53
import org.gvsig.remotesensing.principalcomponents.PCStatisticsProcess;
54

    
55
import Jama.Matrix;
56

    
57
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
58

    
59

    
60
/**
61
* Este test prueba la obtencion de la imagen original a partir de las 
62
* componetes principales calculadas. En el proceso de fusi?n de im?genes 
63
* por el m?todo de PCA est? b?sado en transformaci?n inversa PCA.
64
* 
65
* ** @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
66
* */
67

    
68
public class TFusionPCProcess extends TestCase {
69

    
70
        private String baseDir = "./test-images/";
71
        private String path1 = baseDir+"pc_CreateImageTest.tif";
72
        FLyrRasterSE lyr = null;
73
        
74
        static{
75
                RasterLibrary.wakeUp();        
76
        }
77
        
78
        public void start() {
79
                this.setUp();
80
                this.testStack();
81
        }
82
        
83
        public void setUp() {
84
                System.err.println("TFusionPC running...");
85
                try{
86
                        lyr = FLyrRasterSE.createLayer(
87
                                        path1,
88
                                        path1,
89
                                        null
90
                                        );
91
                
92
                } catch (LoadLayerException e) {
93
                        System.out.print("Error en la construcci?n de la capa");
94
                } 
95
        }
96
        
97
        public void testStack() {
98
                
99
                PCStatisticsProcess sProcess= new PCStatisticsProcess();
100
                sProcess.addParam("selectedBands",new boolean[]{true,true,true});
101
                sProcess.addParam("inputRasterLayer",lyr);
102
                sProcess.run();
103
                
104
                Matrix autoV=sProcess.getAutoVectorMatrix();
105
                // Reordenamos en orden descencente del valor de los autovectores
106
                int resultOrden[]= new int[autoV.getRowDimension()];
107
                int cont = autoV.getRowDimension()-1;
108
                for(int i=0;i<autoV.getRowDimension();i++){
109
                                        resultOrden[i]=cont;
110
                                        cont--;
111
                }
112
                double order[][]= new double[autoV.getRowDimension()][autoV.getColumnDimension()];
113
                for(int i=0; i<resultOrden.length;i++)
114
                        for(int j=0; j<autoV.getColumnDimension();j++)
115
                                order[i][j]=autoV.get(j,resultOrden[i]);
116
                                
117
                
118
                PCImageProcess imgProcess= new PCImageProcess();
119
                imgProcess.addParam("inputRasterLayer",lyr);
120
                imgProcess.addParam("statistics",sProcess.getResult());
121
                imgProcess.addParam("selectedBands",new boolean[]{true,true,true});
122
                imgProcess.addParam("selectedComponents",new boolean[]{true,true,true});
123
                imgProcess.addParam("outputPath",new String ("outfile.tif"));
124
                imgProcess.run();
125
                
126
                FLyrRasterSE resultado= (FLyrRasterSE) imgProcess.getResult();
127
                Matrix inverse=autoV.inverse();
128
                
129
                BufferFactory ds1 = new BufferFactory(resultado.getDataSource());
130
                BufferFactory ds2 = new BufferFactory(lyr.getDataSource());
131
                Grid dataGrid=null; Grid dataOrigen=null;
132
                try {
133
                        dataGrid= new Grid(ds1);
134
                        dataOrigen= new Grid(ds2);
135
                } catch (RasterBufferInvalidException e) {
136
                        e.printStackTrace();
137
                }
138
                float newData[]= new float[3];
139
                float data[] = new float[3];
140
                
141
                
142
                RasterBuffer rasterResult= RasterBuffer.getBuffer(RasterBuffer.TYPE_FLOAT, dataGrid.getRasterBuf().getWidth(), dataGrid.getRasterBuf().getHeight(), dataGrid.getBandCount(), true);
143
                for(int row=0; row<dataGrid.getRasterBuf().getHeight(); row++){         
144
                        for(int col=0; col<dataGrid.getRasterBuf().getWidth();col++){        
145
                                dataGrid.getRasterBuf().getElemFloat(row,col,data);{
146
                                
147
                                                newData=solveSystem(new Matrix(order),data);
148
                                                //newData[i]+= data[i]*inverse.get(resultOrden[i],j);        
149
                                }
150
                                rasterResult.setElemFloat(row,col,newData);
151
                        }
152
                }
153
        
154
                
155
//                 Comprobar que el resultado de la imagen original es el mismo que tras aplicar la inversa a las componentes
156
                
157
        for(int i=0; i<dataGrid.getRasterBuf().getHeight();i++)
158
                for(int j=0; j<dataGrid.getRasterBuf().getHeight();j++){
159
                        assertEquals(rasterResult.getElemFloat(i,j,0),(float)dataOrigen.getRasterBuf().getElemShort(i,j,0),0.1);
160
                        assertEquals(rasterResult.getElemFloat(i,j,1),(float)dataOrigen.getRasterBuf().getElemShort(i,j,1),0.1);
161
                        assertEquals(rasterResult.getElemFloat(i,j,2),(float)dataOrigen.getRasterBuf().getElemShort(i,j,2),0.1);
162
                }
163
        }
164
        
165
        
166
        public float[] solveSystem(Matrix matrix, float columResult[]){
167
                float xCoef[] = new float[3];
168
                double[][] a = new double[columResult.length][1];
169
                for (int i = 0; i < columResult.length; i++)
170
                        a[i][0] = columResult[i];
171
                Matrix c = null;
172
                //if (matrix.det() == 0.0) {
173
                        // Resolucion del sistema usando la libreria flanagan
174
                //        flanagan.math.Matrix matrixFL = new flanagan.math.Matrix(matrix.getArray());
175
                //        xCoef = matrixFL.solveLinearSet(columResult);
176
                //} else {
177
                c = matrix.solve(new Matrix(a));
178
                for (int i = 0; i < columResult.length; i++)
179
                                xCoef[i] = (float)c.get(i, 0);
180
        //        }
181
                return xCoef;
182
        }
183

    
184
        
185
}