title: clustering image segments in opencv
body: <p>I am working on motion detection with non-static camera using opencv.
  I am using a pretty basic background subtraction and thresholding approach to get a broad sense of all that's moving in a sample video. After thresholding, I enlist all separable "patches" of white pixels, store them as independent components and color them randomly with red, green or blue. The image below shows this for a football video where all such components are visible.
  <img src="https://i.stack.imgur.com/FSIBh.png" alt="Moving segments"></p>
  
  <p>I create rectangles over these detected components and I get this image:</p>
  
  <p><img src="https://i.stack.imgur.com/5FRPO.png" alt="Original Image"></p>
  
  <p>So I can see the challenge here. I want to cluster all the "similar" and close-by components into a single entity so that the rectangles in the output image show a player moving as a whole (and not his independent limbs). I tried doing K-means clustering but since ideally I would not know the number of moving entities, I could not make any progress.</p>
  
  <p>Please guide me on how I can do this. Thanks</p>