我正在尝试建立跟踪方法,以2dArray

时间:2019-02-11 22:50:54

标签: java arrays trace edge-detection

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

到目前为止,我正在使用嵌套的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结束 循环

我该如何做?

1 个答案:

答案 0 :(得分:0)

对于以下轮廓,仅了解当前像素还不够。您还需要到达的方向。然后,您可以从当前方向 开始围绕当前像素(顺时针或逆时针)旋转来寻找下一个像素。

因此,您的表应具有8 x 256个条目。另外,您可以依次尝试8个邻居(实际上6个就足够了)。可能会有不同的时空权衡。

要启动遍历,您可以查找一个配置,该配置的像素被空白包围,后跟一个空白。停止条件有点棘手。为了正确起见,您应该在返回初始像素时停止,并准备沿相同方向继续进行。

enter image description here