Use an area to set scale instead of distance?

Is there a way to use a selected area to set the scale? My images have features that are a known size, and it would be easier to select the entire shape to set the scale instead of drawing a line across it.

There’s no direct way to do this, but you can for example:

  • Measure the raw pixel number inside your ROI
  • Divide the known size by the pixel number
  • Take the square root
  • In Image > Properties…, set pixel width and height to the resulting value

Here’s a Groovy script that does all of this:

// @ImagePlus imp
// @double(label="Please enter known area of ROI (or of whole image if no ROI exists)") knownArea
// @String(label="Unit of length") unit

roi = imp.getRoi()
if (roi == null) {
	rawArea = imp.width * imp.height
} else {
	rawArea = roi.getContainedPoints().length
}

pixelSize = Math.sqrt ( knownArea / rawArea )

cal = imp.getCalibration()
cal.pixelWidth = pixelSize
cal.pixelHeight = pixelSize
cal.setUnit(unit)
imp.setCalibration(cal)
imp.updateAndRepaintWindow()

You can run it from the script editor (after selecting Groovy as a language), it will work on the current image thanks to the script parameters at the start of the script.

1 Like