Logical Operations
This group contains pointwise logical operators.
- MaskImage: Masks a graylevel or color image I by a binary mask B.
- LogicalOperationWithImage: Performs a pointwise logical operation between two images.
- LogicalOperationWithValue: Performs a pointwise logical operation between an image and a value.
- LogicalNot: Computes the bitwise logical negation of an image.
- InvertImage: Calculates the reverse intensities of an image on a given number of significant bits.
- BitShift: Shifts the intensities of an image by a given number of bits.
The Logical Operations group contains logical operations between two images
or between an image and a constant, like union, intersection, complementary.
Logical operations are usually performed on binary images, but also can be applied to grayscale or color images. In a binary image, a pixel/voxel with value 1 has the value true and a pixel/voxel with value 0 a value false.
Each logical operation is linked to a boolean operation. In a binary image I, all pixels/voxels with value 1 belong to the set A or foreground and all 0-value pixels/voxels to its complement Ac or background.
Let I1 and I2 be two images on which a logical AND operation is performed.
Supposing that for pixel (n,m), intensities I1 and I2 are respectively equal to I1(n,m)=5 and I2(n,m)=12, let's see how is computed the result O(n,m). Considering the binary code of the two numbers, the operator is applied to each of the eight bits as shown in Figure 1.

Figure 1. Bitwise logical AND.
The result of the logical AND may appear rather strange and for this reason logical operations applied to grayscale images have to be handled with care.
To mask an image, a logical AND is performed between images I and M where every pixel with value 1 is coded with 1 (short value) and every pixel with value 0 is coded with 0 (short value).
When masking a grayscale image I with a binary image M:
When a logical operation is applied to a color image, it operates independently on each color channel.
A color image pixel/voxel is a triplet of values coded on an unsigned byte each:
{RO(i,j)=R1(i,j)⊗R2(i,j)GO(i,j)=G1(i,j)⊗G2(i,j)BO(i,j)=B1(i,j)⊗B2(i,j)
For example, supposing that I1(i,j)=(255;0;255) (magenta) and I2=(127;127;255) (light blue), performing an AND operation should give:
{RO(i,j)=255∧127=127GO(i,j)=0∧127=0BO(i,j)=255∧255=255
When performing a logical operation between an image and a fixed value: O(i,j)=I1(i,j)⊗C, the following formula is applied:
O(i,j)=[;RO(i,j)GO(i,j)BO(i,j)]=[;R1(i,j)⊗CG1(i,j)⊗CB1(i,j)⊗C].
For example, supposing that I1(i,j)=(255;0;255) (magenta) and C=7, performing a XOR operation should give:
O(i,j)=[;RO(i,j)GO(i,j)BO(i,j)]=[;255⊗70⊗7255⊗7;]=[;2487248;].
Logical operations are usually performed on binary images, but also can be applied to grayscale or color images. In a binary image, a pixel/voxel with value 1 has the value true and a pixel/voxel with value 0 a value false.
Each logical operation is linked to a boolean operation. In a binary image I, all pixels/voxels with value 1 belong to the set A or foreground and all 0-value pixels/voxels to its complement Ac or background.
Grayscale Images
When a logical operation is applied to a grayscale image, the logical operations for binary images are applied to each bit plane.Let I1 and I2 be two images on which a logical AND operation is performed.
Supposing that for pixel (n,m), intensities I1 and I2 are respectively equal to I1(n,m)=5 and I2(n,m)=12, let's see how is computed the result O(n,m). Considering the binary code of the two numbers, the operator is applied to each of the eight bits as shown in Figure 1.

Figure 1. Bitwise logical AND.
The result of the logical AND may appear rather strange and for this reason logical operations applied to grayscale images have to be handled with care.
Masking and Bit Plane Combination
The two main applications of logical operations on grayscale images are for masking and for combining bit planes.To mask an image, a logical AND is performed between images I and M where every pixel with value 1 is coded with 1 (short value) and every pixel with value 0 is coded with 0 (short value).
When masking a grayscale image I with a binary image M:
- I(n,m) remains unchanged if M(n,m)=1
- I(n,m) turns to 0 if M(n,m)=0
Color Images
Let I1 and I2 be two color images on which the ⊗ boolean operator is applied to obtain the output image O=I1⊗I2.When a logical operation is applied to a color image, it operates independently on each color channel.
A color image pixel/voxel is a triplet of values coded on an unsigned byte each:
- I1(i,j)=[R1(i,j);G1(i,j);B1(i,j)]
- I2(i,j)=[R2(i,j);G2(i,j);B2(i,j)]
- O(i,j)=[RO(i,j);GO(i,j);BO(i,j)]
{RO(i,j)=R1(i,j)⊗R2(i,j)GO(i,j)=G1(i,j)⊗G2(i,j)BO(i,j)=B1(i,j)⊗B2(i,j)
For example, supposing that I1(i,j)=(255;0;255) (magenta) and I2=(127;127;255) (light blue), performing an AND operation should give:
{RO(i,j)=255∧127=127GO(i,j)=0∧127=0BO(i,j)=255∧255=255
When performing a logical operation between an image and a fixed value: O(i,j)=I1(i,j)⊗C, the following formula is applied:
O(i,j)=[;RO(i,j)GO(i,j)BO(i,j)]=[;R1(i,j)⊗CG1(i,j)⊗CB1(i,j)⊗C].
For example, supposing that I1(i,j)=(255;0;255) (magenta) and C=7, performing a XOR operation should give:
O(i,j)=[;RO(i,j)GO(i,j)BO(i,j)]=[;255⊗70⊗7255⊗7;]=[;2487248;].