title: Removing outlier pixels from a small binary image
body: <p>I am currently implementing an algorithm for identifying the axis of minimum inertia of a colored mass (provided by the second moments). In order to do so, I need to acquire the centre of mass, as given by the first moments.</p>
  
  <p>The weighted averaging function works well, but due to outlier pixels, I am receiving undesired results.</p>
  
  <p>Here is the averaging function:</p>
  
  <p>(e.g. x's weighted average)</p>
  
  <pre><code>for (i = 0, i &lt; rows, i++) {
      for (j = 0, j &lt; cols, j++) {
          if (colorAt(i,j).isForeground()) {
              tempSumX++;
              totalForeground++;
          }
      }
      x_ += i*tempSumX;
      tempSumX = 0;
  }
  x_ /= totalForeground; //where x_ represents the x coordinate of the weighted center of mass.
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ihbtI.png" alt="Incorrect Center of Mass"></p>
  
  <p>Given an image such as this, which is represented by exclusively two colors (background and foreground), how can I remove outlying pixels? Note: Outlying pixels refers to anything not part of the big color-mass. The white dot is the calculated center of mass, which is incorrect.</p>
  
  <p>Much appreciated.</p>