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

update with method for Spatrasters #26

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions image.ContourDetector/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Authors@R: c(
person("BNOSAC", role = "cph", comment = "R wrapper"),
person("Rafael Grompone von Gioi", role = c("ctb", "cph"), email = "[email protected]", comment = "src/smooth_contours"),
person("Gregory Randall", role = c("ctb", "cph"), email = "[email protected]", comment = "src/smooth_contours"),
person("Niccolò Marchi", role = "ctb", email = "niccolo.marchi@unipd.it"))
person("Niccolò Marchi", role = "ctb", email = "sciurusurbanus@hotmail.it"))
Encoding: UTF-8
License: AGPL-3
Version: 0.1.1
Expand All @@ -19,5 +19,7 @@ LinkingTo: Rcpp
Suggests:
pixmap,
magick,
raster
raster,
sf,
terra
RoxygenNote: 7.1.1
1 change: 1 addition & 0 deletions image.ContourDetector/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(image_contour_detector,RasterLayer)
S3method(image_contour_detector,SpatRaster)
S3method(image_contour_detector,matrix)
S3method(plot,cld)
S3method(print,cld)
Expand Down
137 changes: 111 additions & 26 deletions image.ContourDetector/R/contour_detector.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' an efficient algorithm is derived producing sub-pixel contours.
#' @param x a matrix of image pixel values in the 0-255 range.
#' @param Q numeric value with the pixel quantization step
#' @param ... further arguments, not used yet
#' @param ... further arguments passed on to \code{image_contour_detector.matrix}, \code{\link{image_contour_detector.RasterLayer}} or \code{\link{image_contour_detector.SpatRaster}}.
#' @return an object of class cld which is a list with the following elements
#' \itemize{
#' \item{curves: }{The number of contour lines found}
Expand Down Expand Up @@ -61,26 +61,6 @@
#' \}
#' # End of main if statement running only if the required packages are installed
#' }
#'
#' ##
#' ## working with a RasterLayer
#' ##
#' \dontshow{
#' if(require(raster))
#' \{
#' }
#' \donttest{
#' library(raster)
#' x <- raster(system.file("extdata", "landscape.tif", package="image.ContourDetector"))
#'
#' contourlines <- image_contour_detector(x)
#' image(x)
#' plot(contourlines, add = TRUE, col = "blue", lwd = 10)
#' }
#' \dontshow{
#' \}
#' # End of main if statement running only if the required packages are installed
#' }
image_contour_detector <- function(x, Q=2.0, ...){
UseMethod("image_contour_detector")
}
Expand All @@ -103,25 +83,130 @@ image_contour_detector.matrix <- function(x, Q=2.0, ...){
return(contourlines)
}

