title: Finding the Average Color within a Polygon Bound in OpenCV
body: <h2>Background</h2>
  
  <p>I am trying to create a utility that will return the average pixel color within a given polygon using <code>OpenCV</code>. The polygon will be defined via 4 points, but it is not necessarily a rectangle/square. For example, the following structures are to be expected:</p>
  
  <pre><code>     A__________B    A_______B
      /          /     \       \
     /          /       \       \
   D/__________/C       D\_______\C
  </code></pre>
  
  <hr>
  
  <p>Given a <code>cv::Mat</code> image in OpenCV and a polygon defined by the points (A, B, C, D). I know points A, B, C, and D, but I want to calc the average pixel color within the polygon. I wanted to get some suggestions from the OpenCV community on how to do this most efficiently.</p>
  
  <hr>
  
  <h2>Research Done</h2>
  
  <p>Another <a href="https://stackoverflow.com/a/17956767/3342427">post</a> on StackOverflow suggested drawing contours using the <a href="http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=drawcontours#drawcontours" rel="noreferrer"><code>drawContours</code></a> function and then take the <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#mean" rel="noreferrer"><code>mean</code></a> of the bounding rectangle surrounding the contour. I would obviously have to modify the mean calculation so that it uses the polygons drawn by the <a href="http://docs.opencv.org/modules/core/doc/drawing_functions.html#fillpoly" rel="noreferrer"><code>fillPoly</code></a> function instead.</p>
  
  <p>Suggestions/Concerns are much appreciated!</p>