\links{or}{invert}{arthops}
\index{XOR}{XNOR}{\section{Logical XOR/XNOR}}
\title{Point Operations - Logical XOR/XNOR}
\html{
\rawhtml{
}
\rawhtml{
}
\strong{Common Names:} XOR, XNOR, EOR, ENOR
\rawhtml{}
}
\subsection{Brief Description}
XOR and XNOR are examples of \ref{logic}{logical operators} having the
\index{Truth-table|XOR/XNOR}{truth-tables} shown in \figref{ttabxor}.
\fig{ttabxor}{Truth-tables for XOR and XNOR.}
The XOR function is only true if just one (and only one) of the input
values is true, and false otherwise. XOR stands for \em{eXclusive
OR}. As can be seen, the output values of XNOR are simply the inverse
of the corresponding output values of XOR.
The XOR (and similarly the XNOR) operator typically takes two
\ref{binimage}{binary} or \ref{gryimage}{graylevel images} as input,
and outputs a third image whose \ref{value}{pixel values} are just
those of the first image, XORed with the corresponding pixels from the
second. A variation of this operator takes a single input image and
XORs each pixel with a specified constant value in order to produce
the output.
\subsection{How It Works}
The operation is performed straightforwardly in a single pass. It is
important that all the input pixel values being operated on have the
same number of bits in them, or unexpected things may happen. Where the
pixel values in the input images are not simple 1-bit numbers, the XOR
operation is normally (but not always) carried out individually on
each corresponding bit in the pixel values, in
\textref{logic}{bitwise}{bitwise fashion}.
\target{guidelines}{\subsection{Guidelines for Use}}
We illustrate the function of XOR using \imageref{scr3} and
\imageref{scr4}\latex{.} Since logical operators work more reliably
with binary input we first \ref{threshld}{threshold} the two images,
thus obtaining \imageref{scr3thr1} and \imageref{scr4thr1}\latex{.}
Now, we can use XOR to \index{Change detection|Using XOR
operator}{detect changes} in the images, since pixels which didn't
change output 0 and pixels which did change result in 1. The image
\imageref{scr3xor1} shows the result of XORing the thresholded images.
We can see the old and the new position of the moved object, whereas
the stationary object almost disappeared from the image. Due to the
effects of noise, we can still see some pixels around the boundary of
the stationary object, \ie pixels whose values in the original image
were close to the threshold.
In a scene like \imageref{pap1}\latex{,} it is not possible to apply a
threshold in order to obtain a binary image, since one of the objects
is lighter than the background whereas the other one is darker.
However, we can combine two grayscale images by XORing them in a
bitwise fashion. \imageref{pap3} shows a scene where the dark object
was moved and in \imageref{pap2} the light object changed its
position. XORing each of them with the initial image yields
\imageref{pap1xor1} and \imageref{pap1xor2}\latex{,} respectively. In
both cases, the moved part appears at the old as well as at the new
location and the stationary object almost disappears. This technique
is based on the assumption that XORing two similar grayvalues produces
a low output, whereas two distinct inputs yield a high output.
However, this is not always true, \eg XORing 127 and 128 yields 255.
These effects can be seen at the boundary of the stationary object,
where the pixels have an intermediate graylevel and might, due to
\ref{noise}{noise}, differ slightly between two of the images. Hence,
we can see a line with high values around the stationary object. A
similar problem is that the output for the moved pen is much higher
than the output for the moved piece of paper, although the contrast
between their intensities and that of the background value is roughly
the same. Because of these problems it is often better to use
\ref{pixsub}{image subtraction} or \ref{pixdiv}{image division} for
change detection.
As with other logical operators, XOR and XNOR are often used as
sub-components of more complex image processing tasks. XOR has the
interesting property that if we XOR \em{A} with \em{B} to get \em{Q},
then the bits of \em{Q} are the same as \em{A} where the corresponding
bit from \em{B} is zero, but they are of the opposite value where the
corresponding bit from \em{B} is one. So for instance using binary
notation, 1010 XORed with 1100 gives 0110. For this reason, \em{B}
could be thought of as a \index{Bit-reversal mask}{\em{bit-reversal mask}}. Since the operator is
symmetric, we could just as well have treated \em{A} as the mask and
\em{B} as the original.
Extending this idea to images, it is common to see an 8-bit XOR image
\ref{mask}{mask} containing only the pixel values 0 (00000000 binary)
and 255 (11111111 binary). When this is XORed pixel-by-pixel with an
original image it reverses the bits of pixels values where the mask is
255, and leaves them as they are where the mask is zero. The pixels
with reversed bits normally `stand out' against their original color
and so this technique is often used to produce a cursor that is
visible against an arbitrary colored background. The other advantage
of using XOR like this is that to undo the process (for instance when
the cursor moves away), it is only necessary to repeat the XOR using
the same mask and all the flipped pixels will become unflipped.
Therefore it is not necessary to explicitly store the original colors
of the pixels affected by the mask. Note that the flipped pixels are
not always visible against their unflipped color --- light pixels
become dark pixels and dark pixels become light pixels, but middling
gray pixels become middling gray pixels!
The image \imageref{wdg2} shows a simple graylevel image. Suppose that we wish
to overlay this image with its \ref{histgram}{histogram} shown in \imageref{wdg2hst1}
so that the two can be compared easily. One way is to use XOR. We
first use an \ref{imagedit}{image editor} to enlarge the histogram
until it is the same size as the first image. The result is shown in
\imageref{wdg2hst2}\latex{.} To perform the overlay we simply XOR this image
with the first image in bitwise fashion to produce
\imageref{wdg2xor1}\latex{.} Here, the text is quite easy to read, because the original image consists of large and rather light or rather dark areas. If we proceed in the same way with \imageref{bld1} we obtain \imageref{bld1xor1}\latex{.}
Note how the writing is dark against light backgrounds and light against dark backgrounds and hardly visible against gray backgrounds. Compare the result
with that described under \textref{or}{guidelines}{OR}. In fact
XORing is not particularly good for producing easy to read text on
gray backgrounds --- we might do better just to add a constant offset
to the image pixels that we wish to highlight (assuming wraparound
under addition overflow) --- but it is often used to quickly produce
highlighted pixels where the background is just black and white or
where legibility is not too important.
\subsection{Exercises}
\enumerate{
\item XOR \imageref{cir2} and \imageref{cir3}\latex{.} Compare the result with the output of XORing their \ref{invert}{negatives}. Do you see the same effect as for other logical operators?
\item Use the technique discussed above to produce a cursor on \imageref{fce1}\latex{.} Place the cursor on different location of the image and examine the performance on a background with high, low, intermediate and mixed pixel values.
}
\subsection{References}
\strong{R. Gonzalez and R. Woods} \em{Digital Image Processing},
Addison-Wesley Publishing Company, 1992, pp 47 - 51.
\strong{E. Davies} \em{Machine Vision: Theory, Algorithms and
Practicalities}, Academic Press, 1990, Chap.~2.
\strong{B. Horn} \em{Robot Vision}, MIT Press, 1986, pp 47 - 48.
\subsection{Local Information}
\input{xor.loc}