I am trying to create eroded and dilated versions of a raster object with your EBImage package in R.
Dilation works fine with the following code:
library(raster) par(mfrow = c(1,2)) r <- raster("C:/Program Files/R/R-3.2.3/doc/html/logo.jpg") plot(r, main = "original image")
library(EBImage) kEBIMAGE <- makeBrush(3, shape = "box") rAr <- as.array(r, transpose = TRUE) rIm <- as.Image(rAr[]) rErIm <- dilate(rIm, kern = kEBIMAGE) rErAr <- as.array(rErIm) rRas <- setValues(r, as.vector(aperm(rErAr))) unique(rRas) plot(rRas, main = "dilated image")
However, erode() only returns 0 and 1 for the cells of the array/raster:
library(raster) par(mfrow = c(1,2)) r <- raster("C:/Program Files/R/R-3.2.3/doc/html/logo.jpg") plot(r, main = "original image") library(EBImage) kEBIMAGE <- makeBrush(3, shape = "box") rAr <- as.array(r, transpose = TRUE) rIm <- as.Image(rAr[]) rErIm <- erode(rIm, kern = kEBIMAGE) rErAr <- as.array(rErIm) rRas <- setValues(r, as.vector(aperm(rErAr))) unique(rRas) plot(rRas, main = "dilated image")
> sessionInfo() R version 3.3.0 (2016-05-03) Platform: x86_64-w64-mingw32/x64 (64-bit)
Do you know this kind of behaviour? How can I fix it, or is it a bug?
Thanks for your help. This helped. Since I have to deal with geotagged tiff files, i have to convert the data back to a RasterLayer object. To adjust the Data to the 16bit (2byte) structure I multiply the array by 2**16.
In that case you probably want rather multiply by
2^16-1
in order to map from 0:1 to 0:65535, which I believe is your desired range.