提取矩形的坐标

时间:2012-06-30 14:28:36

标签: c# image-processing aforge

我编写了一个代码来从图像中找到矩形(我可以在图像上绘制矩形形状)。我现在想做的是让图像上的矩形物体的坐标。我使用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();
}

谢谢

1 个答案:

答案 0 :(得分:0)

我设法检索矩形的坐标。这对我很愚蠢,我对C#很新。矩形类提供了诸如x,y,width和height组件之类的方法,这些方法可以为我提供矩形的位置