Revision 14322 trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/classification/ClassificationProcess.java

View differences:

ClassificationProcess.java
45 45
import java.awt.geom.AffineTransform;
46 46
import java.io.File;
47 47
import java.io.IOException;
48
import java.util.ArrayList;
49
import java.util.Iterator;
48 50

  
49 51
import javax.swing.JOptionPane;
50 52

  
......
53 55
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent;
54 56
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
55 57
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
56
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
57 58
import org.gvsig.raster.buffer.RasterBuffer;
58 59
import org.gvsig.raster.dataset.GeoRasterWriter;
59 60
import org.gvsig.raster.dataset.IBuffer;
60 61
import org.gvsig.raster.dataset.NotSupportedExtensionException;
61 62
import org.gvsig.raster.dataset.RasterDriverException;
63
import org.gvsig.raster.datastruct.ColorItem;
64
import org.gvsig.raster.datastruct.ColorTable;
62 65
import org.gvsig.raster.grid.Grid;
66
import org.gvsig.raster.grid.GridPalette;
67
import org.gvsig.raster.grid.filter.RasterFilterList;
68
import org.gvsig.raster.grid.filter.RasterFilterListManager;
69
import org.gvsig.raster.grid.filter.bands.ColorTableFilter;
70
import org.gvsig.raster.grid.filter.bands.ColorTableListManager;
71
import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter;
72
import org.gvsig.raster.grid.filter.statistics.TailTrimFilter;
73
import org.gvsig.raster.grid.roi.ROI;
74
import org.gvsig.raster.hierarchy.IRasterRendering;
63 75
import org.gvsig.raster.process.CancelEvent;
64 76
import org.gvsig.raster.process.RasterTask;
65 77
import org.gvsig.raster.process.RasterTaskQueue;
66 78
import org.gvsig.rastertools.cutting.WriterBufferServer;
67
import org.gvsig.rastertools.roi.ui.ROIManagerPanel;
68 79

  
69 80
import Jama.Matrix;
70 81

  
......
87 98
	private MapContext 				mapContext 			= null;
88 99
	private int 					percent 		  	= 0;
89 100
	private Thread 					blinker				= null;
90
	private ROIManagerPanel			roiPanel			= null;
101
	private ArrayList				rois				= null;
91 102
	private IncrementableTask		incrementableTask 	= null;
92 103
	private WriterBufferServer 		writerBufferServer	= null;
93 104
	private String 					fileNameOutput 		= null;
......
106 117
	 * @param	filenameOutput	string con la ruta del fichero de salida
107 118
	 * 			atransform  del raster original
108 119
	 * */
