Skip to content

Commit

Permalink
Merge pull request #9 from bluegreen-labs/harmonic_cutoff
Browse files Browse the repository at this point in the history
Harmonic cutoff
  • Loading branch information
khufkens authored Jan 19, 2024
2 parents ecbbb36 + efb19d4 commit f6513b2
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ Suggests:
License: AGPL-3
LazyData: true
ByteCompile: true
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# foto 1.2

* fix plotting routine
* flexible constraints on harmonics
* adding new vignette - theoretical basis

# foto 1.1
Expand Down
26 changes: 14 additions & 12 deletions R/foto.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param x an image file, or single or multi-layer SpatRaster
#' (RGB or otherwise), multi-layer data are averaged to a single layer
#' @param window_size a moving window size in pixels (default = 61 pixels)
#' @param harmonics number of harmonics to consider (29 by default)
#' @param method zones (for discrete zones) or mw for a moving window
#' approach
#' @param pca execute PCA, \code{TRUE} or \code{FALSE}. If \code{FALSE} only
Expand Down Expand Up @@ -41,13 +42,16 @@
#' print(names(output))
#' }
#'
foto <- function(x,
window_size = 61,
method = "zones",
norm_spec = FALSE,
high_pass = TRUE,
pca = TRUE,
plot = FALSE) {
foto <- function(
x,
window_size = 61,
harmonics = 29,
method = "zones",
norm_spec = FALSE,
high_pass = TRUE,
pca = TRUE,
plot = FALSE
) {

# get the current enviroment
env <- environment()
Expand Down Expand Up @@ -99,11 +103,7 @@ foto <- function(x,

# use assign
# define output matrix and global increment, i
if (window_size / 2 < 29) {
output <- matrix(0, cells + 1, window_size / 2)
} else {
output <- matrix(0, cells, 29)
}
output <- matrix(0, cells, harmonics)

# see rspectrum function above
i <- 0
Expand All @@ -117,6 +117,7 @@ foto <- function(x,
rspectrum(
x = x,
w = window_size,
hm = harmonics,
n = norm_spec,
h = high_pass,
env = env
Expand All @@ -138,6 +139,7 @@ foto <- function(x,
rspectrum(
x = x,
w = window_size,
hm = harmonics,
n = norm_spec,
env = env
)
Expand Down
23 changes: 14 additions & 9 deletions R/rspectrum.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#'
#' @param x a square matrix
#' @param w a moving window size
#' @param hm harmonics to consider as features
#' @param n normalize, bolean \code{TRUE} or \code{FALSE}
#' @param h high pass filter on the two first spectra values
#' set to 0, limits the influence of low frequency components
Expand All @@ -13,12 +14,16 @@
#' @return Returns a radial spectrum values for the image used
#' in order to classify texture using a PCA (or other) analysis.

rspectrum <- function(x,
w,
n = TRUE,
h = TRUE,
env,
...) {
rspectrum <- function(
x,
w,
hm,
n = TRUE,
h = TRUE,
env,
...
) {

# increment
i <- get("i", envir = env)

Expand Down Expand Up @@ -77,10 +82,10 @@ rspectrum <- function(x,
# in accordance to ploton et al. 2012
output <- get("output", envir = env)

if (w / 2 < 29) {
output[i, ] <- rspec[1:floor(w / 2)]
if (length(rspec) < hm) {
output[i, 1:length(rspec)] <- rspec
} else {
output[i, ] <- rspec[1:29]
output[i,] <- rspec[1:hm]
}

# assign values to matrix
Expand Down
3 changes: 3 additions & 0 deletions man/foto.Rd

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

4 changes: 3 additions & 1 deletion man/rspectrum.Rd

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

9 changes: 4 additions & 5 deletions vignettes/foto-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,18 @@ output_2 <- foto(r_2,
```



```{r figure_3, fig.width=7, fig.height=4}
par(mfrow = c(1,2))
plot(r_2, col = grey.colors(100), axes = FALSE, legend = FALSE, box = FALSE)
plot(r, col = grey.colors(100), axes = FALSE, legend = FALSE, box = FALSE)
par(mfrow = c(1,2))
plotRGB(output_batch[[1]], stretch = "hist")
plotRGB(output_batch[[2]], stretch = "hist")
plotRGB(output_batch[[1]], stretch = "hist", smooth = FALSE)
plotRGB(output_batch[[2]], stretch = "hist", smooth = FALSE)
par(mfrow = c(1,2))
plotRGB(output_2$rgb, stretch = "hist")
plotRGB(output$rgb, stretch = "hist")
plotRGB(output_2$rgb, stretch = "hist", smooth = FALSE)
plotRGB(output$rgb, stretch = "hist", smooth = FALSE)
```


Expand Down

0 comments on commit f6513b2

Please sign in to comment.