Skip to content

Commit

Permalink
Fixed NAMESPACE Bug on fieldRotate() and documentation improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
DidierMurilloF committed Jun 25, 2021
1 parent e09b18b commit 8bf4564
Show file tree
Hide file tree
Showing 46 changed files with 1,593 additions and 1,180 deletions.
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
^.*\.Rproj$
^\.Rproj\.user$
^Dockerfile$
^\.dockerignore$
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.RData
.Rhistory
.git
.gitignore
manifest.json
rsconnect/
Rproj.user
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
54 changes: 42 additions & 12 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
Package: FIELDimageR
Type: Package
Title: FIELDimageR: A R Package to Analyze Orthomosaic Images from Agricultural Field Trials
Version: 0.2.9
Author: Filipe Inacio Matias
Maintainer: Filipe Inacio Matias <fmatias@wisc.edu>
Description: This package is a compilation of function to analyze pos-mosaicking images from research experimental fields, and allows to: reduce the mosaic dimension by cropping it in small sections; remove the soil effect; build vegetation indices; rotate the field and build the plot shape file; extract information for each plot; and evaluate stand count, plant height and canopy (greenness).
License: GPL-2
Imports: sp, raster, plyr, EBImage, doParallel, foreach, parallel, maptools
Sugested:rgdal, rgeos, igraph
Title: A Tool to Analyze Orthomosaic Images From Agricultural Field Trials
Version: 0.1.0
Authors@R:
c(person(given = "Filipe",
family = "Matias",
role = c("cre", "aut"),
email = "filipematias23@gmail.com"))
Description: A compilation of functions to analyze orthomosaic images
from research fields. To prepare the image it first allows to crop the image, remove soil
and weeds and rotate the image. The package also builds a plot shapefile in order to extract
information for each plot to evaluate different wavelengths, vegetation indices, stand count,
canopy percentage, and plant height.
License: GPL-3 + file LICENSE
biocViews:
Imports:
EBImage,
attempt,
config (>= 0.3.1),
DT,
fftwtools,
git2r,
glue,
maptools,
methods,
raster,
rgdal,
scales,
sp,
xml2,
foreach,
doParallel,
plyr,
processx,
grDevices
Encoding: UTF-8
LazyData: true
URL: https://github.com/OpenDroneMap/FIELDimageR
RoxygenNote: 6.1.1
biocViews: EBImage
RoxygenNote: 7.1.1
Author: Filipe Matias [cre, aut]
Maintainer: Filipe Matias <filipematias23@gmail.com>
Suggests:
spelling,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Language: en-US
23 changes: 0 additions & 23 deletions Dockerfile

This file was deleted.

19 changes: 19 additions & 0 deletions FIELDimageR.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
80 changes: 68 additions & 12 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,68 @@
export(fieldRotate,fieldCrop,fieldShape,fieldPolygon,fieldObject,fieldIndex,fieldMask,fieldMap,fieldInfo,fieldPlot,fieldCount,fieldArea,fieldDraw)
importFrom("sp", "Polygons","Polygon","SpatialPolygonsDataFrame","SpatialPolygons","spsample","SpatialPointsDataFrame","over", "proj4string")
importFrom("raster", "stack", "projection","plotRGB","res","projectRaster","crs","extent","atan2","crop","rasterToPolygons","mask","extract","clump","drawLine","drawPoly","xyFromCell")
importFrom("maptools", "elide")
importFrom("parallel", "makeCluster", "detectCores")
importFrom("foreach", "foreach", "%dopar%")
importFrom("doParallel", "registerDoParallel")
importFrom("graphics", "abline", "axis", "lines", "par", "plot","points","locator","legend")
importFrom("utils", "read.csv")
importFrom("grDevices", "rgb", "col2rgb")
importFrom("plyr", "join")
importFrom("EBImage","watershed","distmap")
# Generated by roxygen2: do not edit by hand

