Skip to content

Commit 4e3838c

Browse files
Histogram Matching demo (cont. #178) (#185)
Bring the scikit histogram matching example into JuliaImages Co-authored-by: Ashwani Rathee <[email protected]>
1 parent 2e66c75 commit 4e3838c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ MosaicViews = "e94cdb99-869f-56ef-bcf0-1ae2bcbe0389"
2929
Noise = "81d43f40-5267-43b7-ae1c-8b967f377efa"
3030
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3131
PaddedViews = "5432bcbf-9aad-5242-b902-cca2824c8663"
32+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
3233
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
3334
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
3435
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
@@ -65,6 +66,7 @@ MosaicViews = "0"
6566
Noise = "0.2"
6667
OffsetArrays = "1.0"
6768
PaddedViews = "0"
69+
Plots = "1"
6870
Rotations = "1"
6971
SimpleTraits = "0"
7072
TestImages = "0, 1.0"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)