对于一所学校PROJEKT我试图建立一个跟踪方法。它应该追查基于来自边缘检测方法的阵列形状的轮廓的坐标。我确信轮廓只有一个像素厚。因为它是一个PROJEKT我不能使用任何花哨的招数从libary。
到目前为止,我使用的是嵌套的for循环找到形状的开始。然后,我使用这样的形状:
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结束循环
我怎样才能使这项工作?