# Logic and arithmetic image operations

## 1 Bitwise logic operations

Mainly used for **masking** (selecting features to enhance)

### 1.1 NOT

$f(x,y) $ | NOT |
---|---|

0 | 1 |

1 | 0 |

### 1.2 AND

AND | 0 | 1 |
---|---|---|

0 | 0 | 0 |

1 | 0 | 1 |

- $f$ AND
`0xff`

? - NOT ($f$ AND
`0xff`

)?

### 1.3 OR

OR | 0 | 1 |
---|---|---|

0 | 0 | 1 |

1 | 1 | 1 |

### 1.4 XOR

XOR | 0 | 1 |
---|---|---|

0 | 0 | 1 |

1 | 1 | 0 |

## 2 Exercise: Shadow enhancement using logic operations

- Log the map: log_map
- Brighten everywhere, not just the shadow

- Gray-level binary slice the map: mask_shadow
- Identify the range of pixel values
- 255 for the shadow, 0 for the rest
- What range works best?

- log_map AND mask_shadow: enhanced_shadow
- NOT mask_shadow: mask_nonshadow
- map AND mask_nonshadow: nonenhanced_nonshadow
- nonenhanced_nonshadow OR enhanced_shadow: map_with_enhanced_shadow

## 3 Arithmetic operations

### 3.1 Subtraction

Difference/motion detection

\[g(x,y)=f(x,y)-h(x,y)\]

### 3.2 Averaging

Let $f(x,y)$ be a noiseless image, but we have \[g(x,y)=f(x,y)+\eta(x,y)\] where $\eta(x,y)$ is noise with a zero mean. For example, $\eta\sim\mathcal{N}(0,\sigma_\eta^2)$.

How can we enhance $g(x,y)$ and make it closer to $f(x,y)$?

### 3.3 Averaging: Need more noisy images?

Obtain more noisy images $g_i(x,y)$ for $1\leq i\leq K$.

### 3.4 Averaging: Take the average of these images

The average of these noisy images \begin{split} \bar{g}(x,y)&=\frac{1}{K}\sum_{i=1}^K g_i(x,y)\\ &=f(x,y)+\frac{1}{K}\sum_{i=1}^K\eta_i(x,y). \end{split}

### 3.5 Average: The expected value of the average

The expected value of $\bar{g}(x,y)$ \[E\left[\bar{g}(x,y)\right]=f(x,y)\] because $f(x,y)$ is constant and $E\left[\frac{1}{K}\sum_{i=1}^K\eta_i(x,y)\right]=0$.

More important, \[\sigma_{\bar{g}(x,y)}=\frac{1}{\sqrt{K}}\sigma_\eta(x,y),\] which means the more $g_i(x,y)$, the smaller error.

## 4 Exercise: Noise reduction using averaging

- Add noise to the map: noisy_map_1
- Probability of noise: 0.5
- Max noise: 20

- Repeat step 1 * 4 more times: noisy_map_2, ..., noisy_map_5
- Subtract the map from noisy_map_1: diff_1
- Average noisy_map_1 and noisy_map_2: average_map_2
- Subtract the map from average_map_2: diff_2
- Repeat steps 4-5 * 3 more times by adding one more noisy map each time
- Compare the histograms of diff_1, ..., diff_5

## 5 Homework: Non-shadow enhancing

Using a new workflow that is very similar to the shadow-enhancing technique we used in class, enhance non-shadow areas in your original grayscale image because these areas are too bright. Capture the original image, step-by-step tool screenshots, and the final image. For each step, explain why you use which specific tool.

## 6 Homework: Noise reduction using averaging

Find how many noisy maps you need to reduce the standard deviation of noise below 10% of its maximum (you decide this number when adding noise)? Submit your report and the original gray, noisy, and difference maps in TIFF in one ZIP file to D2L. In the report, explain how you calculated the noisy and difference maps, mention your maximum noise, and add a table with the number of noisy maps for averaging, the average and standard deviation of the difference map for each averaging. For example,

Number of noisy maps | Average noise | SD noise | SD/max noise (%) |
---|---|---|---|

1 | 0.17 | 10.5 | 52.5% |

2 | 0.25 | 5.5 | 27.5% |

... | |||

10 | 0.15 | 1.5 | 7.5% |