English 中文(简体)
2.D阵列边界跟踪算法
原标题:algorithm to trace border in 2D array

I have a 2D array of integers that represent groupings (crystal grains) on a 2D surface. something like this: something like this (each pixel of that image is assigned an integer depending on the group it belongs to, so every red pixel is assigned 1 for example, every blue is 2)

在两个此类集团(用户点击)之间的边界上给予X,Y-coordination, how,我可以追踪这2个集团之间的边界,使沿边界的每座钢ordinate得以拯救,并获得两个终端坐标。 (一) 在没有终点但空档的情况下,并不涉及封闭状态。

不管算法如何 我来到这里似乎非常渴望执行,我可以想象,以前没有人这样做了。 任何帮助? 最好是在c#中找到解决办法,但非常赞赏对算法的任何担忧。

http://www.un.org。

我本应该指出,我将执行一种算法,使这一行文如此:

  1. between the two endpoints define a line
  2. get the boundary point farthest away from the current line, if it is farther than d, split the line at this point, so there are two line segments
  3. repeat until no boundary point is farther away than d from the line strip

我认为,这足以执行我为什么不说这一点。 要想到这里,我就迫切需要解决当前的问题。

问题:

how is the original data formted? - it s an brief short[,]belling; with which is around 250x150 which such as:

11111112222
11111222222
11112222222
11112222222 <- user clicks on leftmost 2 or rightmost 1 -> I want to trace that border
11111122222                                                down to the first
11111133322                                                encountered 3 and up to the
11333333333                                                frame-border

what is an endpoint? - 正如我一直在思考全球解决办法时,我可以把终点描述为一个2x2地区,其中4个食堂由肤色1、肤色2和至少三分之一不同的颜色组成。

what is contiguouslylink? - 相对于算法其余部分而言,它确实没有问题,见下文。

what about yshaped areas? - 我不关心他们,你可以假定边界后面的肤色1 面积至少为2个,如果我们谈论4个或8个邻国,那也是这样。

what do I currently have? - at first I tried a walking -algorithm, something like mvds posted, but found me doing stepping, neighborcalculation and checking in all 4 directions which was tedious and looked awful. I didn t find a nice representation for "this is the direction the last step came from, don t check that pixel for neighborhood".

然后,我放弃了步行算法,并尝试了一种全球办法(如过滤器):每台钢子检查,如果是彩色1,在4个邻里有色2。 有了这一点,我就能够跨越肤色1和肤色2之间的界限。 我将拆除与用户-电离层协调不相连的所有边界,但随后我要谈谈:最终点是什么?

我仍然感谢更多的投入。 现在,我看一看一下我可以在多大程度上使用微薄算法。

最佳回答

我说,你已经确定这2种颜色,如被描述为先处理步骤。

我认为,如果每个(x,y)代表不是一种幻灯,而是4个针头触及的点,那么你会发现,使用一种协调制度是有益的。 然后,你可以写出一项功能,确定北方是否是边界线标,同样对南、东、西(或者也许你倾向于术语上下/下/右边)。

从边界一站开始,例如,将4x4居民区扫描成一个N/S/E/W作为边界点。 沿这条边界,然后扫描除你所走方向外的四条方向,用于下一个六甲边界。 页: 1 之后,你知道你会重一点。

回到边界一开始,以与你最初的经历不同的方向追踪边界,直到你到达另一个终点。

This gives you all the pixel borders. Each pixel border has colour 1 on one side and colour 2 on the other side.

(我本来会认为,病媒化比确定边界要困难得多,但这不是你的问题主要在于什么正确?) 为此,我从终点开始,沿边界沿循边境的边界顺序,每次检查从终点到目前点的直线是否与平线相匹配。 一行一行,一行一行即告终,一行即行一行。

问题回答

这里有几条思想和算法的开始:

寻找平等肤色的area>的大纲比找到边界容易,特别是当边界不是真正的“组合”时(即只有2个色体)似乎出现在你的形象上。

寻找两个大纲的对应部分并不十分复杂。 关于大纲A的每一点,都找到了大纲B的最接近点。 如果距离 > > > >;X ,A和B之间的中间点位于边界。 (X) 取决于贵国边界的模糊性。

如果你能够使用户在边界两侧两次点击,那将是巨大的。 如果你坚持one点击,则在点击点周围X的半径处发现两个最大的地区。

确定一个地区的大纲并不复杂:

  1. take a point (x,y) to start, take a direction (dx,dy)=(1,0) to start
  2. take color C of point (x,y) which will be the color to trace
  3. run x+=dx,y+=dy until at (x+dx,y+dy) you have another color and are on a boundary
  4. peek at (x+dx,y+dy), if it is not the same color C, you are hitting a boundary: turn left and goto 4.
  5. x+=dx, y+=dy, i.e. take a step
  6. record (x,y) as part of the boundary
  7. if ( x==xstart && y==ystart ) you re done
  8. turn right and goto 4.

<<>tturn 左 系指(dx ,dy ) = (dy,-dx),Revs++

<turn right means: (dx ,dy ) = (-dy,dx), Revolutions--

<代码>revolutions,视追溯方向(内部/外)而终止正或负。

