forked from OpenDroneMap/FIELDimageR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfct_imgRAST.R
40 lines (40 loc) · 1.1 KB
/
fct_imgRAST.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#' imgRAST
#'
#' @title Converts an EBImage object to a terra object
#'
#' @description Converts an EBImage object to a terra object
#'
#' @param img image/object class \code{EBImage}
#'
#' @param ref SpatRaster with the crs that rast_obj should be assigned
#'
#' @importFrom terra rast
#'
#' @return \code{terra} object
#'
#' @export
imgRAST<-function(img,ref){
if (!requireNamespace("EBImage", quietly = TRUE)) {
stop("EBImage package is required for this function.")
}
if(class(img)=='Image') {
if(!is.na(dim(img)[3])){
b1<-t(imageData(img)[,,1]*255)
b2<-t(imageData(img)[,,2]*255)
b3<-t(imageData(img)[,,3]*255)
rgb<-array(c(b1,b2,b3),dim = c(dim(b1)[1],dim(b1)[2],dim(img)[3]))
} else if (is.na(dim(img)[3])){
b<-t(imageData(img)*255)
rgb<-as.array(b)
} else {
stop("Invalid number of channels in the image.")
}
rast_obj <- rast(rgb)
crs(rast_obj)<-crs(ref)
ext(rast_obj)<-ext(ref)
rast_obj[rast_obj==0] <- NA
return(rast_obj)
} else {
stop("Input is not an EBImage object.")
}
}