我正在尝试构建一个跟踪方法,以 2dArray 的形式跟踪图片轮廓的坐标

I'm trying to build a trace method, to trace the coordinates of the outline of a picture in form of a 2dArray

对于一个学校项目,我正在尝试构建一个跟踪方法。它应该根据 EdgeDetection 方法中的数组跟踪形状轮廓的坐标。我确保轮廓只有一个像素厚。因为这是一个项目,所以我不能使用图书馆的任何花哨技巧。

到目前为止,我正在使用嵌套的 forloop 来查找形状的开头。然后我使用这样的形状:

p9  p2  p3
p8  p1  p4
p7  p6  p5

p1 是我在数组中的当前位置,然后我在 p2-p9 中搜索下一个位置。

到目前为止的代码如下所示:

private static String traceCoordinates(int o[][])
{

    String useless = null; 
    //int[][] n = new int[m[0].length][m.length];
    //int[][] o = new int[n.length][n[0].length];

   // n = transpose(m);
    //o = thinning(n);

    int hits = 0;
    System.out.print("        ");
    for (int a = 0; a < 1; a++)
    {
        for (int b = 0; b < o[0].length; b++)
        {
            String c = String.format("%3s", b);
            System.out.print(c + " ");
        }System.out.println("");
    }

    for (int y = 0; y < o.length; y++)
    {
        //String e = String.format("%3s", y).replace(" ", "0");
        //System.out.print("row" + e + " ");
        for (int x = 0 ; x < o[0].length ; x++)
        {
            if (o[y][x] == 0)
            {
                int xEnd = x;
                int yEnd = y;

                int a = y + 1;
                int b = y - 1;
                int c = x + 1;
                int d = x - 1;

                /*
                Her findes værdien af de 8 nabopixler på baggrund af deres indbyrdes index.
                */
                int p2 = o[b][x];
                int p3 = o[b][c];
                int p4 = o[y][c];
                int p5 = o[a][c];
                int p6 = o[a][x];
                int p7 = o[a][d];
                int p8 = o[y][d];
                int p9 = o[b][d];

                do
                {

                    System.out.println(y + ";" + x + " " + "begin");
                    if (p2 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                        o[y][x] = 66;
                        y--;
                        System.out.print("Next point: ");
                        System.out.println("2; " + pntC(x, y) + " ");
                        hits++;
                    }
                    else if (p3 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                       o[y][x] = 66;
                        y--;
                        x++;
                        System.out.print("Next point: ");
                        System.out.println("3; " + pntC(x, y) + " ");;
                        hits++;
                    }
                    else if (p4 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                        o[y][x] = 66;
                        x++;
                        System.out.print("Next point: ");
                        System.out.println("4; " + pntC(x, y) + " ");
                        hits++;
                    }
                    else if (p5 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                        o[y][x] = 66;
                        x++;
                        y++;
                        System.out.print("Next point: ");
                        System.out.println("5; " + pntC(x, y) + " ");
                        hits++;
                    }
                    else if (p6 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                        o[y][x] = 66;
                        y++;
                        System.out.print("Next point: ");
                        System.out.println("6; " + pntC(x, y) + " ");
                        hits++;
                    }
                    else if (p7 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                        o[y][x] = 66;
                        x--;
                        y++;
                        System.out.print("Next point: ");
                        System.out.println("7; " + pntC(x, y) + " ");
                        hits++;
                    }
                    else if (p8 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                        o[y][x] = 66;
                        x--;
                        System.out.print("Next point: ");
                        System.out.println("8; " + pntC(x, y) + " ");
                        hits++;
                    }
                    else if (p9 == 0 && hits < 1)
                    {
                        System.out.print("This point: ");
                        System.out.print(pntC(x, y) + " ");
                        o[y][x] = 66;
                        x--;
                        y--;
                        System.out.print("Next point: ");
                        System.out.println("9; " + pntC(x, y) + " ");
                        hits++;
                    }
                    System.out.println(y + ";" + x + " " + "end");
                    hits = 0;

                }while(o[y][x] == 0);   
                System.out.println("loop\n");



            }

        }
        System.out.println("");
    }System.out.println(hits);
    for( int row = 0 ; row < o.length ; row++)
    {//start row for loop
        String e = String.format("%3s", row);
        System.out.print("row" + e + " ");
        for (int column = 0 ; column < o[0].length ; column++)
        {// start column for loop
                           if (o[row][column] == 255)
                            {
                                       System.out.print("___|");
                            }
                           else
                           {
                               System.out.print("   " + o[row][column]);
                           }
        }// end colum for loop

        System.out.println(" end row");
    }// end row for loop
return useless;    
}

当我完成后,跟踪通过 StringBuilder 作为字符串返回,因此是无用的变量。

我的问题是我的方法只能检测一个方向的直线。当那个方向没有更多的像素时,它跳出循环。

我正在练习以下数组:

int[][] A = new int[][]
    {
        {1,  2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21, 22},
        {2,  0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 2},
        {3,  255, 0,   0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 2},
        {4,  255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 2},
        {5,  255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 2},
        {6,  255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 2},
        {7,  255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 2},
        {8,  255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 2},
        {9,  255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {10, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {11, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {12, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {13, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {14, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {16, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {17, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {18, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {19, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {20, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {21, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2},
        {1,  2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21, 22}
    };

我希望看到返回的是这样的:

1;1 2;2 2;3 3;4 4;4 5;5 6;6 7;7 8;8 9;9 10;10 9;11 8;12 7;13 6;14 5;15 4;16 3;17 2;18 1;19

按照这个顺序。

现在该方法的实际输出是:

1;1 begin

这个点:001/001 下一个点:5; 002/002
2;2 结束 2;2 开始 这个点:002/002 下一个点:5; 003/003
3;3 结束 循环

3;4 开始 这个点:004/003 下一个点:6; 004/004
4;4 结束 4;4 开始 这个点:004/004 下一个点:6; 004/005
5;4 结束 循环

5;5 开始 这个点:005/005 下一个点:5; 006/006
6;6 结束 6;6 开始 这个点:006/006 下一个点:5; 007/007
7;7 结束 7;7 开始 这个点:007/007 下一个点:5; 008/008
8;8 结束 8;8 开始 这个点:008/008 下一个点:5; 009/009
9;9 结束 9;9 开始 这个点:009/009 下一个点:5; 010/010
10;10 结束 10;10 开始 这个点:010/010 下一个点:5; 011/011
11;11 结束 循环

我怎样才能完成这项工作?

对于轮廓跟随,仅仅知道当前像素是不够的。您还需要到达它的方向。然后你开始通过从当前方向.

开始围绕当前像素旋转(顺时针或逆时针)来寻找下一个像素

因此您的 table 应该有 8 x 256 个条目。或者,您可以依次尝试 8 个邻居(实际上 6 个就足够了)。不同的 space/time 权衡是可能的。

要启动遍历,您可以寻找一个像素被空心包围的配置,后跟一个像素。停止标准有点棘手。为了正确起见,您应该在回到初始像素时停下来,准备继续朝同一方向前进。