extract_dn: Extract digital numbers

View source: R/extract_dn.R

extract_dnR Documentation

Extract digital numbers

Description

Wrapper function around terra::extract().

Usage

extract_dn(r, sky_points, use_window = TRUE, fun = NULL)

Arguments

r

SpatRaster. The image from which the argument sky_points was obtained.

sky_points

The output of extract_sky_points() or an object of the same class and structure.

use_window

Logical vector of length one. If TRUE, a window of 3 \times 3 pixels will be used to extract the digital number from r.

fun

A function that takes a vector as input and returns a one-length numeric or logical vector as output (e.g. mean).

Value

An object of the class data.frame. It is the argument sky_points with an added column per each layer from r. The layer names are used to name the new columns. If a function is provided as the fun argument, the result will be summarized per column using the provided function, and the row and col information will be omitted. Moreover, if r is an RGB image, a color will be returned instead of a data.frame. The latter feature is useful for obtaining the sky_blue argument for enhance_caim().

Note

The point selection tool of ‘ImageJ’ software can be used to manually digitize points and create a CSV file from which to read coordinates (see Examples). After digitizing the points on the image, use the dropdown menu Analyze>Measure to open the Results window. To obtain the CSV file, use File>Save As...

See Also

Other Tool Functions: calc_oor_index(), calc_spherical_distance(), colorfulness(), correct_vignetting(), defuzzify(), display_caim(), extract_feature(), extract_rel_radiance(), extract_sky_points(), extract_sun_coord(), find_sky_pixels(), masking(), optim_dist_to_black(), optim_normalize(), percentage_of_clipped_highlights(), read_bin(), read_caim(), read_caim_raw(), read_ootb_sky_model(), sor_filter(), vicinity_filter(), write_bin(), write_caim(), write_ootb_sky_model()

Examples

## Not run: 
caim <- read_caim()
r <- caim$Blue
bin <- apply_thr(r, thr_isodata(r[]))
z <- zenith_image(ncol(caim), lens())
a <- azimuth_image(z)
g <- sky_grid_segmentation(z, a, 10)
sky_points <- extract_sky_points(r, bin, g)
sky_points <- extract_dn(caim, sky_points)
head(sky_points)

# See fit_cie_sky_model() for details on below file
path <- system.file("external/sky_points.csv",
                    package = "rcaiman")
sky_points <- read.csv(path)
sky_points <- sky_points[c("Y", "X")]
colnames(sky_points) <- c("row", "col")
head(sky_points)
plot(caim$Blue)
points(sky_points$col, nrow(caim) - sky_points$row, col = 2, pch = 10)

bin <- regional_thresholding(r, rings_segmentation(z, 15), "thr_isodata")
bin <- bin & select_sky_vault_region(z, 0, 85)
mx <- optim_normalize(caim, bin)
caim <- normalize_minmax(caim, mx = mx, force_range = TRUE)
m <- !is.na(z)

sky_blue <- extract_dn(caim, sky_points, fun = median)
as(sky_blue, "polarLAB")
ecaim <- enhance_caim(caim, m, sky_blue = sky_blue)
plot(ecaim)
.refine_sky_blue <- function(chroma) {
  ecaim <- enhance_caim(caim, m, polarLAB(50, chroma, 293))
  total_mu <- -extract_dn(ecaim, sky_points, fun = sum)
  if (is.na(total_mu)) total_mu <- 0
  total_mu
}
chroma <- seq(0, 1, 0.05) * 100
total_mu <- Map(.refine_sky_blue, chroma) %>% unlist()
plot(chroma, total_mu, type = "l")

opt_result <- optim(17,
                      .refine_sky_blue,
                      method = "L-BFGS-B", lower = 0, upper = 100)
opt_result$convergence
opt_result$par
ecaim <- enhance_caim(caim, m, polarLAB(50, 17, 293))
plot(ecaim)
apply_thr(ecaim, thr_isodata(ecaim[m])) %>% plot()

## End(Not run)

GastonMauroDiaz/rcaiman documentation built on April 14, 2025, 9:39 a.m.