## Background

The AND binary operation takes two binary inputs and outputs 1 only when both the inputs are 1 else produces 0. The NAND binary operator is the complete inverse of the AND binary operation. NAND outputs 0 when both the inputs are 1, else it produces 0. The functionality of AND and NAND is clarified by the help of the truth tables:

Using AND and NAND boolean algebra allows you to represent things like: The sky is high and blue.

## Understanding AND & NAND Mathematically:

The ANDing of two images is carried out by performing AND operation to the corresponding images of the two images to produce the output pixel value. For instance, suppose that we wish to AND the integers 167 and 211 together using 8-bit integers. 167 is 10100111 in binary and 255 is 11010011. ANDing these together in bitwise fashion, we have 10000011 in binary or 131 in decimal.

This is the method adopted in the sample implementation, but not at all the only implementation of this of the logical operators rather you can implement the logical ANDing and NANDing using some thresholding to transform the digital image data into the binary format, or simply by taking the 0 pixel value as the logical 0 and the non-zero pixel values as the logical 1 value.

## General Working

The AND and NAND operations are performed through a single pass module which during operation passes through the each pixel of each image and calculates the pixels of the output image by doing the respective operation on the corresponding pixels to calculate the output pixels. It is necessary to have two images of the identical size. In the sample program, if two images are not identical in size, then they are made of the same size ( explained below).

## Guidelines for Use

The most obvious application of AND is to compute the intersection of two images. We illustrate this with an example where we want to detect those objects in a scene which did not move between two images, i.e. which are at the same pixel positions in the first and the second image. We illustrate this example using

and

If we simply AND the two images in a bitwise fashion we obtain

and if we simply NAND the two images in a bitwise fashion, we obtain the result as:

AND can also be used to perform so called bit-slicing on an 8-bit image. To determine the influence of one particular bit on an image, it is ANDed in a bitwise fashion with a constant number, where the relevant bit is set to 1 and the remaining 7 bits are set to 0. For example, to obtain the bit-plane 8 (corresponding to the most significant bit) we AND the image with 128 (10000000 binary) and threshold the output at a pixel value of 1.

## C# Sample Program:

The algorithm is coded in C# using unsafe so the quality and speed of the program may not be affected. The class BitmapData is used to read and process the pixels in the image. This is the specialty of C# to provide such a speed even on image processing applications. There is a set of modules that are designed to implement the algorithm. The program expects that the input images are gray scaled.

Then it takes the BitmapData objects of the two input images. It determines which image is bigger in width as well as in height. Then it scales the smaller image to acquire the size of the bigger image to make the sizes of both the images equal. Then the algorithm calculates the ANDed image in the single pass through the image. The final output is an image showing the result of the AND operator on the two input images which is then displayed on the screen in the down most picture box.

## Attachments:

Project Files: ANDing.zip