Converting ImageJ ROI to R spatstat data doesn't work - r

I tried to use the RImageJROI package from David C Sterratt to transfer multiple ROIs into R and convert them to Spatstat.
This is a minimal example of my R-code
library(RImageJROI)
ROI = read.ijzip("path/ROI.zip")
spat.ROI = ij2spatstat(ROI)
Reading the zip-file works well but when I run the converting command I get the following error:
Error in conv.fun(k, window = window, unitname = unitname, scale =
scale, : object 'out' not found
Attached there is a file for creating a ROI.zip -file which causes the error after processing it in ImageJ with:
run("Analyze Particles...", "add");
roiManager("Save", "Path\\Roi.zip");
Is there a way to get RImageJROI working, am I missing something obvious, or does anyone know another solution for my project?

You can use Bio7 for that with has special methods to transfer different ImageJ ROI's and image data to R.
Some methods where especially designed for spatstat after a great spatstat tutorial on the R conference in 2015.
Here a link to some ImageJ spatstat notes and simple scripts:
http://bio7.org/?p=2618
https://github.com/Bio7/Bio7_Workshop
Here some video tutorials to transfer ROI data:
3D Point Pattern:
https://youtu.be/DmfSASgJa_g
Line Segments:
https://youtu.be/EPan7kibYpo
Polygons:
https://youtu.be/bS_2ejOt7Tg
Point Patterns:
https://youtu.be/7t5V2o8jFJw
Particle Measurments:
https://youtu.be/7t5V2o8jFJw
Georeferenced Polygons (which can be converted to spatstat objects):
https://youtu.be/P2NflfBB2Tg

I got the same error message. I found this to work after looking inside the ij2spatstat function.
library(RImageJROI)
library(spatstat)
roi <- read.ijroi("RoiSet/0071-0081.roi") # path to single ROI
poly <- list(x = roi$coords[,2], y = roi$coords[,1])
out <- owin(poly = poly)
plot(out)
You can put this inside a loop to transform every ROI in a directory.

Related

From POV-Ray to rgl

I've followed this tutorial to export a POV-Ray graphic to a STL file, through Meshlab. I've also tried the export to the OBJ format. Everything seems to work fine from the creation of the POV-Ray graphic to the exporting in Meshlab.
But then I've tried to render the graphic in R with the functions readSTL and readOBJ of the rgl package, and the problem is here.
In fact, the exported STL file is empty:
solid STL generated by MeshLab
endsolid vcg
So, of course, rgl::readSTL renders nothing in R.
The OBJ file is not empty, but it contains no faces (only vertices and vertex normals):
####
#
# OBJ File Generated by Meshlab
#
####
# Object blob.obj
#
# Vertices: 8437
# Faces: 0
#
####
vn -0.900372 -0.267658 -0.343060
v -4.000525 2.600000 -0.833225
......
After running rgl::readOBJ in R the rendering is just a white scene, there's nothing. Even if there's no face, we could expect to get some points.
Maybe I'm mistaken during one step of the procedure. Do you have any idea about:
how to export to a non-empty STL file in Meshlab ?
how to get the points with readOBJ in R ?
how to get the faces in the OBJ file when exporting from Meshlab ?
do you know another way to go from POV-Ray to rgl, if possible preserving the colors ?
Update
I've found a way to get the faces in the OBJ file: instead of doing Screened Poisson Surface Reconstruction in Meshlab, as said in the tuto, I do Surface Reconstruction: Ball Pivoting.
But then rgl::readOBJ generates this error:
Error in order(vlinks[[i]][, 2]) : argument 1 is not a vector
The same procedure allows to export a non-empty STL file. But then rgl::readSTL generates this error:
Error in matrix(NA, 3 * n, 3) : invalid 'nrow' value (too large or NA)
In addition: Warning message:
In matrix(NA, 3 * n, 3) : NAs introduced by coercion to integer range
Ok, I've found.
Follow the linked tuto to create the file blob.asc in POV-Ray
In Meshlab, open this file, and do Surface Reconstruction: Ball Pivoting in the menu Filters -> Remeshing, Simplification and Reconstruction. Perhaps one previously needs to do Screened Poisson Surface Reconstruction before, I don't know.
Export the file as STL. Check "Binary Encoding" (a default option), because rgl::readSTL reads binary STL files only, not ASCII files.
In R, now you can do rgl::readSTL. And this works:

ClusterLongData kml package export to scv

I am clustering time series in R using package KmL. I have read both manual and paper how to use this package, but Im not very clear how to export the results (data frame, where each trajectories are assign to some clusters e.g.
trajectory (i), time1, time2, time3, clustername)
I have read several answers here Output from 'choice' in R's kml
but if I do the same (run choice(myCld, typeGraph= "bmp")) my R says:
~ Choice : menu ~ 'Arrow' : change partition 'Space' : select/unselect a partition ... etc. e : change the display (both)
~ 'Return' when its done ~
And only thing that is saved to my library is myCld.Rdata and it runs for very long time without any more results. (my dataset: N trajectories, with time= 1:53) I want to have csv. files as manual suggest (objectName-Cx-y-Clusters.csv)
I am also not very clear WHERE should I PRESS on "Return" or "Arrow"? There is no option to press on anything in my Rstudio workplace.
I am really a beginner with R so any help would be appreciated. Thanks!
I am not sure that kml is compatible with Rstudio. The older version of Rstudio did not handle instruction like getGraphicsEvent that was used by choice.

Using SRTM tif file in R

I'm trying to import a SRTM dataset into R. I've downloaded the data in a tif file however am having trouble reading it in "R".
Ive tried using the following code:
t = readTIFF("srtm_56_06/srtm_56_06.tif", as.is=TRUE)
load('srtm_56_06/srtm_56_06.tif')
read_file<-as.matrix(raster("srtm_56_06/srtm_56_06.tif")
However I am still getting error messages:
load('srtm_56_06/srtm_56_06.tif')
# Error: bad restore file magic number (file may be corrupted) -- no data loaded
# In addition: Warning message:
# file ‘srtm_56_06.tif’ has magic number 'II*'
# Use of save versions prior to 2 is deprecated
library(raster)
t = readTIFF("srtm_56_06/srtm_56_06.tif", as.is=TRUE)
# Error: could not find function "readTIFF"
read_file<-as.matrix(raster("srtm_56_06/srtm_56_06.tif") + min(read_file)
# Error: unexpected symbol in:
# "read_file<-as.matrix(raster("srtm_56_06/srtm_56_06.tif")
# min"
Can anyone help me with the commands to import this data. I'm a novice at "R" and a little lost.
Just read it with raster, but note you depend on rgdal being installed as well to read a .tif.
library(raster)
library(rgdal)
r <- raster("srtm_56_06/srtm_56_06.tif")
If that works, try
plot(r)
r
If it's really a "TIFF" then that should be fine, if it's really a GeoTIFF then you'll have a sensible map as well. (If it's something else that GDAL can read you might get a good result anyway, remember the extension of a file is not a reliable indicator of its contents).
The SRTM clue suggests that this is a single band DEM file from the tiled global SRTM data set. If it's somehow a "multi-band image" then you could read that with brick and plot with plotRGB (but I really doubt that is the case here). Note that there is a native binary format for SRTM that raster/rgdal could read as well but either they distributed .tif as well or someone else converted it.
There are a number of misconceptions in your code:
load is for a particular file type created from R (not these .tifs)
readTIFF is not in package raster
read_file would be a sensible matrix, if you have rgdal installed (which raster must use to load a .tif), but why throw away the spatial metadata?

R: Error thrown while using RGDAL and RASTER packages

To whom this may concern:
Here is the source code:
GRA_D1<- raster(files[[1]])
//Sets up an empty output raster:
GRA_D1<- writeStart(GRA_D1,filename='GRA_D1.tif', format='GTiff', overwrite=TRUE)
//Write to the raster, for loop:
for(i in 1:dim(GRA_D1)[1]){
//Extract raster values at rows
d.Frame<- matrix(NA,ncol=2,nrow=dim(GRA_D1)[2])
d.Frame[,1]<- getValues(r1[[1]],i)
d.Frame[,2]<- getValues(r1[[2]],i)
w.Frame<- as.data.frame(d.Frame)
names(w.Frame)<- c("D1_pred_disAg","D1_pred_RK")
//Apply the predictive model:
m.pred<-predict(mod.1, w.Frame)
//Write the predictions to the empty TIFF raster
GRA_D1<-writeValues(GRA_D1,m.pred,i)
print(i)}
//Finish writing to the raster
GRA_D1<- writeStop(GRA_D1)
I am attempting to write output to an empty TIFF raster, but I keep receiving the following error message:
#Error in .local(.Object, ...) :
`general_file_path\GRA_D1.tif' does not exist in the file system,
and is not recognised as a supported dataset name.
I wonder if this is related to misusing functions in either the RGDAL or RASTER package.
Could someone please assist me?
Thanks in advance for your generosity.
Cheers,
AD
Super simple fix. Cannot believe that it is this simple and that it took me this long, but here is the answer:
"rgdal" and/or "GTiff" files don't like the use of underscores in their dataset names.
When running the code with "GRAD1.tif" (instead of "GRA_D1.tif"), all works well.
You really should not be doing any of this, I think, as you could do:
p <- predict(r1, mod.1, filename='GRA_D1.tif')
(and that filename works just fine)

Read source code of an R function including C

Suppose I'm curious about the workings of the R function, say HoltWinters. I typed HoltWinters and it shows me R source for the function. On inspection, the source shows the function is a wrapper around a second function:
final.fit <- hw(alpha, beta, gamma)
Presumably, the serious work happens in the function hw. However, I can't find this function anywhere to read its source
> hw
Error: object 'hw' not found
How can I read the source?
Edit: Ok, so now I've read hw , I see it's a wrapper around C_HoltWinters. How can I read that?
As you successfully found, there are lines
hw <- function(alpha, beta, gamma)
.C(C_HoltWinters,
....
in the source of HoltWinters function. Which means that we need to look at C files: you can find all the source code of R here, or just go straight here.

Resources