Tuesday, October 4, 2011

Boolean operators




One of the basic functions for binary images (containing TRUE and FALSE pixels) are the boolean operators useful to manipulate this kind of images.



1- Boolean operators

Fig.1: Image A


We need two test images to work with boolean operators. Build your own or download the two images of Fig.1 and 2 by right-clicking on them.
Then, load these two images in ImageJ with File > Open.
Check that your images are 8-bit and only contain black (value = 0) and white (value=255) pixels because boolean operators are only working with binary (boolean) images (containing two types of pixels: TRUE and FALSE).

Fig.2: Image B



Note: To convert an image in a binary image, use a threshold value to partition your image in two types of pixels (Image > Adjust > Threshold) or if your image is still in black and white, go to Process > Binary > Make Binary.

1-1- AND, OR, XOR operators
All types of calculation between images are available in Process > Image Calculator...
- Select image 'A' as image 1 and 'B' as image 2, then choose the AND operator (don't be confused with the 'Add' operator) in the combo list.
-Repeat the calculations with OR and XOR (exclusive OR). You'll get the images shown in Fig.3.

Fig.3: Result of AND, OR, and XOR respectively
To understand how it works, the simplest way is to fill an array where all the combinations of TRUE (value = 255) and FALSE (value = 0) pixels are met. For example, with the AND operator (Fig. 4), the pixel of XY-coordinates (131;150) is TRUE in 'A', and TRUE in 'B', the resulting image A AND B has a TRUE pixel value at (131;150). Repeat the same approach with the three other cases (TRUE/FALSE, FALSE/TRUE, and FALSE/FALSE) as shown in Fig. 4.

Fig.4: AND, OR, XOR operators
To summarize,
  • AND: The resulting pixel is TRUE if and only if a pixel of 'A' is TRUE *and* the other of 'B' is TRUE.
  • OR: The resulting pixel is TRUE if and only if a pixel is TRUE *or* the other is TRUE *or* both are TRUE.
  • XOR (for exclusive OR); The resulting pixel is TRUE if and only if a pixel is TRUE *or* the other is TRUE but not when the two pixels are TRUE.
1-2- NOT operator
Fig. 5: Result of NOT(A) calculated with Edit > Invert
There is an additional boolean operator called the NOT operator. Contrarily to the others, only one image is required and the NOT(image) corresponds to the negative (black becomes white and vice versa) of the original image.
In ImageJ, there is no NOT operator, however you can use the Edit > Invert function to get the same result.


1-3- Exercises
Try to find out what kind of boolean operator(s) are used to get these three images. Following the approach described in 1-1, fill the array of TRUE/FALSE and then, interpret this array in terms of boolean operations.
Tips: The left and right images are computed from images 'A' and 'B'. The middle image is only calculated from image 'A'.
Fig. 6: Three resulting images obtained by the use of various boolean operators.
Note: The answer will be somewhere in this blog.

2- Applications


The boolean operators are useful when one of the image acts as a mask to remove or highlight objects of interest.
Here is an example based on the analysis of the sample image 'blobs' (File > Open Samples > Blobs (25K) ).
After binarization (Process > Binary > Make Binary) , an Analyze > Analyze Particles... is run by choosing a size range of 400-Infinity and the display mode 'Masks'. The resulting image of Fig. 6B is created.
To get the blobs whose size is less than 400, you can re-run a new Analyze Particles with a size range of 0-400 or just calculate a XOR image between the binary image and the mask (Fig. 6C).
Note: Why a XOR? That's seems puzzling, but after the binarization, the image is in 'Inverted LUT' mode, thus black becomes TRUE and white, FALSE. In these conditions, a blob (in black and TRUE) present in both images disappear in the XOR image (TRUE XOR TRUE = FALSE).

Fig. 6: A) Binary image of blobs.gif; B) Mask after Analyze Particles. C) A XOR B to get the smallest blobs. These images are in 'Inverted LUT' mode.

3- Links


Several mini-games of crazybiocomputing are based on boolean operators:

No comments:

Post a Comment