Mat image = CvInvoke.Imread(@"C:\Users\Hello\Pictures\123.jpg", Emgu.CV.CvEnum.ImreadModes.Unchanged);
CvInvoke.Imshow("image", image);
Mat gray = new Mat();
CvInvoke.CvtColor(image, gray, Emgu.CV.CvEnum.ColorConversion.Rgb2Gray);
CvInvoke.Threshold(gray, gray, 200, 255, Emgu.CV.CvEnum.ThresholdType.Otsu | Emgu.CV.CvEnum.ThresholdType.Binary);
CvInvoke.Imshow("gray", gray);
Mat element = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(3, 3), new Point(2, 2));
CvInvoke.Dilate(gray, gray, element, new Point(2, 2), 1, Emgu.CV.CvEnum.BorderType.Default, new Emgu.CV.Structure.MCvScalar(0));
Random rnd = new Random();
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(gray, contours, null, Emgu.CV.CvEnum.RetrType.Tree, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
for (int i = 0; i < contours.Size; i++) {
if (CvInvoke.ContourArea(contours[i]) > 100.0) {
// Rectangle rc = CvInvoke.BoundingRectangle(contours[i]);
// CvInvoke.Rectangle(image, rc, new MCvScalar(0, 0, 255));
CvInvoke.DrawContours(image, contours, i,
new MCvScalar(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)));
}
}
CvInvoke.Imshow("result", image);
CvInvoke.WaitKey();