我有以下图片:
https://www.upsieutoc.com/images/2014/10/18/binkq27dd30.png
我想计算我的图片中有多少个圈子。我的图像是nxn 8位二进制图像,而不是0和1。 那么,我该怎么办? 谢谢你的阅读!
答案 0 :(得分:2)
缩减版: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数组,所以你可以看到两个逻辑。