The Simplest Classifier - Histogram Comparison
The Simplest Classifier - Histogram Comparison
root@mpatacchiola:~$ about_me;
https://mpatacchiola.github.io/blog/2016/11/12/the-simplest-classifier-histogram-intersection.html 1/12
2020/9/6 The Simplest Classifier: Histogram Comparison
∑ min(Ij , Mj )
j=1
The min function take as arguments two values and return the
smallest one. The result of the intersection is the number of
pixels from the model that have corresponding pixels of the same
colours in the input image. To normalise the result between 0 and 1
we have to divide it by the number of pixels in the model histogram:
n
∑ min(Ij , Mj )
j=1
n
∑ Mj
j=1
Implementation in Python
In python we can easily play with histograms, for instance numpy
has the function numpy.histogram() and OpenCV the function
https://mpatacchiola.github.io/blog/2016/11/12/the-simplest-classifier-histogram-intersection.html 2/12
2020/9/6 The Simplest Classifier: Histogram Comparison
The code below generates two histograms using values sampled from
two different normal distribution (mean=mu_1, mu_2; std=2.0). The
histograms have 100 bins which contains values in the range [-15,
15]. Once we have the two numpy histograms we can use the following
function to compare them:
Plotting the histogram generated from N (4, 2) and the one generated
from N (−4, 2) we can easily visualise the intersection:
https://mpatacchiola.github.io/blog/2016/11/12/the-simplest-classifier-histogram-intersection.html 3/12
2020/9/6 The Simplest Classifier: Histogram Comparison
On the x axis there are the 100 bins, whereas on the y axis is
represented the number of elements inside each bin. You can see the
first bin as a counter for the elements comprised between [-15.0,
-15.15), the second bin as a counter for the elements in the range
[-15.15, -15.30), and so on. The intersection between the red graph
and the blue one is what we are looking for. That intersection is
larger when the two distribution are similar. In the previous
example the intersection value returned by return_intersection() was
0.035. If we choose normal distributions with closest mean N (2, 2)
and N (−2, 2) we get a larger intersection:
https://mpatacchiola.github.io/blog/2016/11/12/the-simplest-classifier-histogram-intersection.html 4/12
2020/9/6 The Simplest Classifier: Histogram Comparison
Superheroes classification
Maybe you already noticed that each superheroes from the Marvel and
DC universe has a distinctive multicolour fingertip. In the image
below you can visually get the differences:
https://mpatacchiola.github.io/blog/2016/11/12/the-simplest-classifier-histogram-intersection.html 5/12
2020/9/6 The Simplest Classifier: Histogram Comparison
import cv2
import numpy as np
from deepgaze.color_classification import HistogramColorClassifier
Now we have to load the models. You can read the image with the
OpenCV function cv2.imread() and then load it calling the deepgaze
addModelHistogram() function. You have to repeat this operation for
all the models you want to store in the classifier. In my case I
stored eight model in the following order [Flash, Batman, Hulk,
Superman, Capt. America, Wonder Woman, Iron Man, Wolverine] . For each model
I called the addModelHistogram() function as follow:
https://mpatacchiola.github.io/blog/2016/11/12/the-simplest-classifier-histogram-intersection.html 6/12
2020/9/6 The Simplest Classifier: Histogram Comparison
To check the result of the comparison we can print the values stored
in the comparison_array :
0.00818883 #Flash
0.55411926 #Batman
0.12405966 #Hulk
0.07735263 #Superman
0.34388389 #Capt. America
0.12672027 #Wonder Woman
0.09870308 #Iron Man
0.2225694 #Wolverine
As you can see the second value (~0.55) is the highest one, meaning
that the second model (Batman) has the best match with the input
image. This value is the raw distance, to obtain a probability
distribution we have to normalise the array dividing it by the sum
of the values:
0.00526411 #Flash
0.35621003 #Batman
0.07975051 #Hulk
0.04972536 #Superman
0.22106232 #Capt. America
0.08146086 #Wonder Woman
0.06345029 #Iron Man
0.14307651 #Wolverine
https://mpatacchiola.github.io/blog/2016/11/12/the-simplest-classifier-histogram-intersection.html 8/12
2020/9/6 The Simplest Classifier: Histogram Comparison
Also in this case we got a good result (26.6%). What happens if the
image is in a different position and there are some additional
colours respect to the model? We can try with Wonder Woman:
are small change in the object perspective and when there is noise
in the background. If you want you can test the other images
For all the
downloading the example from the deepgaze repository.
images the highest value identified the correct superhero. In
their article Swain and Ballard tested the histogram intersection on
a dataset containing 66 models. For the 66-object dataset the
correct model was the best matches 29 of 32 times and in the other
3 cases the correct model was the second highest match. If you have
the occasion to test the model with larger datasets please share
your results in the comments below.
Acknowledgments
The superheroes images are courtesy of Christopher Chong.
References
Swain, M. J., & Ballard, D. H. (1991). Color indexing. International
journal of computer vision, 7(1), 11-32.
ALSO ON MPATACCHIOLA