[GRASS-SVN] r69965 - grass/trunk/raster/r.texture

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Dec 1 12:30:33 PST 2016


Author: mmetz
Date: 2016-12-01 12:30:33 -0800 (Thu, 01 Dec 2016)
New Revision: 69965

Modified:
   grass/trunk/raster/r.texture/h_measure.c
   grass/trunk/raster/r.texture/h_measure.h
   grass/trunk/raster/r.texture/main.c
Log:
r.texture: add flag to allow NULL cells

Modified: grass/trunk/raster/r.texture/h_measure.c
===================================================================
--- grass/trunk/raster/r.texture/h_measure.c	2016-12-01 17:09:56 UTC (rev 69964)
+++ grass/trunk/raster/r.texture/h_measure.c	2016-12-01 20:30:33 UTC (rev 69965)
@@ -119,21 +119,36 @@
 }
 
 int set_vars(int **grays, int curr_row, int curr_col,
-                int size, int offset, int t_d)
+                int size, int offset, int t_d, int with_nulls)
 {
     int R0, R45, R90, R135, x, y;
     int row, col, row2, col2, rows, cols;
+    int rowmin, rowmax, colmin, colmax, wrows, wcols, rowd, cold;
     int itone, jtone;
     int cnt;
 
     rows = cols = size;
+    wrows = Rast_window_rows();
+    wcols = Rast_window_cols();
 
     /* Determine the number of different gray scales (not maxval) */
     for (row = 0; row <= PGM_MAXMAXVAL; row++)
 	tone[row] = -1;
     cnt = 0;
-    for (row = curr_row - offset; row <= curr_row + offset; row++) {
-	for (col = curr_col - offset; col <= curr_col + offset; col++) {
+    rowmin = curr_row - offset;
+    if (rowmin < 0)
+	rowmin = 0;
+    rowmax = curr_row + offset;
+    if (rowmax > wrows - 1)
+	rowmax = wrows - 1;
+    colmin = curr_col - offset;
+    if (colmin < 0)
+	colmin = 0;
+    colmax = curr_col + offset;
+    if (colmax > wcols - 1)
+	colmax = wcols - 1;
+    for (row = rowmin; row <= rowmax; row++) {
+	for (col = colmin; col <= colmax; col++) {
 	    if (grays[row][col] < 0) {	/* No data pixel found */
 		continue;
 	    }
@@ -146,8 +161,9 @@
 	}
     }
     /* what is the minimum number of pixels 
-     * to get reasonable texture measurements ? */
-    if (cnt < t_d * t_d * 4)
+     * to get reasonable texture measurements ? 
+     * at the very least, any of R0, R45, R90, R135 must be > 1 */
+    if (cnt < size * size / 4 || (!with_nulls && cnt < size * size))
 	return 0;
 
     /* Collapse array, taking out all zero values */
@@ -180,35 +196,49 @@
     /* Find gray-tone spatial dependence matrix */
     for (row = 0; row < rows; row++) {
 	row2 = curr_row - offset + row;
+	if (row2 < 0 || row2 >= wrows)
+	    continue;
 	for (col = 0; col < cols; col++) {
 	    col2 = curr_col - offset + col;
+	    if (col2 < 0 || col2 >= wcols)
+		continue;
 	    if (grays[row2][col2] < 0)
 		continue;
 	    x = bsearch_gray(tone, Ng, grays[row2][col2]);
-	    if (col + t_d < cols &&
-	        grays[row2][col2 + t_d] >= 0) {
-		y = bsearch_gray(tone, Ng, grays[row2][col2 + t_d]);
+	    rowd = row2;
+	    cold = col2 + t_d;
+	    if (col + t_d < cols && cold < wcols &&
+	        grays[rowd][cold] >= 0) {
+		y = bsearch_gray(tone, Ng, grays[rowd][cold]);
 		P_matrix0[x][y]++;
 		P_matrix0[y][x]++;
 		R0 += 2;
 	    }
-	    if (row + t_d < rows &&
-	        grays[row2 + t_d][col2] >= 0) {
-		y = bsearch_gray(tone, Ng, grays[row2 + t_d][col2]);
+	    rowd = row2 + t_d;
+	    cold = col2;
+	    if (row + t_d < rows && rowd < wrows &&
+	        grays[rowd][cold] >= 0) {
+		y = bsearch_gray(tone, Ng, grays[rowd][cold]);
 		P_matrix90[x][y]++;
 		P_matrix90[y][x]++;
 		R90 += 2;
 	    }
-	    if (row + t_d < rows && col - t_d >= 0 &&
-	        grays[row2 + t_d][col2 - t_d] >= 0) {
-		y = bsearch_gray(tone, Ng, grays[row2 + t_d][col2 - t_d]);
+	    rowd = row2 + t_d;
+	    cold = col2 - t_d;
+	    if (row + t_d < rows && rowd < wrows &&
+	        col - t_d >= 0 && cold >= 0 &&
+	        grays[rowd][cold] >= 0) {
+		y = bsearch_gray(tone, Ng, grays[rowd][cold]);
 		P_matrix45[x][y]++;
 		P_matrix45[y][x]++;
 		R45 += 2;
 	    }
-	    if (row + t_d < rows && col + t_d < cols &&
-	        grays[row2 + t_d][col2 + t_d] >= 0) {
-		y = bsearch_gray(tone, Ng, grays[row2 + t_d][col2 + t_d]);
+	    rowd = row2 + t_d;
+	    cold = col2 + t_d;
+	    if (row + t_d < rows && rowd < wrows &&
+	        col + t_d < cols && cold < wcols &&
+	        grays[rowd][cold] >= 0) {
+		y = bsearch_gray(tone, Ng, grays[rowd][cold]);
 		P_matrix135[x][y]++;
 		P_matrix135[y][x]++;
 		R135 += 2;

Modified: grass/trunk/raster/r.texture/h_measure.h
===================================================================
--- grass/trunk/raster/r.texture/h_measure.h	2016-12-01 17:09:56 UTC (rev 69964)
+++ grass/trunk/raster/r.texture/h_measure.h	2016-12-01 20:30:33 UTC (rev 69965)
@@ -26,6 +26,6 @@
 float h_measure(int);
 void alloc_vars(int);
 int set_vars(int **grays, int curr_rrow, int curr_col,
-                int size, int offset, int t_d);
+                int size, int offset, int t_d, int with_nulls);
 int set_angle_vars(int angle, int have_px, int have_py,
                    int have_pxpys, int have_pxpyd);

Modified: grass/trunk/raster/r.texture/main.c
===================================================================
--- grass/trunk/raster/r.texture/main.c	2016-12-01 17:09:56 UTC (rev 69964)
+++ grass/trunk/raster/r.texture/main.c	2016-12-01 20:30:33 UTC (rev 69965)
@@ -92,7 +92,7 @@
     RASTER_MAP_TYPE out_data_type;
     struct GModule *module;
     struct Option *opt_input, *opt_output, *opt_size, *opt_dist, *opt_measure;
-    struct Flag *flag_ind, *flag_all;
+    struct Flag *flag_ind, *flag_all, *flag_null;
     struct History history;
     char p[1024];
 
@@ -157,6 +157,11 @@
     flag_all->key = 'a';
     flag_all->description = _("Calculate all textural measurements");
 
+    flag_null = G_define_flag();
+    flag_null->key = 'n';
+    flag_null->label = _("Allow NULL cells in a moving window");
+    flag_null->description = _("This will also avoid cropping along edges of the current region");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -313,10 +318,21 @@
      ***************************************************************************************************/
 
     offset = size / 2;
-    first_row = first_col = offset;
-    last_row = nrows - offset;
-    last_col = ncols - offset;
+
+    if (!flag_null->answer) {
+	first_row = first_col = offset;
+	last_row = nrows - offset;
+	last_col = ncols - offset;
+    }
+    else {
+	/* no cropping at window margins */
+	first_row = first_col = 0;
+	last_row = nrows;
+	last_col = ncols;
+    }
+
     Rast_set_f_null_value(fbuf[0], ncols);
+
     for (row = 0; row < first_row; row++) {
 	for (i = 0; i < n_outputs; i++) {
 	    Rast_put_row(outfd[i], fbuf[0], out_data_type);
@@ -337,7 +353,7 @@
 	/*process the data */
 	for (col = first_col; col < last_col; col++) {
 
-	    if (!set_vars(data, row, col, size, offset, dist)) {
+	    if (!set_vars(data, row, col, size, offset, dist, flag_null->answer)) {
 		for (i = 0; i < n_outputs; i++)
 		    Rast_set_f_null_value(&(fbuf[i][col]), 1);
 		continue;



More information about the grass-commit mailing list