java - 我正在尝试构建一个跟踪方法,以 2dArray 的形式跟踪图片轮廓的坐标
问题描述
对于一个学校项目,我正在尝试建立一个跟踪方法。它应该基于 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 end 8;8 begin 此点: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 结束循环
我怎样才能使这项工作?
解决方案
推荐阅读
- ctypes - 在 python 中调用具有复杂结构的 WinDLL 不起作用
- laravel - 如何使用验证规则在 Laravel 中使用不同的日期格式验证日期?
- ios - 上传图片时出现 ERROR DOMAIN-3840
- laravel - 在 Ubuntu 上运行 composer global 时连接超时需要 laravel/installer
- jquery - jQuery Autosearch 未使用 codeigniter3 在文本框上显示获取的数据
- xcode - Xcode ARKit 图像检测手动大小调整
- spring - 如何获取 Spring Boot 属性文件值作为 Map
- reactjs - 动作返回后没有调用减速器?
- node.js - 是否有任何实时示例可以使用 mocha 或 jest 工具为 azure nodejs 函数编写单元测试用例
- python-3.x - 将列 wrt 中的 NULL 或 NA 替换为 pandas 数据框中的其他列