using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
for (Contour<Point> contours = grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
{
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
canny.Draw(new Rectangle(currentContour.BoundingRectangle.X, currentContour.BoundingRectangle.Y, currentContour.BoundingRectangle.Width, currentContour.BoundingRectangle.Height), new Gray(1), 1);
canny.Draw(contours, new Gray(), 2);
}
这段代码给了我边界框,但不符合标记,在某些情况下轮廓超出了盒子范围。 无法共享示例输出图像,因为它需要 10 个信誉 如果有人有解决方案请回复! 谢谢。
最佳答案
Image<Gray, Byte> canny = new Image<Gray, byte>(grayImage.Size);
using (MemStorage storage = new MemStorage())
for (Contour<Point> contours =grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, storage); contours != null; contours = contours.HNext)
{
//Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
CvInvoke.cvDrawContours(canny, contours, new MCvScalar(255), new MCvScalar(255), -1, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));
}
using (MemStorage store = new MemStorage())
for (Contour<Point> contours1= grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, store); contours1 != null; contours1 = contours1.HNext)
{
Rectangle r = CvInvoke.cvBoundingRect(contours1, 1);
canny.Draw(r, new Gray(255), 1);
}
这个方法给了我完美的边界框!
关于c# - 查找轮廓的边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22473528/