Skip to main content

Working with Masks

Masks are binary images which are used for filtering or isolating specific regions of interest within an image for processing and analysis.

Create a Mask object

In ImageJS there are three ways of creating a mask. First method for creating a mask is creating a Mask object. It is the easiest way, but once applied on the image, such mask will not affect it in any way, so it needs to be additionally customized by user.

const mask = new Mask(500, 500); // Creates a simple mask filled with 0s of size 500x500.

Options

PropertyRequiredDefault value
originno{row: 0, column: 0 }
datano-

Use threshold() method

Another approach is to obtain a mask by using threshold method on an image.

const mask = image.threshold(); // returns a mask

In most cases, thresholding is your go-to method to get a mask from an image.

placeholderplaceholder
Ran in 0.00μs (Infinity ops/s)
tip

threshold()method possesses different algorithms which can affect the mask output. It is better to try several of them to see which one fits your needs best. For instance the demo above uses 'otsu' algorithm.

Use cannyEdgeDetector() method

There is also a third way to get a mask. It is to use cannyEdgeDetector method.

const mask = image.cannyEdgeDetector(); // returns a mask
placeholderplaceholder
Ran in 0.00μs (Infinity ops/s)

Canny Edge detection is useful when there is a need to determine edges of the elements. Elements with a change of intensity are colored white, while regions with no change are colored black.