如何用一组点分割图像,将每个像素分配给最近的点?

Lam*_*amp 3 algorithm image-processing

我尝试实现一种用种子点分割图像的方法,并将每个像素分配给最近的点。

例如,如果像素接近1,则设置为1。

输入:

0 0 0 0 0 3 0
0 1 0 0 0 0 0
0 0 0 0 2 0 0
0 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)

输出:

1 1 1 3 3 3 3
1 1 1 2 2 3 3
1 1 1 2 2 2 2
1 1 1 2 2 2 2
Run Code Online (Sandbox Code Playgroud)

目前的方法需要太长的时间并计算(width * height * numPoints)次,有没有任何算法可以更快?


7秒处理5 9478 * 1868张图像,numPoints = 8

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    byte index = 0;
                    double distance = double.MaxValue;
                    for (int m = 0; m < elements.Count; m++)
                    {
                        CircleROI circle = roiResized[m];
                        double currentDistance = Math.Abs(i - circle.Center.Y) +
                            Math.Abs(j - circle.Center.X);
                        if (currentDistance < distance)
                        {
                            distance = currentDistance;
                            index = (byte)m;
                        }
                    }

                    *data++ = index;
                }
            }
Run Code Online (Sandbox Code Playgroud)

Ste*_*tef 5

您可以使程序的运行时间与像素数成正比。

维护您当前正在“处理”的像素队列

初始化该队列,使其包含所有最初非零的像素。

然后在队列不为空时循环:

  • 从队列中弹出一个像素(x,y);
  • 将 (x, y) 的每个零邻域着色为与 (x, y) 相同的颜色;
  • 将您已着色的每个像素添加到队列中。