Save binary (thresholded) image as text

Hello
I wish to save thresholded images as text for further treatment with R. I have noted that the pixel values given in the output file are 0 (background) and 255 (object). In other programs, pixels in binary images are coded as 0 (background) and 1 (object). I was wondering why ImageJ is not treating binary images that way and whether there is an option to assign 0 and 1 to the pixels prior to creating the text file.
It is not a big deal since I can change the values in R but it would be nice to skip that step.
Thank you very much for your attention.

tursiops

You can use the image math macro dialog for this (Process->Math->Macro):

1 Like

I agree with @Bio7, dividing each pixel by 255 is the quickest way to achieve this.

You can also use Process > Math > Divide… to do this simple division:


ImageJ 1.x (and most file formats for images) only supports a limited number of pixel types, and binary images are represented in 8-bit (0-255). In order to be displayed black and white without having to adjust the display, the min and max values of this type had to used. See the user guide for some background.


Note that ImageJ2 (or more precisely Imglib2) does support BitType images, and you can convert to such an image type using ImageJ Ops. However, in order to save to tiff or other formats, you’ll have to convert again from BitType to UnsignedByteType. The following Groovy script illustrates this:

// @ImgPlus img
// @OpService ops
// @DatasetIOService datasetIO
// @DatasetService dataset

binary = ops.run("convert.bit", img) // convert to BitType

uint = ops.run("convert.uint8", binary) // convert (back) to UnsignedByteType for saving
datasetIO.save(dataset.create(uint), "C:\\temp\\test.tif")
1 Like