Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sf rather than sp in polymask() #253

Merged
merged 3 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Imports:
farver,
plot3D,
progressr,
sp,
sf,
viridisLite
Suggests:
alphashape3d,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ importFrom(magick,image_rotate)
importFrom(plot3D,perspbox)
importFrom(progressr,progressor)
importFrom(progressr,with_progress)
importFrom(sp,point.in.polygon)
importFrom(stats,aggregate)
importFrom(stats,approx)
importFrom(stats,cor)
Expand Down
17 changes: 11 additions & 6 deletions R/adjacent.R
Original file line number Diff line number Diff line change
Expand Up @@ -643,26 +643,31 @@ adjacent_main <- function(classimg_i, xpts_i = NULL, xscale_i = NULL, bkgID_i =


# Internal function for masking color-classified image data that fall inside/outside a polygon
#' @importFrom sp point.in.polygon
polymask <- function(imagedat,
polygon,
alter_which = c("outside", "inside"),
replacement_value = NA) {
imglong <- data.frame(expand.grid(seq_len(ncol(imagedat)), seq_len(nrow(imagedat))), z = c(imagedat))
names(imglong) <- c("x", "y", "z")

inpoly <- point.in.polygon(imglong$x, imglong$y, polygon$x, polygon$y, mode.checked = FALSE) # todo: replace with base
pts <- do.call(
sf::st_sfc,
apply(imglong[, c("x", "y")], 1, sf::st_point, simplify = FALSE)
)
poly <- sf::st_polygon(list(as.matrix(polygon)))

# It's safe to extract just the first column since at the moment, we can only
# have a single polygon
inpoly <- sf::st_intersects(pts, poly, sparse = FALSE)[, 1]

maskmat <- matrix(data = inpoly, ncol(imagedat), nrow(imagedat))
maskmat <- apply(as.matrix(maskmat), 1, rev)
maskmat <- rev(t(apply(as.matrix(maskmat), 1, rev))) # mirror (DOUBLECHECK)
if (alter_which == "inside") {
imagedat[which(maskmat == 1)] <- replacement_value
imagedat[which(maskmat == 2)] <- replacement_value
imagedat[which(maskmat == 3)] <- replacement_value
imagedat[maskmat] <- replacement_value
}
if (alter_which == "outside") {
imagedat[which(maskmat == 0)] <- replacement_value
imagedat[!maskmat] <- replacement_value
}
imagedat
}
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-polymask.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set.seed(1234)
imgfake <- as.rimg(matrix(
sample(c(0, 1), 100, replace = TRUE), nrow = 10, ncol = 10)
)
polyfake <- data.frame(
x = c(3, 5, 7, 7, 3),
y = c(3, 3, 5, 7, 3)
)

test_that("polymask() outside and inside reproduce original image", {

o <- polymask(
imgfake,
polyfake,
"outside",
replacement_value = 0
)

i <- polymask(
imgfake,
polyfake,
"inside",
replacement_value = 0
)

expect_identical(o + i, imgfake)

})
Loading