ImageDev

Logical Operations

This group contains pointwise logical operators.
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 $A^{c}$ 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 $I_1$ and $I_2$ be two images on which a logical AND operation is performed.
Supposing that for pixel (n,m), intensities $I_1$ and $I_2$ are respectively equal to $I_1(n,m) = 5$ and $I_2(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.

<b> Figure 1.</b> Bitwise logical AND.
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: To combine bit planes, if two images $I_1$ and $I_2$ have grayscale values in the range 0 to 15 (4-bit images), one has to shift the image $I_2$ by 4 bits on the left. A logical OR between image $I_1$ and image $I_2$ shifted by 4 bits ($I_1 \vee ( I_2 << 4)$ ) gives an 8-bit image made of the two images.

Color Images

Let $I_1$ and $I_2$ be two color images on which the $\otimes$ boolean operator is applied to obtain the output image $O={I_1}\otimes{I_2}$.

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: Then the channels of the three images are related as follows:
$\left\{\begin{array}{c} R_O(i,j)={R_1(i,j)}\otimes{R_2(i,j)}\\ G_O(i,j)={G_1(i,j)}\otimes{G_2(i,j)}\\ B_O(i,j)={B_1(i,j)}\otimes{B_2(i,j)}\\ \end{array}\right.$

For example, supposing that $I_1(i,j)=(255;0;255)$ (magenta) and $I_2=(127;127;255)$ (light blue), performing an AND operation should give:
$\left\{\begin{array}{l} R_O(i,j)={255}\wedge{127}=127\\ G_O(i,j)={0}\wedge{127}=0\\ B_O(i,j)={255}\wedge{255}=255\\ \end{array}\right.$

When performing a logical operation between an image and a fixed value: $O(i,j)={I_1(i,j)}\otimes{C}$, the following formula is applied:
$O(i,j)=\left[\begin{array}{c}; R_O(i,j)\\ G_O(i,j)\\ B_O(i,j)\\ \end{array}\right]=\left[\begin{array}{c}; {R_1(i,j)}\otimes{C}\\ {G_1(i,j)}\otimes{C}\\ {B_1(i,j)}\otimes{C}\\ \end{array}\right].$

For example, supposing that $I_1(i,j)=(255;0;255)$ (magenta) and $C=7$, performing a XOR operation should give:
$O(i,j)=\left[\begin{array}{c}; R_O(i,j)\\ G_O(i,j)\\ B_O(i,j)\\ \end{array}\right]=\left[\begin{array}{c}; {255}\otimes{7}\\ {0}\otimes{7}\\ {255}\otimes{7}; \end{array}\right]=\left[\begin{array}{c}; {248}\\ {7}\\ {248}; \end{array}\right].$