Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接组件标记算法有问题_C#_Algorithm_Components - Fatal编程技术网

C# 连接组件标记算法有问题

C# 连接组件标记算法有问题,c#,algorithm,components,C#,Algorithm,Components,所以我一直在尝试编写一个连接组件标记算法,但它并没有给我想要的结果。现在我有一张有3朵玫瑰(没有重叠)的图像,我想给每朵玫瑰贴上自己的灰色标签。在应用标记算法之前,我使用一个阈值来去除背景,只保留玫瑰。玫瑰的灰度值为1(完全白色),背景的灰度值为0(黑色)。下面是这张图片的样子: 完成此操作后,我应用了标记算法。它应该根据给玫瑰的标签给出三种不同的灰度值。但是取而代之的是,该算法在前两个玫瑰上创建了这种奇怪的梯度模式,而最后一个似乎是一个单一的灰度值。这是一张图片: 算法可能看起来很复杂,但

所以我一直在尝试编写一个连接组件标记算法,但它并没有给我想要的结果。现在我有一张有3朵玫瑰(没有重叠)的图像,我想给每朵玫瑰贴上自己的灰色标签。在应用标记算法之前,我使用一个阈值来去除背景,只保留玫瑰。玫瑰的灰度值为1(完全白色),背景的灰度值为0(黑色)。下面是这张图片的样子:

完成此操作后,我应用了标记算法。它应该根据给玫瑰的标签给出三种不同的灰度值。但是取而代之的是,该算法在前两个玫瑰上创建了这种奇怪的梯度模式,而最后一个似乎是一个单一的灰度值。这是一张图片:

算法可能看起来很复杂,但实际上很简单。我先在列上递归,然后在行上递归,对于每个非背景像素,我检查其相邻像素是否已标记(这意味着它们的objectArray值不是零)。如果是这样,我会把他们添加到邻居的列表中。然后,我继续检查此列表是否为空,如果为空,我将通过递增“对象”值并将其值指定给当前像素的标签值来唯一标记当前像素,并将当前像素的父值设置为此唯一标签。如果不为空,则确定邻居列表中的最小标签值,将所有邻居的父值设置为该标签值,并将当前像素的标签和父值设置为该标签值。我对每个像素重复这个过程,直到整个图像都被标记

完成后,我再次递归像素值,这次将每个像素的标签值设置为其父值。然后,我继续根据像素的标签值为其指定一个新的灰度值

我不明白为什么算法没有正确标记玫瑰。有人能帮我吗?以下是算法:

public void label()
{
    int objects = 1;
    int[,] objectArray = new int[colors.GetLength(1), colors.GetLength(0)];
    DisjointSets disjointSet = new DisjointSets();
    int[,] parents = new int[colors.GetLength(1), colors.GetLength(0)];
    List<List<int>> eqSet = new List<List<int>>();
    for (int i = 0; i < colors.GetLength(1); i++) for (int j = 0; j < colors.GetLength(0); j++)
    {
        if (this[i, j].Gray == 1)
        {
            List<Label> neighbors = new List<Label>();
            if (i > 0)
            {
                if (this[i - 1, j].Gray == 1)
                {
                    if (objectArray[i - 1, j] != 0)
                    {
                        neighbors.Add(new Label(i - 1, j, 0));
                    }
                }
                if (j > 0)
                {
                    if (this[i - 1, j - 1].Gray == 1)
                    {
                        if (objectArray[i - 1, j - 1] != 0)
                        {
                            neighbors.Add(new Label(i - 1, j - 1, 0));
                        }
                    }
                }
                if (j < colors.GetLength(0))
                {
                    if (this[i - 1, j + 1].Gray == 1)
                    {
                        if (objectArray[i - 1, j] != 0)
                        {
                            neighbors.Add(new Label(i - 1, j, 0));
                        }
                    }
                }
            }
            if (j > 0)
            {
                if (this[i, j - 1].Gray == 1)
                {
                    if (objectArray[i, j - 1] != 0)
                    {
                        neighbors.Add(new Label(i, j - 1, 0));
                    }
                }
                if (i < colors.GetLength(1))
                {
                    if (this[i + 1, j - 1].Gray == 1)
                    {
                        if (objectArray[i + 1, j - 1] != 0)
                        {
                            neighbors.Add(new Label(i + 1, j - 1, 0));
                        }
                    }
                }
            }
            if (i < colors.GetLength(1))
            {
                if (this[i + 1, j].Gray == 1)
                {
                    if (objectArray[i + 1, j] != 0)
                    {
                        neighbors.Add(new Label(i + 1, j, 0));
                    }
                }
                if (this[i + 1, j + 1].Gray == 1)
                {
                    if (objectArray[i + 1, j + 1] != 0)
                    {
                        neighbors.Add(new Label(i + 1, j + 1, 0));
                    }
                }
            }
            if (j < colors.GetLength(0))
            {
                if (this[i, j + 1].Gray == 1)
                {
                    if (objectArray[i, j + 1] != 0)
                    {
                        neighbors.Add(new Label(i, j + 1, 0));
                    }
                }
            }

            if (neighbors.Count == 0)
            {
                objects++;
                objectArray[i, j] = objects;
                parents[i, j] = objects;
            }
            if (neighbors.Count > 0)
            {
                int smallestLabel = 10000;
                foreach (Label x in neighbors)
                    if (objectArray[x.X, x.Y] < smallestLabel)
                        smallestLabel = objectArray[x.X, x.Y];

                foreach (Label x in neighbors)
                    parents[x.X, x.Y] = smallestLabel;

                objectArray[i, j] = smallestLabel;
                parents[i, j] = smallestLabel;
            }
        }
    }
    for (int i = 0; i < colors.GetLength(1); i++) for (int j = 0; j < colors.GetLength(0); j++)
    {
        if (this[i, j].Gray == 1)
        {
            if (objectArray[i, j] != 0)
            {
                objectArray[i, j] = parents[i, j];
                ColorWrap c = this[i, j];
                c.X = (float)objectArray[i, j] / objects;
                c.Y = (float)objectArray[i, j] / objects;
                c.Z = (float)objectArray[i, j] / objects;
                this[i, j] = c;
            }
        }
    }
}
公共无效标签()
{
int objects=1;
int[,]objectArray=newint[colors.GetLength(1),colors.GetLength(0)];
DisjointSets DisjointSets=新的DisjointSets();
int[,]parents=newint[colors.GetLength(1),colors.GetLength(0)];
列表方程组=新列表();
for(int i=0;i0)
{
if(这[i-1,j].Gray==1)
{
if(objectArray[i-1,j]!=0)
{
添加(新标签(i-1,j,0));
}
}
如果(j>0)
{
if(这[i-1,j-1].Gray==1)
{
if(objectArray[i-1,j-1]!=0)
{
添加(新标签(i-1,j-1,0));
}
}
}
if(j0)
{
if(这[i,j-1].Gray==1)
{
if(objectArray[i,j-1]!=0)
{
添加(新标签(i,j-1,0));
}
}
if(i0)
{
int smallestLabel=10000;
foreach(相邻中的标签x)
if(objectArray[x.x,x.Y]                if (this[i - 1, j + 1].Gray == 1)
                {
                    if (objectArray[i - 1, j] != 0)
                    {
                        neighbors.Add(new Label(i - 1, j, 0));
                    }
                }
          #######...
          ######....
          #####.....
          ####......
          ###O......
          ###.......
          ##x.......
          #xx.......