首页 > 解决方案 > 区域标注(连通分量增长)

问题描述

我正在尝试计算图像中欧元硬币的价值。图像包括一个参考点(30 毫米),使生活更轻松。

我已将图像分割成仅显示硬币的二进制版本。

二进制图像

现在我想运行一个区域标签,在将这些区域一起增长后,它应该为图像中的每个元素提供一个不同的 ID。

作为参考,这就是它应该看起来的样子(在这里使用了不同的图像,但你明白了) 图像中的标记元素

然而,我现在已经为这个任务创建了我的程序,我留下了一个看起来像这样的图像: 错误的区域标签

我相信我过于频繁地增加区域(特别是考虑到我留下了超过 50 万个不同的区域,而不仅仅是图像中不同硬币的数量。但是,我似乎找不到生成的代码部分在这个错误中。此外,我相信我错过了将相同区域合并在一起的机会,但不太知道如何将其实施到程序中。

我知道我必须再次遍历像素才能将相同的区域合并在一起,但我不确定第一次运行所需的准备工作。

这是我的程序:

public static final int FG_VAL = 0;
public static final int BG_VAL = 255;
private void labelRegions(int[][] segmentedImage, int width, int height){
    int[][] region = new int[width][height];
    int cpt = 0; //componentCount
    for(int x = 0; x < width; ++x){
        for(int y = 0; y < height; ++y){

            if ((x-1 >= 0) && segmentedImage[x-1][y] != FG_VAL) {
                if ((y-1 >= 0) && segmentedImage[x][y-1] != FG_VAL) {
                    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 (segmentedImage[x][y] == FG_VAL) {
                if ((y-1 >= 0) && segmentedImage[x][y-1] != FG_VAL) {
                    System.out.println("Assign the label of the North pixel to the current pixel");
                    region[x][y] = region[x][y - 1];
                } else if ((y-1 >= 0) && segmentedImage[x][y-1] == FG_VAL) {
                    System.out.println("Create a new label id and assign it to the current pixel");
                    cpt++;
                    region[x][y] = cpt;
                }
            }

        }
    }
    ImageJUtility.showNewImage(region, width, height, "Labelled?");
}

标签: javaimage-processingimage-segmentationimagej

解决方案


推荐阅读