Java映像连接组件
我有一个程序来检测二值化BuffereImage中的对象,该图像是一个多选答案表。Java映像连接组件,java,image-processing,bufferedimage,Java,Image Processing,Bufferedimage,我有一个程序来检测二值化BuffereImage中的对象,该图像是一个多选答案表。 我正在尝试使用4-Connectivity as in来检测每个对象(答案在工作表上)。 到目前为止,我手头上的资料是: 我根据维基百科的指示提出了这个建议: if(getPixel(image, x, y) != 0){ if(getPixel(image, x-1, y) !=0){ System.out.println("we are in the same region");
我正在尝试使用4-Connectivity as in来检测每个对象(答案在工作表上)。
到目前为止,我手头上的资料是: 我根据维基百科的指示提出了这个建议:
if(getPixel(image, x, y) != 0){
if(getPixel(image, x-1, y) !=0){
System.out.println("we are in the same region");
region[x][y] = region[x-1][y];
}
else if(getPixel(image, x-1, y) !=0 && getPixel(image, x, y-1) !=0){
System.out.println("North and West pixels belong to the same region and must be merged");
region[x][y] = Math.min(region[x-1][y], region[x][y-1]);
}
else if( getPixel(image, x-1, y) ==0 && getPixel(image, x, y-1) !=0){
System.out.println("Assign the label of the North pixel to the current pixel");
region[x][y] = region[x][y-1];
}
else if(getPixel(image, x-1, y) ==0 && getPixel(image, x, y-1) ==0){
System.out.println("Create a new label id and assign it to the current pixel");
cpt++;
region[x][y] = cpt;
}
但问题是它创造了51个区域!并且它只打印每个对象的几个顶部像素(不是所有像素)。有谁能帮我找出问题所在以及如何检测我的物体吗?
如果您能提供帮助,我将不胜感激。虽然我不能完全确定这是否能回答您的问题,但我会修改您的代码,使其如下所示:
if(getPixel(image, x, y) != 0){
if(getPixel(image, x-1, y) !=0){
if(getPixel(image, x, y-1) !=0) {
System.out.println("North and West pixels belong to the same region and must be merged");
region[x][y] = Math.min(region[x-1][y], region[x][y-1]);
} else {
System.out.println("we are in the same region");
region[x][y] = region[x-1][y];
}
} else if(getPixel(image, x-1, y) ==0) {
if(getPixel(image, x, y-1) !=0) {
System.out.println("Assign the label of the North pixel to the current pixel");
region[x][y] = region[x][y-1];
} else if (getPixel(image, x, y-1) ==0) {
System.out.println("Create a new label id and assign it to the current pixel");
cpt++;
region[x][y] = cpt;
}
}
虽然我不能完全确定这是否能回答您的问题,但我会修改您的代码,使其看起来像这样:
if(getPixel(image, x, y) != 0){
if(getPixel(image, x-1, y) !=0){
if(getPixel(image, x, y-1) !=0) {
System.out.println("North and West pixels belong to the same region and must be merged");
region[x][y] = Math.min(region[x-1][y], region[x][y-1]);
} else {
System.out.println("we are in the same region");
region[x][y] = region[x-1][y];
}
} else if(getPixel(image, x-1, y) ==0) {
if(getPixel(image, x, y-1) !=0) {
System.out.println("Assign the label of the North pixel to the current pixel");
region[x][y] = region[x][y-1];
} else if (getPixel(image, x, y-1) ==0) {
System.out.println("Create a new label id and assign it to the current pixel");
cpt++;
region[x][y] = cpt;
}
}
您可能会得到很多区域,因为您似乎没有合并相等的标签。没有用于在代码段中存储相等标签的代码。该算法是两遍算法,其中第一遍分配标签,第二遍合并相等的标签 以下是维基百科页面引用的条件检查: 检查条件:
else if(getPixel(image, x-1, y) !=0 && getPixel(image, x, y-1) !=0)
不检查左像素和上像素是否具有不同的标签
此外,正如supersam654在评论中提到的那样,将永远不会调用第一个else if
。看起来维基百科页面上的条件检查(1)和(2)的顺序应该相反。也就是说,首先检查左上像素是否与当前像素具有相同的值,但标签不相同。然后,如果检查失败,请检查左侧像素是否与当前像素具有相同的值
因此,请尝试以下方法:
我希望这有帮助。您可能会得到很多区域,因为您似乎没有合并相同的标签。没有用于在代码段中存储相等标签的代码。该算法是两遍算法,其中第一遍分配标签,第二遍合并相等的标签 以下是维基百科页面引用的条件检查: 检查条件:
else if(getPixel(image, x-1, y) !=0 && getPixel(image, x, y-1) !=0)
不检查左像素和上像素是否具有不同的标签
此外,正如supersam654在评论中提到的那样,将永远不会调用第一个else if
。看起来维基百科页面上的条件检查(1)和(2)的顺序应该相反。也就是说,首先检查左上像素是否与当前像素具有相同的值,但标签不相同。然后,如果检查失败,请检查左侧像素是否与当前像素具有相同的值
因此,请尝试以下方法:
我希望这有帮助。根据
if/else if
代码的顺序,第二个代码块(第一个else if
)将永远不会被调用。我不认为这是你想要的。@supersam654是的,你是对的,它从未被调用过,我应该把它放在哪里?感谢根据if/else if
代码的顺序,第二个代码块(第一个el