[GRASS-SVN] r67523 - grass-addons/grass7/imagery/i.spec.sam
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jan 8 12:47:30 PST 2016
Author: ychemin
Date: 2016-01-08 12:47:30 -0800 (Fri, 08 Jan 2016)
New Revision: 67523
Modified:
grass-addons/grass7/imagery/i.spec.sam/global.h
grass-addons/grass7/imagery/i.spec.sam/main.c
grass-addons/grass7/imagery/i.spec.sam/open.c
Log:
modifying process for non-squared input spectral matrix
Modified: grass-addons/grass7/imagery/i.spec.sam/global.h
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/global.h 2016-01-08 11:34:43 UTC (rev 67522)
+++ grass-addons/grass7/imagery/i.spec.sam/global.h 2016-01-08 20:47:30 UTC (rev 67523)
@@ -14,7 +14,9 @@
#define MAXFILES 255
extern vec_struct *b, *Avector;
-extern int matrixsize;
+extern int signature;
+extern int signaturecount;
+extern int spectralcount;
extern float curr_angle;
extern char *group;
Modified: grass-addons/grass7/imagery/i.spec.sam/main.c
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/main.c 2016-01-08 11:34:43 UTC (rev 67522)
+++ grass-addons/grass7/imagery/i.spec.sam/main.c 2016-01-08 20:47:30 UTC (rev 67523)
@@ -41,7 +41,8 @@
struct GModule *module;
vec_struct *b, *Avector;
-int matrixsize;
+int signaturecount;
+int spectralcount;
struct Ref Ref;
@@ -65,7 +66,7 @@
{
int nrows, ncols;
int row, col;
- int band;
+ int band, signature;
int i, j, error=0;
/*Assumes max 255 spectral signatures in input matrix*/
float anglefield[255][255];
@@ -75,7 +76,6 @@
} parm;
mat_struct *A; /*first use in open.c G_matrix_set()*/
- mat_struct *AT; /*first use in open.c G_matrix_set()*/
char *group;
float spectangle; /*numerical value of the spectral angle*/
@@ -149,7 +149,7 @@
for (j = 0; j < A->cols ; j++)
{
if (j !=i)
- G_message(" Angle between row %i and row %i: %g\n", (i+1), (j+1), anglefield[i][j]);
+ G_message(" Angle between signature at row %i and signature at row %i: %g\n", (i+1), (j+1), anglefield[i][j]);
}
G_message("\n");
@@ -157,14 +157,13 @@
for (i = 0; i < A->cols ; i++)
for (j = 0; j < A->cols ; j++)
if (j !=i)
- if (anglefield[i][j] < 10.0)
- {
- G_message("ERROR: Spectral entries row %i|%i in your matrix are linear dependent!\n",i,j);
- error=1;
- }
-
- if (!error)
- G_message("Spectral matrix is o.k. Proceeding...\n");
+ if (anglefield[i][j] < 10.0){
+ G_verbose_message("WARNING: Spectral entries row %i|%i in your matrix are linear dependent!\n",i,j);
+ error=1;
+ }
+ if(error==1) G_message("Similarity of input signatures detected, use verbose mode for details");
+
+ G_message("Proceeding...\n");
/* check singular values of Matrix A
* Ref: Boardman, J.W. 1989: Inversion of imaging spectrometry data
@@ -184,69 +183,65 @@
}
G_verbose_message("Singular values of Matrix A: run svdval");
if(G_math_svdval(svdvalues, Avals, A->cols, A->rows))
- G_fatal_error("Error in singular value decomposition, exiting...\n");
- G_verbose_message("/*display values (replace v_output() in original version)*/");
- /*display values (replace v_output() in original version)*/
+ G_verbose_message("Error in singular value decomposition, exiting...\n");
+ G_verbose_message("Display svd values");
+ /*display svd values*/
for(i=0;i<A->cols;i++)
- G_message("%f", svdvalues[i]);
+ G_verbose_message("%f", svdvalues[i]);
G_verbose_message("/* alright, start Spectral angle mapping */");
+ /*****************************************/
/* alright, start Spectral angle mapping */
+ /*****************************************/
nrows = Rast_window_rows();
ncols = Rast_window_cols();
G_verbose_message("Calculating for %i x %i = %i pixels:\n",nrows,ncols, (ncols * ncols));
G_verbose_message("%s ... ", G_program_name());
- G_message("%s ... ", G_program_name());
- for (row = 0; row < nrows; row++) /* rows loop*/
+ for (row = 0; row < nrows; row++)
{
G_percent(row, nrows, 2);
- for (band = 0; band < Ref.nfiles; band++) /* get row for all bands*/
+ /* get row for all bands*/
+ for (band = 0; band < Ref.nfiles; band++)
Rast_get_d_row (cellfd[band], cell[band], row);
- for (col = 0; col < ncols; col++) /* cols loop, work pixelwise for all bands */
- {
+ /* cols loop, work pixelwise for all bands */
+ for (col = 0; col < ncols; col++)
+ {
/* get pixel values of each band and store in b vector: */
- /* Yann: b is a spectral signature extracted for a given pixel */
- /*b = v_get(A->m); //m=rows; dim of vector=matrix size=Ref.nfiles*/
- b = G_vector_init(A->cols,A->cols,CVEC);
+ /* b is a spectral signature extracted for a given pixel */
+ b = G_vector_init(A->cols,A->cols,RVEC);
+ /*Roll through spectral bands*/
for (band = 0; band < Ref.nfiles; band++)
- b->vals[band] = cell[band][col]; /* read input vector */
-
+ b->vals[band] = cell[band][col]; /* read input vector */
/* calculate spectral angle for current pixel
* between pixel spectrum and reference spectrum
* and write result in full degree */
- for (i = 0; i < Ref.nfiles; i++) /* Ref.nfiles = matrixsize*/
- {
- Avector = G_matvect_get_column(A, i); /* go row-wise through matrix*/
- G_verbose_message("Av: %f %f %f %f",Avector->vals[0],Avector->vals[1],Avector->vals[2],Avector->vals[3]);
- G_verbose_message("b: %f %f %f %f",b->vals[0],b->vals[1],b->vals[2],b->vals[3]);
- spectangle = spectral_angle(Avector, b, CVEC);
- result_cell[i][col] = myround (spectangle);
- G_vector_free(Avector);
- }
- G_vector_free(b);
-
- } /* columns loop */
+ for (signature=0;signature<signaturecount;signature++)
+ {
+ G_verbose_message("row[%i] col[%i] signature[%i]", row,col,signature);
+ /* go row-wise through matrix*/
+ Avector = G_matvect_get_row(A, signature);
+ G_verbose_message("Av: %f %f %f %f",Avector->vals[0],Avector->vals[1],Avector->vals[2],Avector->vals[3]);
+ G_verbose_message("b: %f %f %f %f",b->vals[0],b->vals[1],b->vals[2],b->vals[3]);
+ spectangle = spectral_angle(Avector, b, RVEC);
+ result_cell[signature][col] = myround (spectangle);
+ G_vector_free(Avector);
+ }
+ G_vector_free(b);
+ } /* columns loop */
/* write the resulting rows: */
- for (i = 0; i < Ref.nfiles; i++)
- Rast_put_d_row (resultfd[i], result_cell[i]);
-
+ for (signature=0; signature<signaturecount; signature++)
+ Rast_put_d_row (resultfd[signature], result_cell[signature]);
} /* rows loop */
G_percent(row, nrows, 2);
-
/* close files */
- for (i = 0; i < Ref.nfiles; i++)
+ for (signature=0;signature<signaturecount;signature++)
{
- Rast_close (resultfd[i]);
- Rast_unopen(cellfd[i]);
- /* make grey scale color table */
- /*sprintf(result_name, "%s.%d", result_prefix, (i+1));
- sprintf(command, "r.colors map=%s color=grey >/dev/null", result_name);
- system(command);*/ /*Commented by Yann*/
- /* write a color table */
+ Rast_close (resultfd[signature]);
+ Rast_unopen(cellfd[signature]);
}
G_matrix_free(A);
make_history(result_name, parm.group->answer, parm.matrixfile->answer);
Modified: grass-addons/grass7/imagery/i.spec.sam/open.c
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/open.c 2016-01-08 11:34:43 UTC (rev 67522)
+++ grass-addons/grass7/imagery/i.spec.sam/open.c 2016-01-08 20:47:30 UTC (rev 67523)
@@ -51,7 +51,8 @@
}
G_verbose_message("\n");
}
- matrixsize=A->rows;
+ signaturecount=A->cols;
+ spectralcount=A->rows;
G_verbose_message("/* open input files from group */");
/* open input files from group */
@@ -69,11 +70,11 @@
G_message("only has 1 file\n");
G_fatal_error("The group must have at least 2 files\n");
}
- /* Error check: input file number must be equal to matrix size */
- if (Ref.nfiles != matrixsize)
+ /* Error check: input file number must be equal to spectral count */
+ if (Ref.nfiles != spectralcount)
{
G_message("Error: Number of %i input files in group <%s>\n", Ref.nfiles, img_grp);
- G_fatal_error(" does not match matrix size (%i cols).\n", A->cols);
+ G_fatal_error(" does not match spectral count (%i cols).\n", A->cols);
}
G_verbose_message("/* get memory for input files */");
@@ -92,14 +93,14 @@
G_verbose_message("/* open files for results*/");
/* open files for results*/
- result_cell = (DCELL **) G_malloc (Ref.nfiles * sizeof (DCELL *));
- resultfd = (int *) G_malloc (Ref.nfiles * sizeof (int));
- for (i=0; i < Ref.nfiles; i++)
+ result_cell = (DCELL **) G_malloc (signaturecount * sizeof (DCELL *));
+ resultfd = (int *) G_malloc (signaturecount * sizeof (int));
+ for (signature=0; signature < signaturecount; signature++)
{
- sprintf(result_name, "%s.%d", result_prefix, (i+1));
+ sprintf(result_name, "%s.%d", result_prefix, (signature+1));
G_verbose_message("Opening output file [%s]\n", result_name);
- result_cell[i] = Rast_allocate_d_buf();
- resultfd[i] = Rast_open_new (result_name, DCELL_TYPE);
+ result_cell[signature] = Rast_allocate_d_buf();
+ resultfd[signature] = Rast_open_new (result_name, DCELL_TYPE);
}
G_verbose_message("open.c: Returning A");
More information about the grass-commit
mailing list