Skip to content

Commit

Permalink
Add maxcell for quantization
Browse files Browse the repository at this point in the history
  • Loading branch information
ailich committed Nov 15, 2023
1 parent ede0d3c commit 8e1d4ba
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: GLCMTextures
Title: GLCM Textures of Raster Layers
Version: 0.3.9
Version: 0.3.10
Authors@R:
person(given = "Alexander",
family = "Ilich",
Expand Down
5 changes: 3 additions & 2 deletions R/glcm_textures.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @param quantization quantization method (either "equal range", "equal prob", or "none"). "equal range" quantization will create bins that cover a range of equal size. "equal prob" performs equal probability quantization and will use quantiles to create bins with approximately equal number of samples. "none" means the layer has already been quantized.
#' @param min_val minimum value for equal range quantization (if not supplied, the minimum value of the raster is used)
#' @param max_val maximum value for equal range quantization (if not supplied, the maximum value of the raster is used)
#' @param maxcell positive integer used to take a regular sample for quantization if "equal prob" is used (default is Inf)
#' @param na.rm a logical value indicating whether NA values should be stripped before the computation proceeds (default=FALSE)
#' @param include_scale Logical indicating whether to append window size to the layer names (default = FALSE).
#' @param filename character Output filename. Can be a single filename, or as many filenames as there are layers to write a file for each layer
Expand All @@ -33,7 +34,7 @@
#' Haralick, R.M., Shanmugam, K., Dinstein, I., 1973. Textural features for image classification. IEEE Transactions on Systems, Man, and Cybernetics 610–621. https://doi.org/10.1109/TSMC.1973.4309314
#' @export
#'
glcm_textures<- function(r, w = c(3,3), n_levels, shift=list(c(1,0), c(1,1), c(0,1), c(-1,1)), metrics= c("glcm_contrast", "glcm_dissimilarity", "glcm_homogeneity", "glcm_ASM", "glcm_entropy", "glcm_mean", "glcm_variance", "glcm_correlation"), quantization, min_val=NULL, max_val=NULL, na.rm=FALSE, include_scale=FALSE, filename=NULL, overwrite=FALSE, wopt=list()){
glcm_textures<- function(r, w = c(3,3), n_levels, shift=list(c(1,0), c(1,1), c(0,1), c(-1,1)), metrics= c("glcm_contrast", "glcm_dissimilarity", "glcm_homogeneity", "glcm_ASM", "glcm_entropy", "glcm_mean", "glcm_variance", "glcm_correlation"), quantization, min_val=NULL, max_val=NULL, maxcell=Inf, na.rm=FALSE, include_scale=FALSE, filename=NULL, overwrite=FALSE, wopt=list()){
og_class<- class(r)[1]
if(og_class=="RasterLayer"){
r<- terra::rast(r) #Convert to SpatRaster
Expand Down Expand Up @@ -84,7 +85,7 @@ glcm_textures<- function(r, w = c(3,3), n_levels, shift=list(c(1,0), c(1,1), c(0

out_list<- vector(mode = "list", length=length(shift))
if(quantization!="none"){
r<- quantize_raster(r = r, n_levels = n_levels, method = quantization, min_val = min_val, max_val = max_val, wopt=wopt)
r<- quantize_raster(r = r, n_levels = n_levels, method = quantization, min_val = min_val, max_val = max_val, maxcell=maxcell, wopt=wopt)
} else if(!terra::is.int(r)){
r<- terra::as.int(r, wopt=wopt) #Make it an integer raster
}
Expand Down
7 changes: 5 additions & 2 deletions R/quantize_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param method quantization method (either "equal range" or "equal prob"). "equal range" quantization will create bins that cover a range of equal size. "equal prob" performs equal probability quantization and will use quantiles to create bins with approximately equal number of samples.
#' @param min_val minimum value for equal range quantization (if not supplied, the minimum value of the raster is used)
#' @param max_val maximum value for equal range quantization (if not supplied, the maximum value of the raster is used)
#' @param maxcell positive integer used to take a regular sample of x if "equal prob" is used (default is Inf)
#' @param filename character Output filename.
#' @param overwrite logical. If TRUE, filename is overwritten (default is FALSE).
#' @return a single layer SpatRaster or RasterLayer with integer values ranging from 0 to n_levels-1
Expand All @@ -26,7 +27,7 @@
#' Hyndman, R.J., Fan, Y., 1996. Sample Quantiles in Statistical Packages. The American Statistician 50, 361–365. https://doi.org/10.1080/00031305.1996.10473566
#' @export

quantize_raster<- function(r, n_levels, method, min_val=NULL, max_val=NULL, filename= NULL, overwrite = FALSE, wopt=list()){
quantize_raster<- function(r, n_levels, method, min_val=NULL, max_val=NULL, maxcell=Inf, filename= NULL, overwrite = FALSE, wopt=list()){
og_class<- class(r)[1]
if(og_class =="RasterLayer"){
r<- terra::rast(r) #Convert to SpatRaster
Expand All @@ -39,10 +40,12 @@ quantize_raster<- function(r, n_levels, method, min_val=NULL, max_val=NULL, file
if(is.null(max_val)){max_val<- unlist(terra::global(r, fun = max, na.rm=TRUE))}
qrules<- seq(min_val, max_val, length.out = (n_levels + 1))
} else if (method == "equal prob") {
qrules<- unlist(terra::global(r, fun = quantile, probs= seq(0,1,length.out = n_levels+1), na.rm=TRUE, type=8))
qrules<- unlist(terra::global(r, fun = quantile, probs= seq(0,1,length.out = n_levels+1), na.rm=TRUE, type=8, maxcell=maxcell))
} else {
message("Error: method must be 'equal range' or 'equal prob'")
}
qrules[1]<- -Inf
qrules[length(qrules)]<- Inf #Make edges infinite in case sample is used which doesn't contain min/max value
rq <- terra::classify(r, rcl = qrules, include.lowest = TRUE, right = FALSE, wopt=wopt)
rq<- terra::as.int(rq, wopt=wopt)

Expand Down
3 changes: 3 additions & 0 deletions man/glcm_textures.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/quantize_raster.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e1d4ba

Please sign in to comment.