|
| 1 | +# --- |
| 2 | +# cover: assets/chelsea.gif |
| 3 | +# title: Histogram Matching |
| 4 | +# author: Ashwani Rathee |
| 5 | +# date: 2021-03-23 |
| 6 | +# --- |
| 7 | + |
| 8 | +# This demo demonstrates the feature of histogram matching. |
| 9 | + |
| 10 | +# Purpose of Histogram matching is to transform the intensities in a source image so that the |
| 11 | +# intensities distribute according to the histogram of a specified target image. |
| 12 | + |
| 13 | +# Here, histogram matching is shown for the RGB image. But, it can also be applied on |
| 14 | +# grayscale images too. Histogram matching can be used to normalize two images when the images |
| 15 | +# were acquired at the same local illumination (such as shadows) over the same location, |
| 16 | +# but by different sensors, atmospheric conditions, or global illumination. |
| 17 | + |
| 18 | +using ImageContrastAdjustment |
| 19 | +using Images |
| 20 | +using Plots |
| 21 | +using TestImages |
| 22 | + |
| 23 | +# Load example images: one source image and one reference image |
| 24 | + |
| 25 | +# !!! note |
| 26 | +# You need to use `julia >= v"1.3.0"` and `TestImages >= v"1.3.1"` in order to load these |
| 27 | +# two test images. |
| 28 | +img_source = testimage("chelsea"); |
| 29 | +img_reference = testimage("coffee"); |
| 30 | + |
| 31 | +# Applying histogram matching on source image using the reference image. |
| 32 | +# Here we use `adjust_histogram` function from ImageContrastAdjustment.jl. |
| 33 | +# It returns a histogram matched image with a granularity of `nbins`, i.e., number of bins. |
| 34 | +# The first argument `img` is the image to be matched, and the second argument `targetimg` is |
| 35 | +# the image with the desired histogram to be matched to. |
| 36 | + |
| 37 | +img_transformed = adjust_histogram(img_source, Matching(targetimg = img_reference)) |
| 38 | +mosaicview(img_source, img_reference, img_transformed; nrow = 1) |
| 39 | + |
| 40 | +save("assets/chelsea.gif", cat(img_source, img_transformed; dims = 3); fps=2) #src |
| 41 | + |
| 42 | +# To show the effect of histogram matching, we plot for each RGB channel the histogram. |
| 43 | + |
| 44 | +hist_final = [histogram(vec(c.(img))) |
| 45 | + for c in (red, green, blue) |
| 46 | + for img in [img_source, img_reference, img_transformed] |
| 47 | +] |
| 48 | + |
| 49 | +plot( |
| 50 | + hist_final..., |
| 51 | + layout = (3, 3), |
| 52 | + size = (800, 800), |
| 53 | + legend = false, |
| 54 | + title = ["Source" "Reference" "Histograms Matched"], |
| 55 | + reuse = false, |
| 56 | +) |
| 57 | + |
| 58 | +# From top to bottom are histograms for the red, green and blue channels. |
| 59 | +# From left to right are the source, reference and the matched images. |
| 60 | + |
| 61 | +# Visual inspection from the plots confirms that `img_transformed` resembles `img_target` |
| 62 | +# much more closely than `img_source`. |
| 63 | + |
| 64 | +# ### Credit and license |
| 65 | + |
| 66 | +# This demo follows the [scikit-image version](https://scikit-image.org/docs/stable/auto_examples/color_exposure/plot_histogram_matching.html#sphx-glr-auto-examples-color-exposure-plot-histogram-matching-py), any usage of |
| 67 | +# this demo should also satisfies the [scikit-image license](https://github.com/scikit-image/scikit-image/blob/main/LICENSE.txt). |
0 commit comments