 |
OpenCV
3.2.0
Open Source Computer Vision
|
.2.0+dfsg_doc_tutorials_imgproc_histograms_back_projection_back_projection
Goal
In this tutorial you will learn:
- What is Back Projection and why it is useful
- How to use the OpenCV function cv::calcBackProject to calculate Back Projection
- How to mix different channels of an image by using the OpenCV function cv::mixChannels
Theory
What is Back Projection?
- Back Projection is a way of recording how well the pixels of a given image fit the distribution of pixels in a histogram model.
- To make it simpler: For Back Projection, you calculate the histogram model of a feature and then use it to find this feature in an image.
- Application example: If you have a histogram of flesh color (say, a Hue-Saturation histogram ), then you can use it to find flesh color areas in an image:
How does it work?
- We explain this by using the skin example:
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. \(p(i,j)\) ), collect the data and find the correspondent bin location for that pixel (i.e. \(( h_{i,j}, s_{i,j} )\) ).
- Lookup the model histogram in the correspondent bin - \(( h_{i,j}, s_{i,j} )\) - 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:
- 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).
Code
- What does this program do?
- Loads an image
- Convert the original to HSV format and separate only Hue channel to be used for the Histogram (using the OpenCV function cv::mixChannels )
- Let the user to enter the number of bins to be used in the calculation of the histogram.
- Calculate the histogram (and update it if the bins change) and the backprojection of the same image.
- Display the backprojection and the histogram in windows.
- Downloadable code:
- Click here for the basic version (explained in this tutorial).
- For stuff slightly fancier (using H-S histograms and floodFill to define a mask for the skin area) you can check the improved demo
- ...or you can always check out the classical camshiftdemo in samples.
- Code at glance:
#include <iostream>
using namespace std;
int bins = 25;
void Hist_and_Backproj(int, void* );
int main( int, char** argv )
{
{ cout<<"Usage: ./calcBackProject_Demo1 <path_to_image>"<<endl;
return -1;
}
int ch[] = { 0, 0 };
const char* window_image = "Source image";
createTrackbar(
"* Hue bins: ", window_image, &bins, 180, Hist_and_Backproj );
Hist_and_Backproj(0, 0);
return 0;
}
void Hist_and_Backproj(int, void* )
{
MatND hist;
int histSize =
MAX( bins, 2 );
float hue_range[] = { 0, 180 };
const float* ranges = { hue_range };
calcHist( &hue, 1, 0,
Mat(), hist, 1, &histSize, &ranges,
true,
false );
MatND backproj;
imshow(
"BackProj", backproj );
int w = 400; int h = 400;
int bin_w =
cvRound( (
double) w / histSize );
for(
int i = 0;
i < bins;
i ++ )
imshow(
"Histogram", histImg );
}
Explanation
- Declare the matrices to store our images and initialize the number of bins to be used by our histogram:
Mat src; Mat hsv; Mat hue;
int bins = 25;
- Read the input image and transform it to HSV format:
- 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):
hue.create( hsv.size(), hsv.depth() );
int ch[] = { 0, 0 };
as you see, we use the function cv::mixChannels to get only the channel 0 (Hue) from the hsv image. It gets the following parameters:
- &hsv: The source array from which the channels will be copied
- 1: The number of source arrays
- &hue: The destination array of the copied channels
- 1: The number of destination arrays
- ch[] = {0,0}: The array of index pairs indicating how the channels are copied. In this case, the Hue(0) channel of &hsv is being copied to the 0 channel of &hue (1-channel)
- 1: Number of index pairs
- 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.
char* window_image = "Source image";
createTrackbar(
"* Hue bins: ", window_image, &bins, 180, Hist_and_Backproj );
Hist_and_Backproj(0, 0);
- Show the image and wait for the user to exit the program:
- Hist_and_Backproj function: Initialize the arguments needed for cv::calcHist . The number of bins comes from the Trackbar:
void Hist_and_Backproj(int, void* )
{
MatND hist;
int histSize =
MAX( bins, 2 );
float hue_range[] = { 0, 180 };
const float* ranges = { hue_range };
- Calculate the Histogram and normalize it to the range \([0,255]\)
calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, &ranges,
true,
false );
- Get the Backprojection of the same image by calling the function cv::calcBackProject 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:
imshow(
"BackProj", backproj );
- Draw the 1-D Hue histogram of the image:
int w = 400; int h = 400;
int bin_w =
cvRound( (
double) w / histSize );
Mat histImg = Mat::zeros( w, h,
CV_8UC3 );
for(
int i = 0;
i < bins;
i ++ )
imshow(
"Histogram", histImg );
Results
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:
@ IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
@ NORM_MINMAX
flag
Definition: base.hpp:196
@ COLOR_BGR2HSV
convert RGB/BGR to HSV (hue saturation value), color conversions
Definition: imgproc.hpp:581
void calcBackProject(const Mat *images, int nimages, const int *channels, InputArray hist, OutputArray backProject, const float **ranges, double scale=1, bool uniform=true)
Calculates the back projection of a histogram.
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
void mixChannels(const Mat *src, size_t nsrcs, Mat *dst, size_t ndsts, const int *fromTo, size_t npairs)
Copies specified channels from input arrays to the specified channels of output arrays.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
bool empty() const
Returns true if the array has no elements.
#define CV_8UC3
Definition: interface.h:84
MatSize size
Definition: mat.hpp:1978
int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition: fast_math.hpp:93
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Scalar_< double > Scalar
Definition: types.hpp:606
Point2i Point
Definition: types.hpp:183
void calcHist(const Mat *images, int nimages, const int *channels, InputArray mask, OutputArray hist, int dims, const int *histSize, const float **ranges, bool uniform=true, bool accumulate=false)
Calculates a histogram of a set of arrays.
n-dimensional dense array class
Definition: mat.hpp:741
for i
Definition: modelConvert.m:63
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
Definition: affine.hpp:52
#define MAX(a, b)
Definition: cvdef.h:414
@ WINDOW_AUTOSIZE
the user cannot resize the window, the size is constrainted by the image displayed.
Definition: highgui.hpp:184
void create(int rows, int cols, int type)
Allocates new array data if needed.
static Vec< _Tp, cn > normalize(const Vec< _Tp, cn > &v)
int depth() const
Returns the depth of a matrix element.