【问题标题】:Extract circle area from an image in EMGUCV C#从EMGUCV C#中的图像中提取圆形区域
【发布时间】:2019-11-05 14:31:50
【问题描述】:

我使用: Emgucv 4.0.1 Opencv 4.1.0

我有一系列用 HoughCircles 函数检测到的圆圈,我需要一个一个分析。 我需要计算圆形绿色圆圈中有多少颜色,所以我必须只提取圆形图像,但我知道如何只提取比圆圈包含更多像素的框。如何仅提取包围的绿色区域内的图像? 请参阅下面链接中的图片。

1) Source Image

2) Boxed image I retrieved with more pixel than i want

3) Image that I would like to extract

// m_ListCircles = list from HoughCircles() circles coordinates.
// cell = cell number to extract.
// pictureBox1 = main picturebox with all circles detected.
// pictureBoxROI = picturebox destination single circles cutted.

int id = (int)m_ListCircles[0 + cell ];
int x = (int)m_ListCircles[1 + cell ];
int y = (int)m_ListCircles[2 + cell ];
int r = (int)m_ListCircles[3 + cell ]; // radius

// box area around the circle
int X0 = x;
int Y0 = y;
int X1 = x + r * 2;
int Y1 = y + r * 2;

// area to copy
int wid = Math.Abs(X0 - X1);
int hgt = Math.Abs(Y0 - Y1);

if ((wid < 1) || (hgt < 1)) return;

// create a rectangle are to copy
Rectangle source_rectangle = new Rectangle(Math.Min(X0, X1),Math.Min(Y0,Y1), wid, hgt);

// assign the area copied to image var
var image = new Image<Bgr, byte>(new Bitmap(pictureBox1.Image));

image.ROI = source_rectangle;

// show image
pictureBoxROI.Image = image.Bitmap;
pictureBoxROI.Refresh();

/*
// tried this but result is always a black image.
Point xyCell = new Point();
xyCell.X = X0;
xyCell.Y = Y0;

Image<Gray, byte> mask = new Image<Gray, byte>(image.Width, image.Height);
CvInvoke.Circle(mask, xyCella, r, new MCvScalar(255, 255, 255), -1, 
LineType.AntiAlias, 0);
Image<Bgr, byte> dest = new Image<Bgr, byte>(image.Width, image.Height);
dest = image.And(image, mask);

pictureBoxROI.Image = dest.Bitmap;
pictureBoxROI.Refresh();
*/

【问题讨论】:

标签: c# emgucv area detect


【解决方案1】:

您始终可以从找到的圆圈中创建 ROI 蒙版,并像这样分析图像

Custom ROI - 这显示了如何使用掩码

【讨论】:

  • 有趣的建议,即使它看起来像是 c++。
  • Emgu 是 c++ OpenCV 的包装器,工作原理几乎完全相同
【解决方案2】:

您只能拥有矩形图像。但是,您可以在切割矩形后将圆外的所有像素设置为透明。
您可以通过使用毕达哥拉斯计算它们与图像中心点的距离来确定哪些像素在圆外。 这当然非常慢,因为您必须遍历所有像素,但对于低像素计数,它相当快。

try
{
    Image rectCroppedImage = originalImage.Clone(CropRect, originalImage.PixelFormat);
    double r = rectCroppedImage.Height; // because you are centered on your circle

    Bitmap img = new Bitmap(rectCroppedImage);

    for (int x = 0; x < img.Width; x++)
    {
        for (int y = 0; y < img.Height; y++)
        {
            // offset to center
            int virtX = x - img.Width / 2;
            int virtY = y - img.Height / 2;

            if (Math.Sqrt(virtX * virtX + virtY * virtY) > r)
            {
                img.SetPixel(x, y, Color.Transparent);
            }
        }
    }
    return img; // your circle cropped image
}
catch (Exception ex)
{
}

这也可以通过使用蒙版并将图像与白色圆圈“相乘”来实现。例如,可以使用图像魔术来实现这样的事情。您可以在此处找到 ImageMagick NuGet 数据包:https://github.com/dlemstra/Magick.NET

【讨论】:

  • 非常感谢您的代码!它完美地工作。对不起,我在公共分数方面没有太多声誉。
  • @user3512156 您仍然可以接受答案。当您到达适当的代表时,您的赞成票将被保存并发出。
猜你喜欢
  • 2023-03-22
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
相关资源
最近更新 更多