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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 22 14:10:03 PST 2016


Author: mmetz
Date: 2016-11-22 14:10:03 -0800 (Tue, 22 Nov 2016)
New Revision: 69872

Modified:
   grass/trunk/raster/r.texture/h_measure.c
Log:
r.texture: handle possible division by zero

Modified: grass/trunk/raster/r.texture/h_measure.c
===================================================================
--- grass/trunk/raster/r.texture/h_measure.c	2016-11-22 17:54:58 UTC (rev 69871)
+++ grass/trunk/raster/r.texture/h_measure.c	2016-11-22 22:10:03 UTC (rev 69872)
@@ -407,21 +407,24 @@
 float f2_contrast(void)
 {
     int i, j /*, n */;
-    float sum, bigsum = 0;
+    float /* sum,*/ bigsum = 0;
     float **P = P_matrix;
 
+    /* the three-loop version does not work 
+     * when gray tones that do not occur in the current window 
+     * have been removed in tone and P* */
     /*
     for (n = 0; n < Ng; n++) {
 	sum = 0;
 	for (i = 0; i < Ng; i++) {
 	    for (j = 0; j < Ng; j++) {
-		if ((tone[i] - tone[j]) == tone[n] ||
-		    (tone[j] - tone[i]) == tone[n]) {
+		if ((i - j) == n ||
+		    (j - i) == n) {
 		    sum += P[i][j];
 		}
 	    }
 	}
-	bigsum += tone[n] * tone[n] * sum;
+	bigsum += n * n * sum;
     }
     */
 
@@ -462,6 +465,9 @@
 	    tmp += tone[i] * tone[j] * P[i][j];
     }
     stddev = sqrt(sum_sqr - (mean * mean));
+    
+    if (stddev == 0)	/* stddev < GRASS_EPSILON or similar ? */
+	return 0;
 
     return (tmp - mean * mean) / (stddev * stddev);
 }
@@ -594,8 +600,10 @@
 	sum_sqr += P[i] * P[i];
     }
     /* tmp = Ng * Ng; */
-    tmp = (tone[Ng - 1] - tone[0]) * (tone[Ng - 1] - tone[0]);
-    var = ((tmp * sum_sqr) - (sum * sum)) / (tmp * tmp);
+    if (Ng > 1) {
+	tmp = (tone[Ng - 1] - tone[0]) * (tone[Ng - 1] - tone[0]);
+	var = ((tmp * sum_sqr) - (sum * sum)) / (tmp * tmp);
+    }
 
     return var;
 }
@@ -639,6 +647,9 @@
     }
 
     /* fprintf(stderr,"hxy1=%f\thxy=%f\thx=%f\thy=%f\n",hxy1,hxy,hx,hy); */
+    if (hx == 0 && hy == 0)
+	return 0;
+
     return ((hxy - hxy1) / (hx > hy ? hx : hy));
 }
 



More information about the grass-commit mailing list