#' @title Unsupervised Smooth Contour Lines Detection for RasterLayer objects
#' @description Unsupervised Smooth Contour Detection
#' @param x a RasterLayer object
#' @param Q numeric value with the pixel quantization step
#' @param as_sf Boolean. Set to TRUE to export lines as sf spatial objects
#' @param ... further arguments passed on to \code{image_contour_detector.matrix}
#' @return
#' In case \code{as_sf} is \code{FALSE}: an object of class cld which as described in \code{\link{image_contour_detector}}\cr
#' In case \code{as_sf} is \code{TRUE}: an object of class sf containing the detected lines, a curve ID and its length (in the same units as the SpatRaster's CRS)
#' @seealso \code{\link{image_contour_detector}}
#' @export
image_contour_detector.RasterLayer <- function(x, Q=2.0, ...){
image_contour_detector.RasterLayer <- function(x, Q=2.0, as_sf=FALSE, ...){
requireNamespace("raster")
minX = raster::extent(x)[1]
minY = raster::extent(x)[3]
resol = raster::res(x)[1]
x = raster::as.matrix(x)
xmat = raster::as.matrix(x)

if( anyNA(x) ){
x[is.na(x)] = 0
if( anyNA(xmat) ){
x[is.na(xmat)] = 0
warning("NA values found and set to 0") }

contourlines = image_contour_detector.matrix(x, Q=Q)
contourlines = image_contour_detector.matrix( xmat, Q=Q )

contourlines$data$x = contourlines$data$x * resol + minX
contourlines$data$y = contourlines$data$y * resol + minY

# export object as sf
if(isTRUE(as_sf)){
requireNamespace("sf")

contourlines <- contourlines$data
contourlines = sf::st_as_sf( contourlines, coords = c("x", "y"), crs = sf::st_crs(x) )

out = list()

for( i in unique(contourlines$curve) ){
ss = subset(contourlines, curve == i )
ss = sf::st_combine(ss)
out[[length(out)+1]] = sf::st_cast( sf::st_sf(ss), "LINESTRING")
}

contourlines = do.call(rbind,out)
contourlines$curve_ID = 1:nrow(contourlines)
contourlines$length = round( sf::st_length(contourlines), 3)
}

return(contourlines)
}

#' @title Unsupervised Smooth Contour Lines Detection for SpatRaster objects
#' @param x a SpatRaster object
#' @param Q numeric value with the pixel quantization step
#' @param as_sf Boolean. Set to TRUE to export lines as sf spatial objects
#' @param ... further arguments passed on to \code{image_contour_detector.matrix}
#' @return
#' In case \code{as_sf} is \code{FALSE}: an object of class cld which as described in \code{\link{image_contour_detector}}\cr
#' In case \code{as_sf} is \code{TRUE}: an object of class sf containing the detected lines, a curve ID and its length (in the same units as the SpatRaster's CRS)
#' @seealso \code{\link{image_contour_detector}}
#' @export
#' @examples
#' \dontshow{
#' if(require(terra) && require(sf))
#' \{
#' }
#' \donttest{
#' library(terra)
#' x <- rast(system.file("extdata", "landscape.tif", package="image.ContourDetector"))
#'
#' contourlines <- image_contour_detector(x)
#' plot(x)
#' plot(contourlines, add = TRUE, col = "blue", lwd = 10)
#'
#' contourlines <- image_contour_detector(x, as_sf = TRUE)
#' }
#' \dontshow{
#' \}
#' # End of main if statement running only if the required packages are installed
#' }
image_contour_detector.SpatRaster <- function(x, Q=2.0, as_sf=FALSE, ...){
requireNamespace("terra")
minX = terra::ext(x)[1]
minY = terra::ext(x)[3]
resol = terra::res(x)[1]
xmat = terra::as.matrix(x, wide=TRUE)

if(anyNA(xmat)){
xmat[is.na(xmat)] = 0
warning("NA values found and set to 0")
}

contourlines = image_contour_detector.matrix( xmat, Q=Q )

contourlines$data$x = contourlines$data$x * resol + minX
contourlines$data$y = contourlines$data$y * resol + minY

# export object as sf
if(isTRUE(as_sf)){
requireNamespace("sf")

# contourlines <- contourlines$data %>%
# sf::st_as_sf(coords = c("x", "y"), crs = sf::st_crs(x) ) %>%
# dplyr::group_by( curve ) %>%
# dplyr::summarize(do_union=FALSE) %>%
# sf::st_cast("LINESTRING")

contourlines <- contourlines$data
contourlines = sf::st_as_sf( contourlines, coords = c("x", "y"), crs = sf::st_crs(x) )

out = list()

for( i in unique(contourlines$curve) ){
ss = subset(contourlines, curve == i )
ss = sf::st_combine(ss)
out[[length(out)+1]] = sf::st_cast( sf::st_sf(ss), "LINESTRING")
}

contourlines = do.call(rbind,out)
contourlines$curve_ID = 1:nrow(contourlines)
contourlines$length = round( sf::st_length(contourlines), 3)
}
return(contourlines)
}


#' @export
print.cld <- function(x, ...){
Expand Down
26 changes: 26 additions & 0 deletions image.ContourDetector/man/image_contour_detector.RasterLayer.Rd

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

22 changes: 1 addition & 21 deletions image.ContourDetector/man/image_contour_detector.Rd

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

47 changes: 47 additions & 0 deletions image.ContourDetector/man/image_contour_detector.SpatRaster.Rd

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

8 changes: 6 additions & 2 deletions image.LineSegmentDetector/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ Maintainer: Jan Wijffels <[email protected]>
Authors@R: c(
person("Jan", "Wijffels", role = c("aut", "cre", "cph"), email = "[email protected]", comment = "R wrapper"),
person("BNOSAC", role = "cph", comment = "R wrapper"),
person("Rafael Grompone von Gioi", role = c("ctb", "cph"), email = "[email protected]", comment = "src/lsd"))
person("Rafael Grompone von Gioi", role = c("ctb", "cph"), email = "[email protected]", comment = "src/lsd"),
person("Niccolò Marchi", role = "ctb", email = "[email protected]"))
Encoding: UTF-8
License: AGPL-3
Version: 0.1.0
URL: https://github.com/bnosac/image
Imports: Rcpp (>= 0.12.8), sp
LinkingTo: Rcpp
Suggests:
pixmap,
magick
magick,
sf,
terra
RoxygenNote: 7.1.0
Loading