1

我编写了一个代码来从图像中定位矩形(我可以在图像本身上绘制矩形)。我现在要做的是获取图像上矩形对象的坐标。我正在使用 Rectangle 类来获取矩形信息。有没有办法我可以做到这一点?

这是我使用的代码

private void ProcessBW(Bitmap bitmap)
{
BlobCounter blobCounter = new BlobCounter();

blobCounter.FilterBlobs = true;
//blobCounter.MinHeight = 6;
//blobCounter.MinWidth = 6;

blobCounter.ProcessImage(bitmap);

// create Image Object using rear image byte[]
System.Drawing.Image imageR = (System.Drawing.Image)bitmap;

// Derive BitMap object using Image instance, so that you can avoid the issue
//"a graphics object cannot be created from an image that has an indexed pixel format"
Bitmap img = new Bitmap(new Bitmap(imageR));

Rectangle[] rects = blobCounter.GetObjectsRectangles();
//Graphics object to draw
Pen pen = new Pen(Color.Red, 2);
Graphics g = Graphics.FromImage(img);                  

foreach (Rectangle rect in rects)
{
    g.DrawRectangle(pen, rect);
}
pictureBox1.Image = img;
pen.Dispose();
g.Dispose();
}

谢谢

4

1 回答 1

0

我设法检索了矩形的坐标。这对我来说很愚蠢,而且我对 C# 还是很陌生。矩形类提供了诸如 x、y、宽度和高度组件之类的方法,这些组件可以为我提供矩形的位置

于 2012-08-08T14:40:18.997 回答