如何在二值图像RGB中检测圆

本文关键字:检测 RGB 二值图像 | 更新日期: 2023-09-27 18:26:53

我有以下图像:

https://www.upsieutoc.com/images/2014/10/18/binkq27dd30.png

我想数一下我的图像中有多少个圆圈。我的图像是nxn 8位二进制图像,而不是0和1。那么,我能做什么呢?感谢您的阅读!

如何在二值图像RGB中检测圆

的精简版:http://www.codeproject.com/Articles/36378/Optical-Mark-Recognition-with-DotImage

//Create a Bitmap object from an image file.
Bitmap myBitmap = new Bitmap("Answers.jpg");
//Check the pixels in the Bitmap for the circles:
for (int i = 0; i < myBitmap.Width;i++)
{
    for (int j = 0; j < myBitmap.Height;j++)
    {
        Color pixelColor = myBitmap.GetPixel(i, j);
        //Translate pixel to a 1 or 0 depending on if the pixel is black or white
        //This next line is psuedo code:
        boolArray[i][j] = pixelColour.R < 128 && pixelColour.G < 128 && pixelColour.B < 128;
    }
}

然后遍历boolen数组,看看一行中是否有三个或四个1,以及上下,例如:

if (boolArray[i][j] && boolArray[i + 1][j] && boolArray[i + 2][j])
{
   if (boolArray[i][j + 1] && boolArray[i][j + 2] && boolArray[i][j + 3])
   {
    //found an answer marked as a filled in circle
   }
}

注意:您应该在同一个嵌套循环中检查cicles。我只填充了一个多维bool数组,这样您就可以看到这两部分逻辑。