title: Extract OpenCV Contour Array from Blob Detection in C++
body: <p>I'm playing around with blob detection techniques in C++ and OpenCV but I'm having trouble extracting the contour information in a usable format.</p>
  
  <p>Currently I have all contour data stored as an array in a <code>cv::Point</code> format; however, I wish to extract the coordinates and store them in an array under a variable so I can manipulate this data as needed.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>#include "opencv2/highgui/highgui.hpp"
  #include "opencv2/opencv.hpp"
  #include &lt;iostream&gt;
  #define _USE_MATH_DEFINES
  #include &lt;math.h&gt;
  
  using namespace cv;
  using namespace std;
  
  int main(int argc, const char** argv)
  {
      cv::Mat src = cv::imread("frame-1.jpg");
      if (src.empty())
          return -1;
  
      cv::Mat gray;
      cv::cvtColor(~src, gray, CV_BGR2GRAY);
  
      cv::threshold(gray, gray, 160, 255, cv::THRESH_BINARY);
  
      // Find all contours
      std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours;
      cv::findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  
      // Fill holes in each contour
      cv::drawContours(gray, contours, -1, CV_RGB(255, 255, 255), -1);
  
      cout &lt;&lt; contours.size();
  
  std::vector&lt;cv::Point&gt; minXY(contours.size()); // Top Left Point of Bounding Box
  std::vector&lt;cv::Point&gt; maxXY(contours.size()); // Bottom Right Point of Bounding Box
  for (int i = 0; i &lt; contours.size()){
      minXY[i] = maxXY[i] = contours[i][0];  // Assumes the contour has at least 1 point
      for(int j = 1; j &lt; contours[i].size(); ++j){
           if (contours[i][j].x &lt; minXY[i].x){
              minXY[i].x = contours[i][j].x;
           } else if (contours[i][j].x &gt; maxXY[i].x){
              maxXY[i].x = contours[i][j].x;
           }
           if (contours[i][j].y &lt; minXY[i].y){
              minXY[i].y = contours[i][j].y;
           } else if (contours[i][j].y &gt; maxXY[i].y){
              maxXY[i].y = contours[i][j].y;
           }
      }
  }
  
      namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
      imshow("MyWindow", gray);
  
      waitKey(0);
  
      destroyWindow("MyWindow");
  
      return 0;
  }
  </code></pre>
  
  <p>The result of <code>contours[2]</code> gives an array with contour coordinates of the third blob; however, I want to extract this array into a normal variable so I can actually use it. I'm assuming this means some sort of <code>cv::Point</code> conversion?</p>
  
  <p>UPDATE: I should probably clarify that I'd like the coordinates of largest blob's contour so that I can manipulate the data such as finding the smallest x point, finding the average x point, find the centroid of the blob etc.</p>
  
  <p>Currently I have been able to find all the contours and then identify the largest blob, as well as identify its contour coordinates. However, I haven't found a way to search through this array to find the least x value, or a method to sum just the x coordinates. I would ultimately also like to find the most efficient method of finding the blob's centroid (using bounding box, moments, simple arithmetic or other method.)</p>
  
  <p>** contours[2] spits out an array in the form:</p>
  
  <blockquote>
    <p>[100, 267] <br>
    [101, 270] <br>
    [102, 271] <br></p>
  </blockquote>
  
  <p>I want to find a way to search the left column (ie. all the x values) to find the smallest and largest etc. I've tried several methods but none have gotten me close. Surely there's an easy solution to this. It seems all my problems stem from the fact that the contour array is in the form of cv::Point.</p>