# Image Histograms
Histogram of a digital image with gray values is the discrete function
$
p\left(r_{k}\right)=\frac{n_{k}}{n}
$
$n_{k}:$ Number of pixels with gray value $r_{k}$
$n:$ total Number of pixels in the image
If we consider the gray values in the image as realizations of a random variable $\mathrm{R},$ with some probability density, histogram provides an approximation to this probability density.
In other words, $\operatorname{Pr}\left(R=r_{k}\right) \approx p\left(r_{k}\right)$
Shape of histogram provides useful information for image global appearance.
Histogram based image enhancement
![[Screenshot 2020-09-13 at 11.10.18 AM.jpg]]
## Histogram Modification
- $v_{b}=M\left(v_{a}\right)$
- $M\left(v_{\mathrm{a}}\right)$ takes any value $v_{\mathrm{a}}$ in image $\mathrm{A}$ and maps it into $v_{\mathrm{b}}$ in image B.
Requirement: the mapping $\mathrm{M}$ is a non descending function $\left(\mathrm{M}^{-1}\right.$ exists $)$
If it is required to map the full gray-level range (256 values) to its full range while keeping the gray-level order, a non-decreasing monotonic mapping function is needed:
**Contrast Enhancement** ![[Screenshot 2020-09-13 at 11.15.26 AM.jpg]]
**Power-law transformation**
$S=C r^{\gamma}$
![[Screenshot 2020-09-13 at 11.17.37 AM.jpg]]
Results -
![[Screenshot 2020-09-13 at 11.18.35 AM.jpg]]
## Histogram Equalization
For a better visual discrimination of an image we would like to re-assign gray-levels so that graylevel resource will be optimally assigned.
Our goal: finding a gray-level transformation $\mathrm{M}(\mathrm{v})$ such that:
- The histogram $\mathrm{H}_{\mathrm{b}}$ is as flat as possible.
- The order of gray-levels is maintained.
- The histogram bars are not fragmented.
![[Screenshot 2020-09-13 at 11.22.18 AM.jpg]]
Histogram equalization may not always produce desirable results, particularly if the given histogram is very narrow. It can produce false edges and regions. It can also increase image "graininess" and "patchiness."
## Histogram Matching
Histogram matching is a process where an image is modified such that its histogram matches that of another (reference) image.
![[Screenshot 2020-09-13 at 11.24.43 AM.jpg]]
## Histogram Binarization
Assign a binary value to each pixel based on a threshold.
Example: Document scanning, background subtraction, blob detection etc.
$B(r, c)=\left\{\begin{array}{lll}1 & \text { if } & I(r, c) \geq T \\ 0 & \text { if } & I(r, c)<T\end{array}\right.$
$-\quad(r, c):$ row and column
$-\quad I(. . .):$ gray-level intensity image
$-\quad T \quad:$ intensity threshold
$-\quad B(\ldots):$ binary intensity image
How to select proper threshold?
- Histogram analysis, works only for simple images with high constrast
- Heuristic solution - fitting a 2 mode gaussian mixture to the histogram: minimizing KL divergence
**[[Gaussian Mixture Model]] fitting: KL algorithm**
The observations come from a weighted mixture of two Gaussians distributions
Gaussian Distribution of Background $\left(u_{1}, \sigma_{1}^{2}\right)$
Gaussian Distribution of Object $\left(u_{2}, \sigma_{2}^{2}\right)$
$f(i)=\frac{q_{1}}{\sigma_{1} \sqrt{2 \pi}} e^{\left.-\frac{1}{2} \frac{i-\mu_{1}}{\sigma_{1}}\right)^{2}}+\frac{q_{2}}{\sigma_{2} \sqrt{2 \pi}} e^{-\frac{1}{2}\left(\frac{i-\mu_{2}}{\sigma_{2}}\right)^{2}}$
![[Screenshot 2020-09-13 at 11.34.33 AM.jpg]]
![[Screenshot 2020-09-13 at 11.34.59 AM.jpg]]
---
## References