title: Opencv: Get all objects from segmented colorful image
body: <p>How to get all objects from image i am separating image objects through colors.
  There are almost 20 colors in following image. I want to extract all colors and their position in a vector(Vec3b and Rect).</p>
  
  <p>I'm using egbis algorithum for segmentation </p>
  
  <p><img src="https://i.stack.imgur.com/2XL8S.jpg" alt="Input Image"></p>
  
  <p>Segmented image</p>
  
  <p><a href="https://i.stack.imgur.com/WIMpj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WIMpj.jpg" alt="enter image description here"></a></p>
  
  <pre><code>Mat src, dst;
    String imageName("/home/pathToImage.jpg" );
    src = imread(imageName,1);
    if(src.rows &lt; 1)
        return -1;
    for(int i=0; i&lt;src.rows; i=i+5)
    {    for(int j=0; j&lt;src.cols; j=j+5)
        {
            Vec3b color = src.at&lt;Vec3b&gt;(Point(i,j));
            if(colors.empty())
              {
                  colors.push_back(color);
              }
            else{
                bool add = true;
              for(int k=0; k&lt;colors.size(); k++)
              {
                   int     rmin = colors[k].val[0]-5,
                    rmax = colors[k].val[0]+5,
                    gmin = colors[k].val[1]-5,
                    gmax = colors[k].val[1]+5,
                    bmin = colors[k].val[2]-5,
                    bmax = colors[k].val[2]+5;
                  if((
                          (color.val[0] &gt;= rmin &amp;&amp; color.val[0] &lt;= rmax) &amp;&amp;
                          (color.val[1] &gt;= gmin &amp;&amp; color.val[1] &lt;= gmax) &amp;&amp;
                          (color.val[2] &gt;= bmin &amp;&amp; color.val[2] &lt;= bmax))
  
                  )
                  {
                      add = false;
                      break;
                  }
  
              }
              if(add)
                  colors.push_back(color);
  
            }
        }
    }
    int size = colors.size();
    for(int i=0; i&lt;colors.size();i++)
    {
        Mat inrangeImage;
        //cv::inRange(src, Scalar(lowBlue, lowGreen, lowRed), Scalar(highBlue, highGreen, highRed), redColorOnly);
        cv::inRange(src, cv::Scalar(colors[i].val[0]-1, colors[i].val[1]-1, colors[i].val[2]-1), cv::Scalar(colors[i].val[0]+1, colors[i].val[1]+1, colors[i].val[2]+1), inrangeImage);
        imwrite("/home/kavtech/Segmentation/1/opencv-wrapper-egbis/images/inrangeImage.jpg",inrangeImage);
    }
    /// Display
    namedWindow("Image", WINDOW_AUTOSIZE );
    imshow("Image", src );
    waitKey(0);
  </code></pre>
  
  <p>I want to get each color position so that 
  i can differentiate object positions. Please Help!</p>