109
	public  ClassificationProcess(Grid imageGrid, ROIManagerPanel roisP, MapContext mapContext, String fileNameOutput,AffineTransform atransform){
120
	public  ClassificationProcess(Grid imageGrid, ArrayList rois, MapContext mapContext, String fileNameOutput,AffineTransform atransform){
110 121
		inputGrid= imageGrid;
111
		roiPanel= roisP;
122
		this.rois = rois;
112 123
		this.mapContext=mapContext;
113 124
		this.fileNameOutput= fileNameOutput;
114 125
		this.atransform= atransform;
......
130 141
		int iNX= inputGrid.getLayerNX();
131 142
		
132 143
		try{
133
			numClases = roiPanel.getTable().getRowCount();
144
			numClases = rois.size();
134 145
			// Se calculan las inversas de las matrices de Varianza-covarianza de todas las rois y se almacenan en inverseVarCovMAtrix
135 146
			Matrix Sinverse=null;
136 147
			inverseVarCovMatrix= new Matrix[numClases];
137 148
			
138 149
			for (int i=0; i<numClases;i++){
139
				S= new Matrix((roiPanel.getROI("ROI" + String.valueOf(i)).getRoi()).getVarCovMatrix());
150
				S= new Matrix(((ROI)rois.get(i)).getVarCovMatrix());
140 151
				Sinverse= S.inverse();
141 152
				inverseVarCovMatrix[i]= Sinverse;	
142 153
			 }
......
224 235
				if (endIndex < 0)
225 236
					endIndex = fileNameOutput.length();
226 237
				lyr = FLyrRasterSE.createLayer(fileNameOutput.substring(fileNameOutput.lastIndexOf(File.separator) + 1, endIndex),new File(fileNameOutput), mapContext.getProjection());
238
				
239
				setLeyend(lyr);
240
				
227 241
				mapContext.getLayers().addLayer(lyr);
228 242
				mapContext.endAtomicEvent();
229 243
				mapContext.invalidate();
......
236 250
				e.printStackTrace();	
237 251
			} catch (IOException e) {
238 252
				e.printStackTrace();
239
			} catch (NotInitializeException e) {
240
				e.printStackTrace();
241
			} 
242
			catch (InterruptedException e) {
253
			} catch (InterruptedException e) {
243 254
				Thread.currentThread().interrupt();
244 255
			} catch (LoadLayerException e) {
245 256
				JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
......
249 260
			}			
250 261

  
251 262
	} // Fin run()
263
	
264
	private void setLeyend(FLayer lyr) {
265
		if(lyr instanceof IRasterRendering) {
266
			IRasterRendering rendering = (IRasterRendering) lyr;
267
			ArrayList colorItems = new ArrayList();
268
			ColorItem colorItem = null;
269
			int classValue = 0;
270
			for (Iterator iter = rois.iterator(); iter.hasNext();) {
271
				ROI roi = (ROI) iter.next();
272
				colorItem = new ColorItem();
273
				colorItem.setColor(roi.getColor());
274
				colorItem.setNameClass(roi.getName());
275
				colorItem.setValue(classValue);
276
				colorItems.add(colorItem);
277
				classValue++;
278
			}
279
			ColorTable colorTable = new ColorTable();
280
			colorTable.createPaletteFromColorItems(colorItems, false);
281
			
282
			RasterFilterList filterList = rendering.getRenderFilterList();
283
			RasterFilterListManager manager = new RasterFilterListManager(filterList);
284
			ColorTableListManager cManager = (ColorTableListManager) manager.getManagerByClass(ColorTableListManager.class);
252 285

  
253
	
254
	
286
			filterList.remove(ColorTableFilter.class);
287

  
288
			((FLyrRasterSE)lyr).setLastLegend(null);
289

  
290
			filterList.remove(LinearEnhancementFilter.class);
291
			filterList.remove(TailTrimFilter.class);
292
			GridPalette gridPalette = new GridPalette(colorTable);
293
			cManager.addColorTableFilter(gridPalette);
294
			((FLyrRasterSE)lyr).setLastLegend(gridPalette);
295
			
296
			rendering.setRenderFilterList(filterList);
297
		}
298
	}
299

  
300

  
255 301
	/**
256 302
	 * Metodo que implementa el clasificador de maxima probabilidad. 
257 303
	 * Para cada pixel, obtiene la calase que minimiza la expresion: -Ln(P(x))= Ln(|Si|)+Y'* inverse(Si)*Y
......
265 311
		{
266 312
			double[][] y = new double[inputGrid.getBandCount()][1];
267 313
			for (int i=0;i<inputGrid.getBandCount();i++){
268
				roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().setBandToOperate(i);
269
				y[i][0]=pixelBandsValues[i]-(roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().getMeanValue());
314
				((ROI)rois.get(clase)).setBandToOperate(i);
315
				y[i][0]=pixelBandsValues[i]-((ROI)rois.get(clase)).getMeanValue();
270 316
			}
271 317
			Y = new Matrix(y);
272 318
			result= Y.transpose().times((Matrix)inverseVarCovMatrix[clase]).times(Y);
......
300 346
		{
301 347
			double[][] y = new double[inputGrid.getBandCount()][1];
302 348
			for (int i=0;i<inputGrid.getBandCount();i++){
303
				roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().setBandToOperate(i);
304
				y[i][0]=pixelBandsValues[i]-(roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().getMeanValue());
349
				((ROI)rois.get(clase)).setBandToOperate(i);
350
				y[i][0]=pixelBandsValues[i]-((ROI)rois.get(clase)).getMeanValue();
305 351
			}
306 352
			 Y = new Matrix(y);
307 353
			 result= (Y.transpose().times(inverseVarCovMatrix[clase])).times(Y);
......
335 381
		{
336 382
			double[][] y = new double[inputGrid.getBandCount()][1];
337 383
			for (int i=0;i<inputGrid.getBandCount();i++){
338
				roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().setBandToOperate(i);
339
				y[i][0]=pixelBandsValues[i]-(roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().getMeanValue());
384
				((ROI)rois.get(clase)).setBandToOperate(i);
385
				y[i][0]=pixelBandsValues[i]-((ROI)rois.get(clase)).getMeanValue();
340 386
			}
341 387
			 Y = new Matrix(y);
342 388
			 result= (Y.transpose().times(inverseVarCovMatrix[clase])).times(Y);
......
369 415
		{
370 416
			double[][] y = new double[inputGrid.getBandCount()][1];
371 417
			for (int i=0;i<inputGrid.getBandCount();i++){
372
				roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().setBandToOperate(i);
373
				y[i][0]=pixelBandsValues[i]-(roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().getMeanValue());
418
				((ROI)rois.get(clase)).setBandToOperate(i);
419
				y[i][0]=pixelBandsValues[i]-((ROI)rois.get(clase)).getMeanValue();
374 420
			}
375 421
			 Y = new Matrix(y);
376 422
			 result= (Y.transpose().times(inverseVarCovMatrix[clase])).times(Y);
......
389 435
		return clasefinal;
390 436
	}
391 437
	
392
	
438

  
393 439
	/**
394 440
	 * Metodo que implementa el clasificador de maxima probabilidad. 
395 441
	 * Para cada pixel, obtiene la calase que minimiza la expresion: -Ln(P(x))= Ln(|Si|)+Y'* inverse(Si)*Y
......
398 444
	 * @return clase a la que pertenece el pixel (por el metodo de maxima probabilidad)
399 445
	 * */
400 446
	public int getPixelClassForTypeDouble(double pixelBandsValues[]){
401
		double probabilidades[]=new double[roiPanel.getTable().getHeight()];
447
		double probabilidades[]=new double[numClases];
402 448
		for (int clase=0; clase<numClases;clase++)
403 449
		{
404 450
			double[][] y = new double[inputGrid.getBandCount()][1];
405 451
			for (int i=0;i<inputGrid.getBandCount();i++){
406
				roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().setBandToOperate(i);
407
				y[i][0]=pixelBandsValues[i]-(roiPanel.getROI("ROI" + String.valueOf(clase)).getRoi().getMeanValue());
452
				((ROI)rois.get(clase)).setBandToOperate(i);
453
				y[i][0]=pixelBandsValues[i]-((ROI)rois.get(clase)).getMeanValue();
408 454
			}
409 455
			 Y = new Matrix(y);
410 456
			 result= (Y.transpose().times(inverseVarCovMatrix[clase])).times(Y);
......
423 469
		return clasefinal;
424 470
	}
425 471
	
426
		
427 472
	public  RasterBuffer getGridResult(){
428 473
		return rasterResult;
429 474
	}

Also available in: Unified diff