我正在尝试使用Zhang-Suen thinning algorithm。我试图用Java实现它。但问题是它发现我的边缘不是一个像素宽度线。我第一次使用这个算法,我不知道我的逻辑有什么问题。
我想要实现的目标是:
我能够实现的目标是:
public void thinStepI(){
delList.clear();
neighbor = 0;
connectivity = 0;
for(int i=4;i<width-4;i++)
for(int j=4;j<height-4;j++){
p = pixelList[i][j];
if (p == 1){
p1 = pixelList[i-1][j];
p2 = pixelList[i-1][j+1];
p3 = pixelList[i][j+1];
p4 = pixelList[i+1][j+1];
p5 = pixelList[i+1][j];
p6 = pixelList[i+1][j-1];
p7 = pixelList[i][j-1];
p8 = pixelList[i-1][j-1];
neighbor = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
if (p1 == 0 && p2 == 1)
connectivity ++;
if (p2 == 0 && p3 == 1)
connectivity ++;
if (p3 == 0 && p4 == 1)
connectivity ++;
if (p4 == 0 && p5 == 1)
connectivity ++;
if (p5 == 0 && p6 == 1)
connectivity ++;
if (p6 == 0 && p7 == 1)
connectivity ++;
if (p7 == 0 && p8 == 1)
connectivity ++;
if (p8 == 0 && p1 == 1)
connectivity ++;
if ( connectivity == 1 && (neighbor >= 2 && neighbor <= 6) &&
(p1 * p3 * p5 == 0) && (p3 * p5 * p7 == 0) ){
delList.add(i);
delList.add(j);
}
}
}
int length = delList.size();
if (length != 0){
for(int i =0; i < (length - 1); i+=2){
pixelList[delList.get(i)][delList.get(i+1)] = 0;
System.out.println("oldu");
}
thinStepI();
}
}
public void thinStepII(){
delList.clear();
neighbor = 0;
connectivity = 0;
for(int i=4;i<width-4;i++)
for(int j=4;j<height-4;j++){
if (pixelList[i][j] == 1){
p = pixelList[i][j]; // ** Origin Pixel **
p1 = pixelList[i-1][j];
p2 = pixelList[i-1][j+1];
p3 = pixelList[i][j+1];
p4 = pixelList[i+1][j+1];
p5 = pixelList[i+1][j];
p6 = pixelList[i+1][j-1];
p7 = pixelList[i][j-1];
p8 = pixelList[i-1][j-1];
neighbor = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
if (p1 == 0 && p2 == 1)
connectivity ++;
if (p2 == 0 && p3 == 1)
connectivity ++;
if (p3 == 0 && p4 == 1)
connectivity ++;
if (p4 == 0 && p5 == 1)
connectivity ++;
if (p5 == 0 && p6 == 1)
connectivity ++;
if (p6 == 0 && p7 == 1)
connectivity ++;
if (p7 == 0 && p8 == 1)
connectivity ++;
if (p8 == 0 && p1 == 1)
connectivity ++;
if ( connectivity == 1 && (neighbor >= 2 && neighbor <= 6) &&
(p1 * p3 * p7 == 0) && (p1 * p5 * p7 == 0) ){
delList.add(i);
delList.add(j);
}
}
}
int length = delList.size();
if (length != 0){
for(int i =0; i < (length - 1); i+=2){
pixelList[delList.get(i)][delList.get(i+1)] = 0;
System.out.println("oldu2");
}
thinStepII();
}
}
我的逻辑错误在哪里导致我得到错误的结果?
答案 0 :(得分:2)
我认为您用于描述ZS算法的链接是错误的。删除前景像素的必要条件是其交叉数为1.“交叉数”是当您顺时针遍历8个相邻像素时前景像素变为背景像素的次数订购。您的链接中的描述似乎缺少这一点。看到: http://www.uel.br/pessoal/josealexandre/stuff/thinning/ftp/lam-lee-survey.pdf 为了更好的讨论。
答案 1 :(得分:1)
声明
connectivity = 0;
应该放在每个方法的两个for循环中。
答案 2 :(得分:1)
您必须循环,直到对位图没有任何更改。 你只做了一次迭代算法。