R/calculate_sobel.r

#' Estimates the gcc value for a region of interest
#' automatically generated by any given algorithm
#'
#' @param img: RGB image to process (filename or 3-layer RGB stack or brick)
#' @param plot: plot resulting image with all available information
#' @keywords sobel index, edge detection, feature
#' @export
#' @examples
#' # no examples yet

calculate_sobel = function(img,
                           roi = NULL){
  
  # verify data formats if not transform
  # to the correct data format
  if (class(img) == "character"){
    img = raster::brick(img)
  }
  
  # if no roi is specified calcualte the roi
  if (is.null(roi)){
    roi = estimate_roi(img)
  }
  
  # calculate various glcm indices on the Green channel
  if (nlayers(img) == 3){
    img = img[[2]] #/ sum(img)
  } else {
    img = img
  }
  
  # select the ROI from the original image
  img_region = mask(img, roi$roi)
  
  # calculate edges along the columns (top to bottom of image)
  # wind lodging will show up as more pronounced edges in this
  # direction due to it's horizontal component
  sobel_data = sobel(img_region, method = "y")
  
  # only evaluate edge coming from the top
  sobel_data[sobel_data <= 0] = NA
  
  # calculate stats across region of interest
  img_region_stats = cellStats(sobel_data, mean, na.rm = TRUE)
  
  # return values as a structure list
  return(list("roi" = roi$roi,
              "horizon" = roi$horizon,
              "sobel" = img_region_stats))
}
khufkens/cropmonitor documentation built on May 31, 2019, 8:29 a.m.