title: how to set initial centers of K-means openCV c++
body: <p>I am trying to do a segmentation  of an image  using OpenCv and Kmeans, the code that I  have just implemented is the following:</p>
  
  <pre><code>#include "opencv2/objdetect/objdetect.hpp"
  #include "opencv2/highgui/highgui.hpp"
  #include "opencv2/imgproc/imgproc.hpp"
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  
  using namespace std;
  using namespace cv;
  
  int main(int, char** argv)
  {
      Mat src, Imagen2, Imagris, labels, centers,imgfondo;
      src = imread("C:/Users/Sebastian/Documents/Visual Studio 2015/Projects/ClusteringImage/data/leon.jpg");
      imgfondo = imread("C:/Users/Sebastian/Documents/Visual Studio 2015/Projects/ClusteringImage/data/goku640.jpg");
      if (src.empty()|| imgfondo.empty())
      {
          printf("Error al cargar imagenes");
          waitKey();
          return -1;
      }
      Mat samples(src.rows * src.cols, 3, CV_32F);
      for (int y = 0; y &lt; src.rows; y++)
          for (int x = 0; x &lt; src.cols; x++)
              for (int z = 0; z &lt; 3; z++)
                  samples.at&lt;float&gt;(y + x*src.rows, z) = src.at&lt;Vec3b&gt;(y, x)[z];
      //KMEANS_USE_INITIAL_LABELS
      kmeans(samples, 2, labels, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0), 3, KMEANS_USE_INITIAL_LABELS, centers);
      Mat new_image(src.size(), src.type());
      int cluster;
      if (centers.at&lt;float&gt;(0, 1) &gt; centers.at&lt;float&gt;(1, 1))  cluster = 0;
      else        cluster = 1;
      for (int y = 0; y &lt; src.rows; y++)
          for (int x = 0; x &lt; src.cols; x++)
          {
              int cluster_idx = labels.at&lt;int&gt;(y + x*src.rows, 0);
              if (cluster_idx == cluster)
              {
                  new_image.at&lt;Vec3b&gt;(y, x)[0] = imgfondo.at&lt;Vec3b&gt;(y, x)[0];
                  new_image.at&lt;Vec3b&gt;(y, x)[1] = imgfondo.at&lt;Vec3b&gt;(y, x)[1];
                  new_image.at&lt;Vec3b&gt;(y, x)[2] = imgfondo.at&lt;Vec3b&gt;(y, x)[2];
              }
              else
              {
                  new_image.at&lt;Vec3b&gt;(y, x)[0] = src.at&lt;Vec3b&gt;(y, x)[0];
                  new_image.at&lt;Vec3b&gt;(y, x)[1] = src.at&lt;Vec3b&gt;(y, x)[1];
                  new_image.at&lt;Vec3b&gt;(y, x)[2] = src.at&lt;Vec3b&gt;(y, x)[2];
              }
          }
      imshow("Original image", src);
      imshow("clustered image", new_image);
      waitKey();
  }
  </code></pre>
  
  <p>It works pretty well and it does what i want, but i would like to  set my own initial centers values. I have read that it could be done using the flag "KMEANS_USE_INITIAL_LABELS" but im not very sure about how to use it, and how and where should I set the initial values.
  thanks.</p>