All good times of day!
Faced with the following task: you need to find the shapes in the image, cut them out and save them in jpg or png.
Find the shapes worked out (attached the image), but how do I get their extreme coordinates?
I find shapes with EMGU CV.
I'm taking Image it out of PictureBox.
// - 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
Image<Gray, byte> grayImage = inputImage.SmoothMedian(1).Convert<Gray, byte>().ThresholdBinaryInv(new Gray(230), new Gray(255));
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Mat hierarchy = new Mat();
for (int i = 0; i < 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 >= 3 && perimetr > 100 && 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 > 100)
{
if (approximation.Size == 3)
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Triangle", 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 >= 0.95 && aspectRatio <= 1.05)
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Square", 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, "Rectangle", 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, "Pentagon", 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, "Hexagon", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
if (approximation.Size > 6)
{
var R = (double)perimetr / (3.14 * 2);
if (R > (Math.Min(inputImage.Width, inputImage.Height) / 3) * 0.1 && R < (Math.Min(inputImage.Width, inputImage.Height) / 2))
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Circle", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
}
}
}
Thank you for your attention.
I found a solution to my question. There is a method that returns a description of the contour with an array of points.
Point[] contour = contours[i].ToArray();