0

In R, I have a matrix that equals an image, where each cell is 0 if it is background and >0 if it is a ROI. Each ROI has its distinct number, so if it spans several matrix cells all these cells will have the same number. I want to generate ROI files from this that can be read by ImageJ.

example:

mx <- matrix(data=c(1,1,0,0,0,1,0,2,2,2,0,0,2,2,0,0,0,0,0,0,3,3,0,0,0), ncol=5, nrow=5)
# now some function to save the first, second and third ROI, each as a separate file

The EBImage package is what I use to get this data from my images, but it doesn't provide functions to write ROIs.

EDIT: The ROI names have to be exactly like they are in the matrix. One way to achieve this would be a labeled image (if someone knows how to generate this, please let me know) or even better to directly export the ROIs (more flexible).

NicolasH2
  • 774
  • 5
  • 20

1 Answers1

0

I don't know how to write an roiset in R that can be read by ImageJ, but your matrix is essentially a segmented image. So you could import your matrix as an image into ImageJ and then use a simple macro to create the selections.

First in R:

mx <- matrix(data=c(1,1,0,0,0,1,0,2,2,2,0,0,2,2,0,0,0,0,0,0,3,3,0,0,0), ncol=5, nrow=5)
# scale matrix for 8-bit export
mxn <- mx/255
EBImage::writeImage(mxn,"mxn.tif", type = "tiff", bits.per.sample = 8)

Now in ImageJ, load mxn.tif and then:

roiManager("reset");
getStatistics(area, mean, min, max);
for(i = 1; i <= max; i ++) {
    setThreshold(i - 0.1, i + 0.1);
    run("Create Selection");
    // do whatever you would like here
    getStatistics(area);
    // and/or...
    roiManager("Add");
    roiManager("select", i-1);
    roiManager("Rename", i);
}
quantixed
  • 287
  • 3
  • 12
  • yes, that is possible, but the ROIs have to be named exactly as they are named in the matrix. So I would need to have a "labeled" image, which I don't know how to get (so that would be one fix). But the most useful way would be to export the ROIs directly from R (which is more flexible in general). – NicolasH2 Jan 19 '22 at 07:52
  • I think this approach does what you want. I will edit my answer to make it more clear. – quantixed Jan 19 '22 at 17:06