java - 区域标注(连通分量增长)
问题描述
我正在尝试计算图像中欧元硬币的价值。图像包括一个参考点(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?");
}
解决方案
推荐阅读
- java - lombok - 找不到符号 getId()
- c - “塔计数”给出了正确的输出,但 CodeChef 说“错误的答案”,也许角落案例有问题,但我不知道
- git - “轻量级标签”可以指向树对象或 blob 对象吗?
- apache - Apache 调度程序规则不拒绝 /.json;%0aa.css
- sql - 如何在 1 个表中组合 2 个查询?
- javascript - JavaScript:从 Icons 渲染图标时出错 - Material Design
- php - 查询多个 WHERE 和 CASE PHP(CodeIgniter) MySQL
- git - 带有压缩档案的 Git
- python - 有没有办法让python请求通过用户输入来禁用ssl验证?
- git - 如何强制 git rebase 并覆盖合并冲突