@prev_tutorial{tutorial_histogram_comparison} @next_tutorial{tutorial_template_matching}
| | | | -: | :- | | Original author | Ana Huamán | | Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn:
Let's say you have gotten a skin histogram (Hue-Saturation) based on the image below. The
histogram besides is going to be our model histogram (which we know represents a sample of
skin tonality). You applied some mask to capture only the histogram of the skin area:
Now, let's imagine that you get another hand image (Test Image) like the one below: (with its
respective histogram):
What we want to do is to use our model histogram (that we know represents a skin tonality) to detect skin areas in our Test Image. Here are the steps -# In each pixel of our Test Image (i.e. \f$p(i,j)\f$ ), collect the data and find the
correspondent bin location for that pixel (i.e. \f$( h_{i,j}, s_{i,j} )\f$ ).
-# Lookup the model histogram in the correspondent bin - \f$( h{i,j}, s{i,j} )\f$ - and read
the bin value.
-# Store this bin value in a new image (BackProjection). Also, you may consider to normalize
the *model histogram* first, so the output for the Test Image can be visible for you.
-# Applying the steps above, we get the following BackProjection image for our Test Image:
![](images/Back_Projection_Theory4.jpg)
-# In terms of statistics, the values stored in BackProjection represent the probability
that a pixel in *Test Image* belongs to a skin area, based on the *model histogram* that we
use. For instance in our Test image, the brighter areas are more probable to be skin area
(as they actually are), whereas the darker areas have less probability (notice that these
"dark" areas belong to surfaces that have some shadow on it, which in turns affects the
detection).
Downloadable code:
Code at glance: @include samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp @end_toggle
Downloadable code:
Code at glance: @include samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java @end_toggle
Downloadable code:
Code at glance: @include samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py @end_toggle
Read the input image:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Read the image @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Read the image @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Read the image @end_toggle
Transform it to HSV format:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Transform it to HSV @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Transform it to HSV @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Transform it to HSV @end_toggle
For this tutorial, we will use only the Hue value for our 1-D histogram (check out the fancier code in the links above if you want to use the more standard H-S histogram, which yields better results):
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Use only the Hue value @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Use only the Hue value @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Use only the Hue value @end_toggle
as you see, we use the function @ref cv::mixChannels to get only the channel 0 (Hue) from the hsv image. It gets the following parameters:
Create a Trackbar for the user to enter the bin values. Any change on the Trackbar means a call to the Hist_and_Backproj callback function.
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Create Trackbar to enter the number of bins @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Create Trackbar to enter the number of bins @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Create Trackbar to enter the number of bins @end_toggle
Show the image and wait for the user to exit the program:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Show the image @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Show the image @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Show the image @end_toggle
Hist_and_Backproj function: Initialize the arguments needed for @ref cv::calcHist . The number of bins comes from the Trackbar:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp initialize @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java initialize @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py initialize @end_toggle
Calculate the Histogram and normalize it to the range \f$[0,255]\f$
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Get the Histogram and normalize it @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Get the Histogram and normalize it @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Get the Histogram and normalize it @end_toggle
Get the Backprojection of the same image by calling the function @ref cv::calcBackProject
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Get Backprojection @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Get Backprojection @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Get Backprojection @end_toggle
all the arguments are known (the same as used to calculate the histogram), only we add the backproj matrix, which will store the backprojection of the source image (&hue)
Display backproj:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Draw the backproj @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Draw the backproj @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Draw the backproj @end_toggle
Draw the 1-D Hue histogram of the image:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp Draw the histogram @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/Histograms_Matching/back_projection/CalcBackProjectDemo1.java Draw the histogram @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py Draw the histogram @end_toggle
Here are the output by using a sample image ( guess what? Another hand ). You can play with the
bin values and you will observe how it affects the results: