Goal 
In this tutorial you will learn how to:
Theory 
Code 
This tutorial code's is shown lines below. You can also download it from here 
#include <iostream>
int thresh = 100;
int max_thresh = 255;
void thresh_callback(int, void* );
int main( int argc, char** argv )
{
  String imageName(
"../data/happyfish.jpg"); 
   if (argc > 1)
  {
    imageName = argv[1];
  }
  {
    cerr << "No image supplied ..." << endl;
    return -1;
  }
  const char* source_window = "Source";
  createTrackbar( 
" Canny thresh:", 
"Source", &thresh, max_thresh, thresh_callback );
   thresh_callback( 0, 0 );
  return(0);
}
void thresh_callback(int, void* )
{
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;
  Canny( src_gray, canny_output, thresh, thresh*2, 3 );
   for( size_t i = 0; i< contours.size(); i++ )
     {
       Scalar color = 
Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
      }
  imshow( 
"Contours", drawing );
 }
Explanation 
Result 
Here it is: