This program demonstrates connected components and use of the trackbar
#include <iostream>
int threshval = 100;
static void on_trackbar(int, void*)
{
    Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
     std::vector<Vec3b> colors(nLabels);
    colors[0] = 
Vec3b(0, 0, 0);
    for(int label = 1; label < nLabels; ++label){
        colors[label] = 
Vec3b( (rand()&255), (rand()&255), (rand()&255) );
    }
    for(int r = 0; r < dst.rows; ++r){
        for(int c = 0; c < dst.cols; ++c){
            int label = labelImage.
at<
int>(r, c);
             pixel = colors[label];
         }
     }
    imshow( 
"Connected Components", dst );
 }
int main( int argc, const char** argv )
{
    CommandLineParser parser(argc, argv, 
"{@image|stuff.jpg|image for converting to a grayscale}");
     parser.
about(
"\nThis program demonstrates connected components and use of the trackbar\n");
    cout << "\nThe image is converted to grayscale and displayed, another image has a trackbar\n"
            "that controls thresholding and thereby the extracted contours which are drawn in color\n";
    if(img.empty())
    {
        cout << "Could not read input image file: " << inputImage << endl;
        return EXIT_FAILURE;
    }
    createTrackbar( 
"Threshold", 
"Connected Components", &threshval, 255, on_trackbar );
     on_trackbar(threshval, 0);
    return EXIT_SUCCESS;
}