title: Remove connected components from image which have a sum of pixel values < threshold
body: <p>In simple words, this is an example of input available and the corresponding output I want: </p>
  
  <pre><code>In: [ 0 0 0 0 0 0 1 0 1 0
        0 6 6 0 0 0 0 1 1 0
        0 8 9 0 0 0 0 0 1 1
        8 0 0 0 9 9 0 0 0 0
        0 0 0 0 8 8 0 0 0 0
        0 0 0 0 0 0 0 0 0 0
        9 9 7 0 0 0 0 0 1 0
        0 6 8 0 0 0 0 3 2 0
        0 0 0 0 0 0 0 2 1 0
        0 0 0 0 0 0 0 0 0 0 ]
  </code></pre>
  
  <p>After binarizing and obtaining labelled image using <strong>cv2.connectedComponents()</strong>:</p>
  
  <pre><code>labels: [ 0 0 0 0 0 0 1 0 1 0
            0 2 2 0 0 0 0 1 1 0
            0 2 2 0 0 0 0 0 1 1
            2 0 0 0 3 3 0 0 0 0
            0 0 0 0 3 3 0 0 0 0
            0 0 0 0 0 0 0 0 0 0
            4 4 4 0 0 0 0 0 5 0
            0 4 4 0 0 0 0 5 5 0
            0 0 0 0 0 0 0 5 5 0
            0 0 0 0 0 0 0 0 0 0 ]
  </code></pre>
  
  <p>From here, I want the following output:</p>
  
  <pre><code>Out: [0 0 0 0 0 0 0 0 0 0
        0 6 6 0 0 0 0 0 0 0
        0 8 9 0 0 0 0 0 0 0
        8 0 0 0 9 9 0 0 0 0
        0 0 0 0 8 8 0 0 0 0
        0 0 0 0 0 0 0 0 0 0
        9 9 7 0 0 0 0 0 0 0
        0 6 8 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 ]
  </code></pre>
  
  <p>There are many connected components present (in this case: 5 components of 8-connectivity). The components (of <strong>In</strong>) which have their sum of pixels &lt; 10 (a threshold) are removed in <strong>Out</strong>. </p>
  
  <p>How do I implement this in C++ (possibly using OpenCV)?</p>
  
  <p>I have already done it on Python using  OpenCV but not able to implement on C++. </p>
  
  <p>This is a part of my Python code, if useful (<strong>labels</strong> is the output of <strong>cv2.connectedComponents()</strong> ):</p>
  
  <pre><code>for cnt in range(1, labels.max()+1): 
          idx = np.where(labels.copy() == cnt)
          valMat = In[idx]
          sum_valMat = sum(valMat)
          if sum_valMat &gt; 3000:
              fingerNodes[idx] = 0
  </code></pre>
  
  <p>Input is a simple 2D matrix. This is an example of connected component:</p>
  
  <p><a href="https://i.stack.imgur.com/RdT8N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RdT8N.png" alt="enter image description here"></a></p>