title: Finding the coordinates of the points along the contour using OpenCV and C++
body: <p>I am new to using OpenCV and C++, I have process a picture and extract the contour in the picture. Then I draw the contour on a new black frame. Specifically, I draw the contour with the colour red. I would like to get the coordinates of each and all the red pixels in the black frame and store it in an array. I need help on this part, codes are preferred. Thanks in advance.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  #include &lt;sstream&gt;
  
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2\core\core.hpp&gt;
  #include &lt;opencv2\highgui\highgui.hpp&gt;
  #include &lt;opencv2\imgproc\imgproc.hpp&gt;
  #include &lt;opencv2/objdetect/objdetect.hpp&gt;
  
  using namespace cv;
  using namespace std;
  
  void on_trackbar(int, void*);
  void createTrackbars();
  void toggle(int);
  
  const int MAX_NUM_OBJECTS = 500;
  
  const int FRAME_WIDTH = 900;
  const int FRAME_HEIGHT = 600;
  
  const int MIN_OBJECT_AREA = 20 * 20;
  const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH / 1.5;
  
  Point middle;
  
  int l_MIN = 30;
  int l_MAX = 165;
  int a_MIN = 139;
  int a_MAX = 165;
  int b_MIN = 136;
  int b_MAX = 172;
  
  int kerode = 2;
  int kdilate = 8;
  
  bool showchangedframe = true;
  
  int main(int argc, char** argv)
  {
      createTrackbars();
      on_trackbar(0, 0);
  
      int x, y;
      Mat frame, labframe, rangeframe;
      Mat newframe, newrf;
      int key;
  
      while ((key = waitKey(30)) != 27)
      {
          toggle(key);
          frame = imread(argv[1], 1);
          newframe = imread(argv[1], 1);
          dframe = imread(argv[1], 1);
          newframe = Scalar(0, 0, 0);
          dframe = Scalar(0, 0, 0);
          cvtColor(frame, labframe, COLOR_BGR2Lab);
          inRange(labframe, Scalar(l_MIN, a_MIN, b_MIN), Scalar(l_MAX, a_MAX, b_MAX), rangeframe);
  
          erode(rangeframe, rangeframe, getStructuringElement(MORPH_RECT, Size(kerode, kerode)));
          dilate(rangeframe, rangeframe, getStructuringElement(MORPH_RECT, Size(kdilate, kdilate)));
  
          newrf = rangeframe.clone();
  
          int largest_area = 0;
          int largest_contour_index = 0;
          vector&lt;vector&lt;Point&gt; &gt; contours;
  
          findContours(newrf, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
  
          vector&lt;Moments&gt; mu(contours.size()); //get moments
          for (int i = 0; i &lt; contours.size(); i++)
          {
              mu[i] = moments(contours[i], false);
          }
  
          vector&lt;Point2f&gt; mc(contours.size()); //get centers
          for (int i = 0; i &lt; contours.size(); i++)
          {
              mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
          }
  
          for (int i = 0; i &lt; contours.size(); i++) //iterate through each contour. 
          {
              double a = contourArea(contours[i], false); //Find the area of contour
  
              if (a&gt;largest_area)
              {
                  largest_area = a;
                  largest_contour_index = i; //Store the index of largest contour
              }
          }
  
          drawContours(newframe, contours, largest_contour_index, CV_RGB(255, 0, 0), 4);
          circle(newframe, mc[largest_contour_index], 5, CV_RGB(255, 255, 0), -1, 8, 0);
  
          imshow("Detected", newframe);
  
          if (showchangedframe)
              imshow("Camera", frame);
          else
              imshow("Camera", rangeframe);
      }
  }
  
  void on_trackbar(int, void*)
  {
      if (kerode == 0)
          kerode = 1;
      if (kdilate == 0)
          kdilate = 1;
  }
  
  void createTrackbars()
  {
      String trackbarWindowName = "TrackBars";
      namedWindow(trackbarWindowName, WINDOW_NORMAL);
      createTrackbar("l_MIN", trackbarWindowName, &amp;l_MIN, l_MAX, on_trackbar);
      createTrackbar("l_MAX", trackbarWindowName, &amp;l_MAX, l_MAX, on_trackbar);
      createTrackbar("a_MIN", trackbarWindowName, &amp;a_MIN, a_MAX, on_trackbar);
      createTrackbar("a_MAX", trackbarWindowName, &amp;a_MAX, a_MAX, on_trackbar);
      createTrackbar("b_MIN", trackbarWindowName, &amp;b_MIN, b_MAX, on_trackbar);
      createTrackbar("b_MAX", trackbarWindowName, &amp;b_MAX, b_MAX, on_trackbar);
      createTrackbar("Erosion", trackbarWindowName, &amp;kerode, 31, on_trackbar);
      createTrackbar("Dilation", trackbarWindowName, &amp;kdilate, 31, on_trackbar);
  }
  
  void toggle(int key)
  {
      if (key == 'r')
          showchangedframe = !showchangedframe;
  }
  </code></pre>
  
  <p>Here is the output:</p>
  
  <p><img src="https://i.stack.imgur.com/gTQLI.jpg" alt="Output"></p>