export(fieldArea)
export(fieldCount)
export(fieldCrop)
export(fieldDraw)
export(fieldIndex)
export(fieldInfo)
export(fieldMap)
export(fieldMask)
export(fieldObject)
export(fieldPlot)
export(fieldPolygon)
export(fieldRotate)
export(fieldShape)
importFrom(EBImage,distmap)
importFrom(EBImage,watershed)
importFrom(doParallel,registerDoParallel)
importFrom(foreach,"%dopar%")
importFrom(foreach,foreach)
importFrom(grDevices,col2rgb)
importFrom(grDevices,colorRamp)
importFrom(grDevices,grey)
importFrom(grDevices,rgb)
importFrom(graphics,abline)
importFrom(graphics,axis)
importFrom(graphics,legend)
importFrom(graphics,lines)
importFrom(graphics,locator)
importFrom(graphics,par)
importFrom(graphics,plot)
importFrom(graphics,points)
importFrom(maptools,elide)
importFrom(methods,as)
importFrom(methods,slot)
importFrom(parallel,detectCores)
importFrom(raster,as.data.frame)
importFrom(raster,as.list)
importFrom(raster,as.matrix)
importFrom(raster,atan2)
importFrom(raster,clump)
importFrom(raster,crop)
importFrom(raster,crs)
importFrom(raster,drawLine)
importFrom(raster,drawPoly)
importFrom(raster,extent)
importFrom(raster,extract)
importFrom(raster,mask)
importFrom(raster,plotRGB)
importFrom(raster,projectRaster)
importFrom(raster,projection)
importFrom(raster,raster)
importFrom(raster,rasterToPolygons)
importFrom(raster,res)
importFrom(raster,stack)
importFrom(raster,values)
importFrom(raster,xyFromCell)
importFrom(sp,Polygon)
importFrom(sp,Polygons)
importFrom(sp,SpatialPointsDataFrame)
importFrom(sp,SpatialPolygons)
importFrom(sp,SpatialPolygonsDataFrame)
importFrom(sp,bbox)
importFrom(sp,over)
importFrom(sp,proj4string)
importFrom(sp,spsample)
importFrom(stats,dist)
importFrom(utils,read.csv)
36 changes: 34 additions & 2 deletions R/fieldArea.R → R/fct_fieldArea.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
fieldArea<-function(mosaic, areaValue=0, fieldShape=NULL, n.core=NULL, plot=T, na.rm=FALSE){
#' fieldArea
#'
#' @title Percentage of object area
#'
#' @description It calculates the percentage of object area in the entire mosaic or per plot using the fieldShape file.
#'
#' @param mosaic object mask of class stack from the function \code{\link{fieldMask}}.
#' @param areaValue referent value of object area in the image.
#' @param fieldShape evaluate the object area percentage per plot using the fieldShape as reference. If fieldShape=NULL,
#' the object area percentage will be calculated directly for the entire original image.
#' @param n.core number of cores to use for multicore processing (Parallel).
#' @param plot if is TRUE the crop image and fieldShape will be plotted.
#' @param na.rm logical. Should missing values (including NaN) be used?.
#'
#' @importFrom raster stack extract crop
#' @importFrom parallel detectCores
#' @importFrom graphics par
#' @importFrom foreach %dopar% foreach
#' @importFrom doParallel registerDoParallel
#' @importFrom grDevices grey
#'
#'
#'
#' @return A list with two element
#' \itemize{
#' \item \code{areaPorcent} is the percentage of object area per plot represented in \code{DataFrame}.
#' \item \code{fieldShape} is the new \code{fieldShape} format \code{SpatialPolygonsDataFrame}.
#' }
#'
#'
#' @export
fieldArea <- function(mosaic, areaValue = 0, fieldShape = NULL, n.core = NULL, plot = TRUE, na.rm = FALSE) {
mosaic <- stack(mosaic)
num.band<-length(mosaic@layers)
print(paste(num.band," layer available", sep = ""))
Expand Down Expand Up @@ -28,7 +59,8 @@ fieldArea<-function(mosaic, areaValue=0, fieldShape=NULL, n.core=NULL, plot=T, n
if(n.core>detectCores()){stop(paste(" 'n.core' must be less than ",detectCores(),sep = ""))}
cl <- parallel::makeCluster(n.core, output = "", setup_strategy = "sequential")
registerDoParallel(cl)
extM <- foreach(i = 1:length(fieldShape), .packages = c("raster")) %dopar%
i = 1:length(fieldShape)
extM <- foreach(i, .packages = c("raster")) %dopar%
{
single <- fieldShape[i, ]
CropPlot <- crop(x = mosaic, y = single)
Expand Down
41 changes: 39 additions & 2 deletions R/fieldCount.R → R/fct_fieldCount.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
fieldCount<-function(mosaic, fieldShape, value=0, minSize=0.01, n.core=NULL, pch=16, cex=0.7, col="red", na.rm=FALSE){
#' fieldCount
#'
#' @title Calculating number of objects per plot
#'
#' @description The mask from function \code{\link{fieldMask}} is used to identify the number of objects per plot..
#'
#' @param mosaic object mask of class stack from the function \code{\link{fieldMask}}.
#' @param fieldShape plot shape file.
#' @param value referent value to vegetation pixels in the mask. If "HUE" was used on
#' \code{\link{fieldMask}} the value=0.
#' @param minSize used to set the minimum size percentage of plant canopy (to remove weeds and more).
#' @param n.core number of cores to use for multicore processing (Parallel).
#' @param pch point symbol, please check \code{help("points")}.
#' @param cex character (or symbol) expansion: a numerical vector, please check \code{help("points")}.
#' @param col color code or name, please check \code{help("points")}.
#' @param na.rm logical. Should missing values (including NaN) be removed?.
#'
#' @importFrom raster projection extract xyFromCell
#' @importFrom EBImage distmap watershed
#'
#'
#' @return A list with five element
#' \itemize{
#' \item \code{fieldCount} is the number of objects per plot represented in DataFrame.
#' \item \code{fieldShape} is the new shapeFile with stand count.
#' \item \code{mosaic} is the Watershed layer.
#' \item \code{objectSel} is the objects area per plot.
#' \item \code{objectReject} is the objects position in the image.
#' }
#'
#'
#' @export
fieldCount <- function(mosaic, fieldShape, value = 0, minSize = 0.01, n.core = NULL, pch = 16,
cex = 0.7, col = "red", na.rm = FALSE) {
if(!is.na(projection(fieldShape))&is.na(projection(mosaic))){
if(projection(fieldShape)!=projection(mosaic)){stop("fieldShape and mosaic must have the same projection CRS, strongly suggested to use fieldRotate() for both files.")}}
mosaic <- stack(mosaic)
Expand Down Expand Up @@ -81,6 +114,10 @@ fieldCount<-function(mosaic, fieldShape, value=0, minSize=0.01, n.core=NULL, pch
print(paste("Number of objects: ", sum(field), sep = ""))
graphics::points(do.call(rbind,objectSel)[,c(2,3)], pch=pch, cex=cex, col=col)
sp::plot(fieldShape, add=T)
Out <- list(fieldCount=field, fieldShape=fieldShape, mosaic=mosaic$watershed, objectSel=objectSel, objectReject=objectReject)
Out <- list(fieldCount=field,
fieldShape=fieldShape,
mosaic=mosaic$watershed,
objectSel=objectSel,
objectReject=objectReject)
return(Out)
}
74 changes: 74 additions & 0 deletions R/fct_fieldCrop.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#' fieldCrop
#'
#' @title Selecting experimental field from original image
#'
#' @description It calculates the percentage of object area in the entire mosaic or per plot using the fieldShape file.
#'
#' @param mosaic object mask of class stack from the function \code{\link{fieldMask}}.
#' @param fieldShape crop the image using the fieldShape as reference. If fieldShape=NULL, four points should be selected
#' directly on the original image to determine the experimental field.
#' @param nPoint number of points necessary to select field boundaries or area to remove (4 >= nPoint <= 50).
#' @param remove if TRUE the selected area will be removed from the image.
#' @param plot if \code{TRUE} (by default) plots the original and cropped image.
#' @param type character indicating the type of plotting, please check help("lines").
#' @param lty line types, please check help("lines").
#' @param lwd line width, please check help("lines").
#' @param fast.plot if TRUE only the grey scale image will be plotted as reference (faster approach).
#' if TRUE only the grey scale image will be plotted as reference (faster approach).
#'
#' @importFrom raster plotRGB mask
#' @importFrom graphics locator lines
#' @importFrom sp Polygons Polygon SpatialPolygonsDataFrame SpatialPolygons
#'
#'
#' @return A image format stack.
#'
#' @export
fieldCrop <- function(mosaic, fieldShape = NULL, nPoint = 4, plot = TRUE, remove = FALSE, type = "l",
lty = 2, lwd = 3, fast.plot = FALSE) {
mosaic <- stack(mosaic)
num.band <- length(mosaic@layers)
print(paste(num.band," layers available", sep = ""))
if(nPoint<4|nPoint>50){stop("nPoint must be >= 4 and <= 50")}
par(mfrow=c(1,2))
if(is.null(fieldShape)|plot){
if(fast.plot){
raster::plot(mosaic[[1]], col=grey(1:100/100), axes=FALSE, box=FALSE, legend=FALSE)}
if(!fast.plot){
if(num.band>2){plotRGB(RGB.rescale(mosaic,num.band=3), r = 1, g = 2, b = 3)}
if(num.band<3){raster::plot(mosaic, axes=FALSE, box=FALSE)}}
}
if(!is.null(fieldShape)){
if(raster::projection(fieldShape) != raster::projection(mosaic)){
stop("fieldShape and mosaic must have the same raster::projection CRS. Use fieldRotate() for both files.")
}
r <- crop(x = mosaic, y = fieldShape)
}
c1 <- NULL
if(is.null(fieldShape)) {
print(paste("Select ",nPoint," points at the corners of field of interest in the plots space.",sep = ""))
for(i in 1:nPoint){
c1.1<-locator(type="p",n = 1, col="red",pch=19)
c1<-rbind(c1,c(c1.1$x,c1.1$y))
}
c1<-rbind(c1,c1[1,])
colnames(c1)<-c("x","y")
lines(c1, col= "red", type=type, lty=lty, lwd=lwd)
}
if(!is.null(c1)) {
p1 <- Polygons(list(Polygon(c1)), "x")
f1 <- SpatialPolygonsDataFrame( SpatialPolygons(list(p1)), data.frame( z=1, row.names=c("x") ) )
raster::projection(f1) <- raster::projection(mosaic)
if(!remove){r <- crop(x = mosaic, y = f1)}
if(remove){r <- mask(x = mosaic, mask = f1, inverse = remove)}
}
r <- stack(r)
if(plot){
if(fast.plot) {
raster::plot(r[[1]], col=grey(1:100/100), axes=FALSE, box=FALSE, legend=FALSE)}
if(!fast.plot){
if(num.band>2){plotRGB(RGB.rescale(r,num.band=3), r = 1, g = 2, b = 3)}
if(num.band<3){raster::plot(r, axes=FALSE, box=FALSE)}}}
par(mfrow=c(1,1))
return(r)
}
Loading

0 comments on commit 8bf4564

Please sign in to comment.