有一个独角兽案,即当你开始在一只六餐区时,这种 lo就无限制。 很容易检查。 此外,你还可能要检查X/y边界。 当然,“同肤色”和“其他肤色”也可作为某种肤色距离限加以实施(即r(g,b)-(R,G,B><D)。

<>strong>dis claimer, 这是一个行之有效但简单、缓慢算法 在没有任何相关知识和经验的情况下,我一劳永逸。

你的描述对我来说是很明确的,但如果我正确理解你的话,你要赞扬以下几点:

  • the set of contiguously connected points of colour A which are adjacent to a point of colour B
  • that contains the given starting point.

鉴于这一具体特点,你的法典实际上写了自己。 你们现在需要决定的唯一一点是,“毗连”的含义是什么(例如,只有与玉米相连的ix子?)?

此外,你的描述也不明确。 如果你把“最终点”定义为“在定点中仅有一个邻国的定点”的话,则考虑该地区武器属于单一颜色的区域:如果你将最终点定为“定点”。 如果你放松要求,允许任何最终点,那么你的法典就可以收集一套最终点。

Gla你解决了你的问题。 我勾画出一种解决办法,为你的样本问题产生这一结果:

1111***2222
111**222222
111*2222222
111*2222222
111***22222
11111*33322
11333333333

这部法典只是因为我需要加以确认,以便将其编码: 报告撰写时要明确,而不是速度。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace StackOverflowEdgeDetection
{
    class Program
    {
        private static HashSet<Point> FindBorder(char[,] grid, Point start, char inside, char outside)
        {
            var border = new HashSet<Point> {};
            var endpoints = new HashSet<Point> { start };

            while (endpoints.Count != 0)
            {
                var p = endpoints.First();
                endpoints.Remove(p);
                border.Add(p);
                var newEndpoints = Neighbours(p).Where(q =>
                        Grid(grid, q) == inside &&
                        !border.Contains(q) &&
                        Neighbours(q).Any(r => Grid(grid, r) == outside)
                    );
                endpoints.UnionWith(newEndpoints);
            }

            return border;
        }

        private static IEnumerable<Point> Neighbours(Point p)
        {
            yield return new Point(p.X - 0, p.Y - 1);
            yield return new Point(p.X + 1, p.Y - 1);
            yield return new Point(p.X + 1, p.Y + 0);
            yield return new Point(p.X + 1, p.Y + 1);
            yield return new Point(p.X + 0, p.Y + 1);
            yield return new Point(p.X - 1, p.Y + 1);
            yield return new Point(p.X - 1, p.Y - 0);
            yield return new Point(p.X - 1, p.Y - 1);
        }

        public static char Grid(char[,] grid, Point p) {
            var x = p.X;
            var y = p.Y;
            var height = grid.GetLength(0);
            var width = grid.GetLength(1);
            return (0 <= x && x < width && 0 <= y && y < height) ? grid[y, x] :   ;
        }

        static void Main(string[] args)
        {
            var border = FindBorder(TestGrid, TestStart, TestInside, TestOutside);

            var points = Enumerable.Range(0, TestHeight)
                                   .SelectMany(y => Enumerable.Range(0, TestWidth)
                                                              .Select(x => new Point(x, y)));

            foreach (var p in points) {
                Console.Write(border.Contains(p) ?  *  : Grid(TestGrid, p));
                if (p.X + 1 == TestWidth) Console.WriteLine();
            }

            Console.ReadLine();
        }

        private static readonly char[,] TestGrid = new char[,] {
            {  1 ,  1 ,  1 ,  1 ,  1 ,  1 ,  1 ,  2 ,  2 ,  2 ,  2  }, 
            {  1 ,  1 ,  1 ,  1 ,  1 ,  2 ,  2 ,  2 ,  2 ,  2 ,  2  }, 
            {  1 ,  1 ,  1 ,  1 ,  2 ,  2 ,  2 ,  2 ,  2 ,  2 ,  2  }, 
            {  1 ,  1 ,  1 ,  1 ,  2 ,  2 ,  2 ,  2 ,  2 ,  2 ,  2  }, 
            {  1 ,  1 ,  1 ,  1 ,  1 ,  1 ,  2 ,  2 ,  2 ,  2 ,  2  }, 
            {  1 ,  1 ,  1 ,  1 ,  1 ,  1 ,  3 ,  3 ,  3 ,  2 ,  2  }, 
            {  1 ,  1 ,  3 ,  3 ,  3 ,  3 ,  3 ,  3 ,  3 ,  3 ,  3  }
        };
        private static readonly Point TestStart = new Point(3, 3);
        private static readonly Point TestAdjacent = new Point(4, 3);
        private static readonly char TestInside = Grid(TestGrid, TestStart);
        private static readonly char TestOutside = Grid(TestGrid, TestAdjacent);
        private static readonly int TestWidth = TestGrid.GetLength(1);
        private static readonly int TestHeight = TestGrid.GetLength(0);
    }
}

http://www.sakri.net/blog/2009/05/28/detecting-ant-pixels-with-marching-squares-algorithm/“rel=“nofollow”>detecting ant pixels with pathing平价算/a> and on wikipedia (should that connection goew) marchingsalgoa/





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签