Pick Pixels that have been painted using the Paintbrush tool

Hi, I’m new to ImageJ and having some trouble. Is there any example code of how to pick the set of pixels in an ImagePlus that have been overlayed by the Paintbrush tool? I am using Java. Sorry if it’s obvious but I’m just a few days into ImageJ coding with Java.

Create a mask from the overlay, convert the mask to an ROI and then call Roi.getContainedPoints() to get the coordinates of the pixels inside the ROI. Here is a JavaScript example:

  img = IJ.openImage("https://imagej.net/images/blobs.gif");
  img.show();
  new WaitForUserDialog("Use brush to draw on overlay").show();
  mask = img.createRoiMask();
  maskImg = new ImagePlus("Mask",mask);
  IJ.run(maskImg, "Create Selection", "");
  roi = maskImg.getRoi();
  points = roi.getContainedPoints();
  print(points.length+" points");

2 Likes

Thank you so much! This is very helpful!

What if I want to have two Images Created from the two brush labels. Instead of it showing the two labels in one mask but to break it so that I show 2 masks.

Thanks again!

Use the overlay brush tool to work with multiple objects. The following JavaScript example lists the overlay ROIs created by the overlay brush tool.

  img = IJ.openImage("https://imagej.net/images/blobs.gif");
  img.show();
  msg = "Use overlay brush tool to draw objects on image.";
  new WaitForUserDialog(msg).show();
  overlay = img.getOverlay();
  for (i=0; i<overlay.size(); i++)
     print(i+" "+overlay.get(i));

1 Like

Hello @Wayne! How can I get the overlays to have the same size as the original image? Such that only the painted area is white and the rest of the pixels are black.

Assign the ROIs in the overlay to the image and call ImagePlus.img.createRoiMask(), as in this example:

  img = IJ.openImage("https://imagej.net/images/blobs.gif");
  img.show();
  msg = "Use overlay brush tool to draw objects on image.";
  new WaitForUserDialog(msg).show();
  overlay = img.getOverlay();
  for (i=0; i<overlay.size(); i++) {
     img.setRoi(overlay.get(i));
     mask = img.createRoiMask();
     new ImagePlus("mask"+i,mask).show();
  }
1 Like

Hello!

How can I change the color and the size of the overlay brush programmatically? And whats the best way to get the overlay by color?

Thanks,

Hi @rkasumba,
The overlay brush tool uses the global foreground color, which you can set using Toolbar.setForegroundColor(). The ImageJ 1.53h8 daily build adds a OverlayBrushTool.setWidth() method that you can use to set the brush width. You can get the ROIs of a given color by filtering on the stroke color. Here is an example that sets the color to cyan, the brush width to 21 and lists the cyan ROIs:

  img = IJ.openImage("https://imagej.net/images/blobs.gif");
  img.show();
  Toolbar.setForegroundColor(Color.cyan);
  OverlayBrushTool.setWidth(21);
  msg = "Use overlay brush tool to draw objects on image.";
  new WaitForUserDialog(msg).show();
  overlay = img.getOverlay();
  print("Cyan ROIs:");
  for (i=0; i<overlay.size(); i++) {
     roi = overlay.get(i);
     if (roi.getStrokeColor().equals(Color.cyan))
        print("  "+i+" "+roi);
  }
2 Likes