OpenCV 3.0 Connected components, can't get it to work properly
<p>I have the following code, it's just a simple test program to learn how to use the connected components functionality in openCV 3.0</p>

<pre><code>int main(int argc, char** argv) {

char* line = argv[1];

Mat image;

image = imread(line,CV_LOAD_IMAGE_GRAYSCALE);   

cv::Mat label=Mat(image.size(),CV_16U);

int la=connectedComponents(image,label, 8,CV_16U);

    //tried also: label.convertTo(label,CV_8U,255);   
   // and label.convertTo(label,CV_16U,255);

namedWindow( "input", CV_WINDOW_AUTOSIZE );
imshow( "input", image);

    namedWindow( "ouput", CV_WINDOW_AUTOSIZE );
    imshow("output", label);
    cout&lt;&lt;la&lt;&lt;"\n";

    imwrite("output.png", label);
    waitKey(0);
    return 0;
    }
</code></pre>

<p>The input image is a color image with two red squares on a white background.
The image is correctly loaded and displayed as a grayscale image.
<a href="https://i.stack.imgur.com/QdEQY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QdEQY.png" alt="program outoput"></a></p>

<p>The thing is no matter what I do, the output is always a blank image, either black or white, depending on the <code>convertTo</code> paremeters. </p>

<p>However, the value returned by <code>connectedComponents</code> is 2.</p>

<p>I tried the full code proposed by Miki, I get this:</p>

<p><a href="https://i.stack.imgur.com/lMPWD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lMPWD.png" alt="enter image description here"></a></p>

<p>I thought the issue could be that connected Components is not working properly.</p>

<p>Tried with a picture I had on my desktop and finally got something:</p>

<p><a href="https://i.stack.imgur.com/tqLGf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tqLGf.png" alt="enter image description here"></a></p>

<p>However, this time the source image is a regular picture with people, buildings, cars...and most of the output is still blank. Does anybody know why?</p>

<p>After adding <code>image = image &lt; 200;</code></p>

<p><a href="https://i.stack.imgur.com/ZQ6qf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZQ6qf.png" alt="enter image description here"></a></p>

<p>With <code>applyColorMap(seeMyLabels, seeMyLabels, COLORMAP_JET);</code>, the labels image goes from almost black grayscale to blue shades</p>

<p><a href="https://i.stack.imgur.com/uOvYK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uOvYK.png" alt="enter image description here"></a></p>