title: Cut the shapes out of the image with EMGU CV
body: <p>All good times of day!</p>
  <p>Faced with the following task: you need to find the shapes in the image, cut them out and <strong>save them in jpg or png.</strong></p>
  <p>Find the shapes worked out (attached the image), but how do I get their extreme coordinates?</p>
  <p>I find shapes with EMGU CV.</p>
  <p>I'm taking Image it out of PictureBox.</p>
  <p>// - was like the idea to find the distance from the center to the edge of the figure through Moments, but did not understand how they work</p>
  <pre><code>Image&lt;Gray, byte&gt; grayImage = inputImage.SmoothMedian(1).Convert&lt;Gray, byte&gt;().ThresholdBinaryInv(new Gray(230), new Gray(255));
   
  VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
   
  Mat hierarchy = new Mat();
   
  for (int i = 0; i &lt; contours.Size; i++)
  {
      double perimetr = CvInvoke.ArcLength(contours[i], true); 
      
      VectorOfPoint approximation = new VectorOfPoint();
      
      CvInvoke.ApproxPolyDP(contours[i], approximation, 0.04 * perimetr, true); 
   
   
      if (approximation.Size &gt;= 3 &amp;&amp; perimetr &gt; 100 &amp;&amp; approximation.Size != 5)
      {
          CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2); 
      }
   
                  
      Moments moments = CvInvoke.Moments(contours[i]);
   
      int x = (int)(moments.M10 / moments.M00);
      int y = (int)(moments.M01 / moments.M00);
                  
      if (perimetr &gt; 100)
      {
          if (approximation.Size == 3)
          {
              CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
              CvInvoke.PutText(inputImage, &quot;Triangle&quot;, new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
                  new MCvScalar(0, 0, 255), 1);
          } 
          if (approximation.Size == 4)
          {
              Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
   
              double aspectRatio = (double)rect.Width / (double)rect.Height;
   
              if (aspectRatio &gt;= 0.95 &amp;&amp; aspectRatio &lt;= 1.05)
              {
                  CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2); 
                  CvInvoke.PutText(inputImage, &quot;Square&quot;, new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
                      new MCvScalar(0, 0, 255), 1);
              }
              else
              {
                  CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
                  CvInvoke.PutText(inputImage, &quot;Rectangle&quot;, new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
                      new MCvScalar(0, 0, 255), 1);
              }
          }
          if (approximation.Size == 5)
          {
              CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2); 
              CvInvoke.PutText(inputImage, &quot;Pentagon&quot;, new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
                  new MCvScalar(0, 0, 255), 1);
          } 
          if (approximation.Size == 6)
          {
              CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
              CvInvoke.PutText(inputImage, &quot;Hexagon&quot;, new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
                  new MCvScalar(0, 0, 255), 1);
          }
          if (approximation.Size &gt; 6)
          {
              var R = (double)perimetr / (3.14 * 2);
              if (R &gt; (Math.Min(inputImage.Width, inputImage.Height) / 3) * 0.1 &amp;&amp; R &lt; (Math.Min(inputImage.Width, inputImage.Height) / 2))
              {
                  CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2); 
                  CvInvoke.PutText(inputImage, &quot;Circle&quot;, new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
                      new MCvScalar(0, 0, 255), 1);
              }
          } 
      }
  }
  </code></pre>
  <p>Thank you for your attention.</p>
  <p><a href="https://i.stack.imgur.com/As98c.png" rel="nofollow noreferrer">Input image</a></p>
  <p><a href="https://i.stack.imgur.com/BLOwD.png" rel="nofollow noreferrer">Output image</a></p>