Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / histogramMatching / HistogramMatchingFilter.java @ 2438

History | View | Annotate | Download (5.33 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.grid.filter.histogramMatching;
23

    
24
import org.gvsig.fmap.dal.coverage.datastruct.BufferHistogram;
25
import org.gvsig.fmap.dal.coverage.datastruct.Params;
26
import org.gvsig.fmap.dal.coverage.exception.HistogramException;
27
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
28
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
29
import org.gvsig.raster.impl.DefaultRasterManager;
30
import org.gvsig.raster.impl.buffer.RasterBuffer;
31
import org.gvsig.raster.impl.datastruct.BufferHistogramImpl;
32
import org.gvsig.raster.impl.store.ParamsImpl;
33

    
34
/**
35
 * Filtro para la aplicaci?n de HistogramMatching a un raster.
36
 * 
37
 * @author aMu?oz (alejandro.munoz@uclm.es)
38
 * @version 27-5-2008
39
 * */
40
public class HistogramMatchingFilter  extends BaseRasterFilter {
41

    
42
        public static String[]           names               = new String[] {"HistogramMatch"};
43
        public BufferHistogram           histogramReference  = null;
44
        private BufferHistogram          histogramSource     = null;
45
        private double [][]              acumulateS          = null;
46
        private double [][]              acumulateR          = null;
47
        public String                    fileNameOutput      = null;
48
        public byte                      tableAsign[][]      = null;
49
        public int                       numbands            = 0;
50
        
51
        /**
52
         * Constructor 
53
         **/
54
        public HistogramMatchingFilter() {
55
                        super();
56
        }
57

    
58
        public String getGroup() {
59
                return "HistogramMatch";
60
        }
61

    
62
        public int getInRasterDataType() {
63
                return 0;
64
        }
65

    
66
        public String[] getNames() {
67
                return names;                        
68
        }
69

    
70
        public int getOutRasterDataType() {
71
                return RasterBuffer.TYPE_BYTE;
72
        }
73

    
74

    
75
        public Params getUIParams(String nameFilter) {
76
                Params params = new ParamsImpl();
77
                return params;
78
        }
79

    
80

    
81
        public boolean isVisible() {
82
                return false;
83
        }
84

    
85
        /**
86
         * Acciones antes de la ejecuci?n del filtro
87
         * */
88
        public void pre() {        
89
                // Carga de los parametros del filtro y obtencion de histogramas
90
                try {
91
                        loadParam();
92
                } catch (HistogramException e) {
93
                        exec = false;
94
                } catch (ProcessInterruptedException e) {
95
                        exec = false;
96
                }
97
                
98
                // Funcion que calcula la correspondencia entre los histogramas, El resultado es tableAsign completa
99
                // para cada clase en el histograma fuente asigan su clase en el histograma corregido.
100
                double inValue=0;
101
                tableAsign= new byte[numbands][256];
102
                
103
                // C?lculo de la tabla de asignaciones entre los histogramas
104
                for (int band=0; band <numbands; band++){
105
                        for(int i= 0; i< tableAsign[0].length; i++){
106
                                inValue = acumulateS[band][i];
107
                                int value= (searchInHistogramReference(inValue,band));
108
                                tableAsign[band][i]= (byte)value;
109
                        }
110
                }
111
        }
112
        
113
        /**
114
         * Acciones posteriores a la ejecuci?n del filtro
115
         * */
116
        public void post() {                
117
        }
118
        
119
        /**
120
         *  Se recogen los parametros necesarios para la aplicacion del filtro. 
121
         *         histogramReference histograma de referencia.
122
         *        fileNameOutput 
123
         * @throws HistogramException 
124
         *  
125
         */
126
        protected void loadParam() throws HistogramException, ProcessInterruptedException {        
127
                // Raster
128
                raster = (RasterBuffer) params.get("raster"); 
129
                height = raster.getHeight();
130
                width = raster.getWidth();
131

    
132
                histogramSource = raster.getHistogramComputer().getBufferHistogram();
133
                histogramSource = BufferHistogramImpl.convertHistogramToRGB(histogramSource);
134

    
135
                // COMPROBACION HISTOGRAMA RGB
136
                if (!histogramSource.isInRangeRGB())
137
                        return;
138

    
139
                // Histograma de referencia
140
                histogramReference = (BufferHistogram)params.get("histogramReference");
141
                histogramReference = BufferHistogramImpl.convertHistogramToRGB(histogramReference);
142
                //numbands = (int)params.get("numbads");
143
                numbands = ((Integer)params.get("numbands")).intValue();
144
                // Histogramas acumilados y normalizados 
145
                acumulateS = BufferHistogramImpl.convertTableToNormalizeAccumulate(histogramSource.getTable());
146
                acumulateR = BufferHistogramImpl.convertTableToNormalizeAccumulate(histogramReference.getTable());
147

    
148
                rasterResult = DefaultRasterManager.getInstance().createBuffer(RasterBuffer.TYPE_BYTE,raster.getWidth(),raster.getHeight(),numbands,true);
149
                fileNameOutput = (String)params.get("fileNameOutput");
150
        }
151

    
152

    
153
        /**
154
         * M?todo que realiza la correspondencia entre las clases de los histogramas
155
         **/
156
        public int searchInHistogramReference(double value,int band) {
157
                int i = 0;
158
                while(value > acumulateR[band][i]) {
159
                        i++;
160
                        if(i == 255) 
161
                                return (int)255;
162
                }
163
                if(i == 0)
164
                        return 0;
165
                if((acumulateR[band][i]-value) < (acumulateR[band][i-1]-value))
166
                        return (int)i;
167
                else 
168
                        return (int)(i-1);
169
        }
170

    
171
        public void process(int x, int y) {
172
        }
173
}