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 $A^{c}$ or background.
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.
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:
$\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].$
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.
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 $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:
- $I_1(i,j)=[R_1(i,j);G_1(i,j);B_1(i,j)]$
- $I_2(i,j)=[R_2(i,j);G_2(i,j);B_2(i,j)]$
- $O(i,j)=[R_O(i,j);G_O(i,j);B_O(i,j)]$
$\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].$