Create Mask source file

imagej
segmentation

#1

Hi to all,

I am looking for the source code of:

IJ.run(“Create Mask”, “”);

i.e. setting the 2 thresholds is ok but transforming the original image (8 bits) to obtain a new image composed of 0’s ans 255’s pixels.

Thanks for hints and answers

Alan


#2

Hi @Alan,

The search in FIji is super helpful for stuff like this:

Clicking on the “Source” button will take you to ij.plugin.Selection

I think this part:

does what you want.

John


#3

Actually, following the above, this is probably more immediately useful to you.


#4

Hi John,
Thanks for your help.
Alan


#5

Hi John,
Coming back to the Mask-stuff.
What seems curious is the difficulty to isolate the binary image created as mask:
Copying the mask does not work
Copying the image contain all the pixels infos.
The only way is what I found in the code you suggest:

ImagePlus imp2 = WindowManager.getCurrentImage();

As if the mask only exists as a window…

Anyway i will use that though it does not seem very elegant


#6

You’re right.

Thank’s

Alan


#7

The ImageProcessor class has a createMask() method. Here is a JavaScript example that shows how to use it:

  img = IJ.openImage("http://wsr.imagej.net/images/m51.zip");
  IJ.setAutoThreshold(img, "Huang dark no-reset");
  ip = img.getProcessor();
  mask = ip.createMask();
  ip.resetThreshold();
  for (i=0; i<ip.getPixelCount(); i++) {
    if (mask.get(i)>0)
       ip.set(i,65535);
  }
  img.show();

This is what the ByteProcessor version of createMask() looks like:

   public ByteProcessor createMask() {
      if (getMinThreshold()==NO_THRESHOLD)
         return null;
      int minThreshold = (int)getMinThreshold();
      int maxThreshold = (int)getMaxThreshold();
      ByteProcessor mask = new ByteProcessor(width, height);
      byte[] mpixels = (byte[])mask.getPixels();
      for (int i=0; i<pixels.length; i++) {
         int value = pixels[i]&0xff;
         if (value>=minThreshold && value<=maxThreshold)
            mpixels[i] = (byte)255;
      }
      return mask;
   }

#8

Wayne,

Thank’s for your answer. I shall get this new ImageJ version.

Alan


#9

You do not need to upgrade to the daily build to use the ImageProcessor.createMask() method, which was added in the ImageJ 1.52d. You do need to upgrade to the daily build (1.52f16) to use the new ImagePlus.createThresholdMask() method, which is now recorded when you use the Edit>Selections>Create Mask command.

Here is some JavaScript code that shows how to use createThresholdMask():

  img = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
  IJ.setAutoThreshold(img, "IsoData");
  mask = img.createThresholdMask();
  new ImagePlus("Mask_"+img.getTitle(),mask).show();

  img = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
  IJ.setAutoThreshold(img, "Default");
  mask = img.createThresholdMask();
  new ImagePlus("Mask_"+img.getTitle(),mask).show();

  img = IJ.openImage("http://wsr.imagej.net/images/m51.zip");
  IJ.setAutoThreshold(img, "Huang dark no-reset");
  mask = img.createThresholdMask();
  new ImagePlus("Mask_"+img.getTitle(),mask).show();

#10

Wayne,

Thank’s for your explainations

Sorry I should have explained that the masks I use are drawn with polylines.

I integrated your update but it needs thresholds to be set to create the masks.

I use the following to proceed:

  1. Draw the ROI’s (in connection with the ROI Manager)

  2. IJ.run(“Create Mask”, “”);

  3. ImagePlus impROI = WindowManager.getCurrentImage();

Alan


#11

It is not clear what you are trying to do. It would help if you could provide a MWE (Minimal Working Example), as a macro or script (https://en.wikipedia.org/wiki/Minimal_Working_Example).

You can use the createRoiMask() method to create masks from ROIs or overlays. You will need to be running ImageJ 1.52f19 or later for it to work with line selections and overlays.

Here is a JavaScript example that demonstrates the use of both the createThresholdMask() and createRoiMask() methods:

  // Threshold mask 1
  img = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
  IJ.setAutoThreshold(img, "IsoData");
  mask = img.createThresholdMask();
  new ImagePlus("Threshold Mask_"+img.getTitle(),mask).show();

  // Threshold mask 2
  img = IJ.openImage("http://wsr.imagej.net/images/m51.zip");
  IJ.setAutoThreshold(img, "Huang dark no-reset");
  mask = img.createThresholdMask();
  new ImagePlus("Threshold Mask_"+img.getTitle(),mask).show();

  // ROI mask
  img = IJ.createImage("Untitled", "16-bit", 250, 250, 1);
  xpoints = [42,169,219,70];
  ypoints = [73,40,151,200];
  roi = new PolygonRoi(xpoints,ypoints,Roi.POLYGON);
  img.setRoi(roi);
  mask = img.createRoiMask();
  new ImagePlus("ROI Mask",mask).show();

  // Overlay mask
  img = IJ.createImage("Untitled", "16-bit", 500, 250, 1);
  overlay = new Overlay();
  xpoints = [22,149,50];
  ypoints = [73,40,200];
  roi = new PolygonRoi(xpoints,ypoints,Roi.POLYGON);
  overlay.add(roi);
  overlay.add(new Roi(170,56,90,130));
  overlay.add(new OvalRoi(290,60,90,111));
  roi = new Line(370,210,440,45);
  overlay.add(roi);
  roi = new Line(390,210,460,45);
  roi.setStrokeWidth(5);
  overlay.add(roi);
  roi = new Line(415,210,485,45);
  roi.setStrokeWidth(10);
  overlay.add(roi);
  img.setOverlay(overlay);
  mask = img.createRoiMask();
  new ImagePlus("Overlay Mask",mask).show();


#12

Wayne, Thank’s a lot.

Alan