<a id="easyimage-arithmetic-and-logic"></a>

# Arithmetic and Logic

Non-exhaustive list of reasons one may use arithmetic and logic operations:

* **To emphasize differences** between images by subtracting the pixels (a conformity check).
* **To compensate for non-uniform lighting** by dividing the target image by the image of the background alone.
* **To remove unwanted areas of an image** by preparing an appropriate mask, and clearing all the pixels that belong to the mask by using logical combinations of pixels.
* **To create a combined image** by combining the pixels of two source images to generate a resulting image.

Arithmetic operations are handled by the [`EasyImage::Oper`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#adf314c42c92233ca337668250125375e) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#afcbccbf75626021b9a80ae1f9978eac4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#ad2662856c57834c7b067d0b4a957d097) function, [`EArithmeticLogicOperation`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html) enum lists all supported operators.

These operations can be applied to images and constants, they have one or two source arguments (image or integer constants) and one destination argument. If the source operands are a color and a gray-level image, each color component combines with the gray-level component to give a color image. [Histogram equalization](computing_image_statistics.html.md) can improve your results.

<a id="easyimage-arithmetic-logic-combinations"></a>

## Arithmetic and Logic Combinations

All possible operations are classed in seven categories in this documentation, and all operations for each category are detailed later.

* [General]()
* [Copy]()
* [Invert]()
* [Shift]()
* [Logical]()
* [Overlay]()
* [Set]()

<a id="easyimage-arithmetic-logic-allowed-combinations"></a>

### Allowed Combinations of Input(s) and Output

| Input(s)                        | Output      | General   | Copy   | Invert   | Shift   | Logical   | Overlay   | Set   |
|---------------------------------|-------------|-----------|--------|----------|---------|-----------|-----------|-------|
| `EBW8` constant                 | `EImageBW8` |           | ✓      |          |         |           |           |       |
| `EC24` constant                 | `EImageC24` |           | ✓      |          |         |           |           |       |
| `EImageBW8`                     | `EImageBW8` |           | ✓      | ✓        |         |           |           |       |
| `EImageBW8`                     | `EImageC24` |           | ✓      | ✓        |         |           | ✓         |       |
| `EImageC24`                     | `EImageC24` |           | ✓      | ✓        |         |           |           |       |
| `EBW8` constant and `EImageBW8` | `EImageBW8` | ✓         |        |          |         |           |           |       |
| `EImageBW8` and `EBW8` constant | `EImageBW8` | ✓         |        |          | ✓       |           |           | ✓     |
| `EImageBW8` and `EImageBW8`     | `EImageBW8` | ✓         |        |          |         | ✓         |           | ✓     |
| `EImageBW8` and `EImageBW8`     | `EImageC24` | ✓         |        |          |         |           | ✓         |       |
| `EC24` constant and `EImageC24` | `EImageC24` | ✓         |        |          |         |           |           |       |
| `EImageC24` and `EC24` constant | `EImageC24` | ✓         |        |          | ✓       |           |           |       |
| `EImageC24` and `EImageC24`     | `EImageC24` | ✓         |        |          |         | ✓         |           |       |
| `EImageC24` and `EImageBW8`     | `EImageC24` | ✓         |        |          |         | ✓         | ✓         |       |
| `EImageBW8` and `EImageC24`     | `EImageC24` | ✓         |        |          |         | ✓         |           | ✓     |

#### NOTE
For logical operators, a pixel with value 0 is assumed `false`, otherwise `true`. The result of a logical operation is 0 when `false` and 255 when `true`.

<a id="general-arithmetic-logic-operations"></a>

## General Operations

| * Compare (abs. value of the difference)<br/>* Saturated sum<br/>* Saturated difference<br/>* Saturated product<br/>* Saturated quotient<br/>* Modulo<br/>* Overflow-free sum<br/>* Overflow-free difference<br/>* Overflow-free product<br/>* Overflow-free quotient   | * Bitwise AND<br/>* Bitwise OR<br/>* Bitwise XOR<br/>* Minimum<br/>* Maximum<br/>* Equal<br/>* Not equal<br/>* Greater or equal<br/>* Lesser or equal<br/>* Greater<br/>* Lesser   |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

<a id="copy-arithmetic-logic-operations"></a>

## Copy Operations

* Copy [`EArithmeticLogicOperation_Copy`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83a2a54a28271809985ec59c0012e5763b2) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#a0743e35466c7ccd2141ea83d07355ad3)
  * Copies a constant to all the pixels of an image. Useful, for example, to initialize a newly created image.
  * Copies an image to another image.

<a id="invert-arithmetic-logic-operations"></a>

## Invert Operations

* Invert [`EArithmeticLogicOperation_Invert`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83a48c809afc404eed05f68b396a3565dcc) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#aeca4f8e031967ffc88ecf8011bf23195)
  * When not specifying a constant, the image is inverted into the destination image. Which means, each pixel channel value is computed as $255 - pixelChannelValue$
  * Otherwise, when a constant is specified, it is computed as $constant - pixelChannelValue$

<a id="shift-arithmetic-logic-operations"></a>

## Shift Operations

* Left Shift [`EArithmeticLogicOperation_ShiftLeft`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83afd007cab66e9eba7aaba3bba1100d055) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#ab07066de12b98da80d5b8f496fc4477f)
* Right Shift [`EArithmeticLogicOperation_ShiftRight`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83a5bde0e155c3f57abd74fc4f7f0ccd76e) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#a7798471bb32e6ae1349e257f1862106d)

<a id="logical-arithmetic-logic-operations"></a>

## Logical Operations

* Logical AND [`EArithmeticLogicOperation_LogicalAnd`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83ab93221c274bd5d276cfa80a93082fe8a) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#a06c80f357e8c5856236a3611d00d87cf)
* Logical OR [`EArithmeticLogicOperation_LogicalOr`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83a83b4627a835aa6ab11ae97f46591e4b6) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#a3ecbad5f8550519c85765e11dc45753d)
* Logical XOR [`EArithmeticLogicOperation_LogicalXor`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83aa7fc065cdcfea6c3cf5422e4cfef7dc0) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#a41ea9928c41eeb79467c16a48e96b551)

<a id="overlay-arithmetic-logic-operations"></a>

## Overlay Operations

* Add an overlay [`EArithmeticLogicOperation_Overlay`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83a1523a8ca90f6786c4733f2c1b29edb25) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#ab559e7c0b38a2d475f490101ca062651)

<a id="set-arithmetic-logic-operations"></a>

## Set Operations

This operation involves three images.

* `Source Image`: that will be conditionally copied to `Destination Image`.
* `Mask Image`: that will set whether a pixel from `Source Image` must be copied to `Destination Image`.
* `Destination Image`: whose pixels will equal pixel from `Source Image` if mask image pixel is
  * Equal to zero when using [`EArithmeticLogicOperation_SetZero`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83a0e2749403f31903720d4155de76956f9) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#ade15a93c9f9ef4387371b9e79bf9e160)
  * Different from zero when using [`EArithmeticLogicOperation_SetNonZero`](../../../doxygen_cpp/namespaceeVision.html#aebf26b10397a83bd42f31e33b5d59d83a0f90f63f376797c242d2c890605fb5bf) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a42668c67c44fbf0785f8d727ecf9963d) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EArithmeticLogicOperation.html#a7a34c8a9ddca6b6bab45334fe19cd08c)
  * Otherwise, the pixel value is **unchanged**.

### C++

```c++
EasyImage::Oper(EArithmeticLogicOperation_SetZero, &sourceImage, &maskImage, &destinationImage);
```

### C#

```c#
EasyImage.Oper(EArithmeticLogicOperation.SetZero, sourceImage, maskImage, destinationImage);
```

### Python

```python
EasyImage.Oper(EArithmeticLogicOperation.SetZero, sourceImage, maskImage, destinationImage);
```

#### NOTE
For logical operators, a pixel with value 0 is assumed `false`, otherwise `true`. The result of a logical operation is 0 when `false` and 255 when `true`.

<a id="easyimage-arithmetic-logic-quick-links"></a>

## See Also

Easy to understand, ready to copy code snippets.
