[R-sig-Geo] as.raster() does not work for RImageJ objects

Robert J. Hijmans r.hijmans at gmail.com
Mon Jan 20 22:15:09 CET 2014


Agus,

as.raster returns a 'raster' object as defined in the graphics package
(or thereabouts). I assume that what you want is to coerce to a
RasterLayer (from the raster package). That is typically done with

raster(image)
# or
as(image, 'RasterLayer')

Robert



On Fri, Jan 17, 2014 at 5:45 AM, Agustin Lobo <alobolistas at gmail.com> wrote:
> ok, thanks. I did not know that there was another class raster in
> another package.
>
> This means that we cannot convert from RImageJ objects to
> raster::raster objects.
> But we can use RImageJ to process, save to disk and read back as raster:raster:
>
> logo <- system.file( "images", "R.jpg", package = "RImageJ" )
> image = IJ$openImage( logo )
> image$show()
> IJ$run( "8-bit" )
> IJ$run( "Invert" )
> IJ$save( "bw-google.gif" )
> image$close()
>
> r4 <- raster("bw-google.gif")
> plot(r4)
> summary(r4)
> extent(r4)
>
> Hopefully I'll be able to use RImageJ for applying smoothing filters
> and other image processing
> methods that are too slow within R, without having to leave the R
> scripting environment.
>
> Agus
>
> On Fri, Jan 17, 2014 at 9:26 AM, Michael Sumner <mdsumner at gmail.com> wrote:
>> You are see a clash of the grDevices::as.raster with the raster
>> package. grDevices is loaded by default, and has a (S3) method for
>> whatever your image is (a matrix?).
>>
>> It seems like the S4 raster::as.raster should allow these to "pass
>> through" to the underlying S3 method.
>>
>> I can't look any closer right now
>>
>>
>>
>>
>>
>> On Fri, Jan 17, 2014 at 7:09 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
>>> Actually, in OSX, there is no error message at using as.raster(), but
>>> it does not work either:
>>>
>>> r <- as.raster(image)
>>> class(r)
>>>
>>> [1] "raster"
>>>> str(r)
>>> 'raster' chr [1:76, 1:100] "#ffffff" "#ffffff" "#ffffff" "#ffffff"
>>> "#ffffff" "#ffffff" "#ffffff" ...
>>>
>>> which is not the structure a raster object should have.
>>>
>>> Then:
>>>
>>> r <- as.raster.jobjRef(image)
>>> Error: could not find function "as.raster.jobjRef"
>>>
>>> while ?as.raster.jobjRef shows the help page
>>> the function as.raster.jobjRef" is not in the workspace (and this is
>>> also the case in linux):
>>>> search()
>>>  [1] ".GlobalEnv"        "package:RImageJ"   "package:rJava"
>>> "package:raster"    "package:sp"
>>>  [6] "tools:rstudio"     "package:stats"     "package:graphics"
>>> "package:grDevices" "package:utils"
>>> [11] "package:datasets"  "package:methods"   "Autoloads"
>>> "package:base"
>>>> ls(2)
>>> [1] "IJ"              "IJWindowManager" "ImageJ"
>>>
>>>
>>> R version 3.0.2 (2013-09-25)
>>> Platform: x86_64-apple-darwin10.8.0 (64-bit)
>>>
>>> locale:
>>>   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>>
>>> attached base packages:
>>>   [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>> other attached packages:
>>>   [1] RImageJ_0.2-146 rJava_0.9-6     raster_2.1-66   sp_1.0-13
>>>
>>> loaded via a namespace (and not attached):
>>>   [1] grid_3.0.2      lattice_0.20-23 tools_3.0.2
>>>
>>>
>>> Agus
>>>
>>> On Thu, Jan 16, 2014 at 4:09 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
>>>> Hi!
>>>>
>>>> While the following works fine:
>>>> install.packages("/home/alobo/Downloads/RImageJ_0.2-146.tar.gz", repos
>>>> = NULL, type="source")
>>>> require(RImageJ)
>>>> require(raster)
>>>> logo <- system.file( "images", "R.jpg", package = "RImageJ" )
>>>> image = IJ$openImage( logo )
>>>> class(image)
>>>> plot(c(100, 250), c(300, 450), type = "n", xlab="", ylab="")
>>>> rasterImage(image, 100, 300, 150, 350, interpolate=FALSE)
>>>>
>>>> (note that the example in
>>>> http://romainfrancois.blog.free.fr/index.php?category/R-package/RImageJ
>>>> is currently wrong, it must be rasterImage() and not raster()
>>>>
>>>> But as.raster() (the most important for me) does not work:
>>>>
>>>>> class(image)
>>>> [1] "jobjRef"
>>>> attr(,"package")
>>>> [1] "rJava"
>>>>> r <- as.raster(image)
>>>> Error in as.raster.jobjRef(image) : attempt to apply non-function
>>>>
>>>> see ?as.raster.jobjRef
>>>>
>>>> Any fix?
>>>>
>>>> Thanks
>>>> Agus
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>>
>> --
>> Michael Sumner
>> Hobart, Australia
>> e-mail: mdsumner at gmail.com
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list