glcm icon indicating copy to clipboard operation
glcm copied to clipboard

Correlation metric outside bounds of -1 to +1

Open ailich opened this issue 7 years ago • 7 comments

I'm getting values outside the bounds that should be possible for correlation. Correlation should be constrained to -1 to +1 (when scale_factor=1), but about 1% of the resulting non NA values seem to fall outside this range when using the glcm function on my raster data. The resulting correlation glcm raster has max and min values of +Inf and -Inf and also contains real numbers outside the -1 to +1 bounds ranging from about 2 to 47. (Running R version 3.4.1 on Windows 10)

ailich avatar Mar 27 '18 21:03 ailich

@ailich I have had the same problem. Do you have any news? I am trying to apply textural features as input variables of a machine learning model. I have doubts about whether to keep the values obtained (although I do not understand them) or assign 1 to values> 1 and -1 to values <-1.

sermomon avatar Jun 05 '20 15:06 sermomon

@sermomon I have not found a solution to this, but there have been github commits so it's possible that it might be fixed in a newer version (e.g. have you tried with the development branch on github (devtools::install_github("azvoleff /glcm") )? I asked this question on stack overflow and got some answers but I didn't feel that any solved the issue. The inf/-inf values make sense to me as that can happen if your using max and min values with na.rm=TRUE and all the inputs are NA, but the ones outside the range of -1 to 1 that are real numbers are odd to me. I ended up using other metrics because I was not confident in the correlation values. Additionally other free software such as QGIS can be used to calculate these metrics.

Also just some general good resources for GLCM include: https://prism.ucalgary.ca/bitstream/handle/1880/51900/texture%20tutorial%20v%203_0%20180206.pdf?sequence=11&isAllowed=y

https://www.tandfonline.com/doi/full/10.1080/01431161.2016.1278314?casa_token=M3ukyfRKO9UAAAAA%3A_ErxUHAqr0A3cHVl_EFIwUruzRhta-60xcmnhgz6IV2VJDYUvlUCSKw-JeMw-xO4iazqgW3DUyc

ailich avatar Jun 05 '20 15:06 ailich

@ailich Thanks for your quick response. I'm going to try with the developing version. I also have access to a Matlab, so I am going to also test from Matlab and report the results. Which tool do you suggest from QGis?

I agree with the comments I have read about the inf and -inf values, but I also don't understand the > 1 and <-1 values. It might be interesting to apply the glcm() function to a raster with the same value for all pixels. If the correlation values = 1, the probability is that it is an error, if, on the contrary, very high values are obtained, the correlation may be on another scale of values. Maybe my contribution is a little inexperienced, but I'm going to do these tests.

https://gis.stackexchange.com/questions/278890/glcm-r-package-correlation-metric-values-outside-bounds-of-1-to-1

sermomon avatar Jun 05 '20 16:06 sermomon

QGIS's processing toolbox has access to functions from many other open source GIS's. r.texture from GRASS GIS should do what you want. It's important to note that "r.texture assumes grey levels ranging from 0 to 255 as input. The input is automatically rescaled to 0 to 255 if the input map range is outside of this range," so you may need to use r.recode and r.quantile as well to do the quantization step. QGIS can also access the Orfeo Toolbox which has a tool to calculate GLCM textures. If you need to code it in R, the RQGIS3 package or RQGIS package lets you call QGIS from R. RQGIS3 is continually developed and works with QGIS3, wile RQGIS is no longer being developed and works with QGIS2. RQGIS3 will give you access to QGIS and GRASS Functions, but cannot link to the Orfeo Toolbox, while RQGIS can also link to the Orfeo Toolbox but its development is no longer being supported. If you do end up using RQGIS/RQGIS3, because you have to do this for many surfaces, I recommend learning how the tool works in QGIS first because having the GUI makes it easier to figure out how to set things up properly.

ailich avatar Jun 05 '20 17:06 ailich

I've created a small reproducible example to demonstrate the issue, which could maybe help understand it.

library(raster)
library(glcm)

glcm_corr<- glcm(raster(L5TSR_1986, layer=1), shift=list(c(0,1), c(1,1), c(1,0), c(1,-1)), statistics = "correlation")
glcm_corr[is.infinite(glcm_corr)]<-NA #remove Inf and -Inf

print(maxValue(glcm_corr))
[1] 11.96335 #Max value greater than 1

ailich avatar Jun 17 '20 02:06 ailich

@sermomon did you have any luck with the MATLAB comparisons?

ailich avatar Jul 06 '20 21:07 ailich

@sermomon, I coded up an alternative implementation here (https://github.com/ailich/GLCMTextures) if you want to test it out.

ailich avatar Nov 16 '20 19:11 ailich