Java Image Connective组件 [英] Java Image Connective Component

查看:144
本文介绍了Java Image Connective组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序来检测二值化BufferedImage中的对象,该图像是一个多选答案表。

我正在尝试使用4连接来检测每个对象(回答表格。

到目前为止,我手头的资料是:

I have this program to detect objects in a binarized BufferedImage, which the image is a multiple choice answer sheet.
I'm trying to use 4-Connectivity as in to detect each object (answer on the sheet).
So far what I have in hand as source is these:


  1. http://en.wikipedia.org/wiki/Connected-component_labeling

  2. http://homepages.inf.ed.ac.uk/rbf /HIPR2/label.htm

  1. http://en.wikipedia.org/wiki/Connected-component_labeling
  2. http://homepages.inf.ed.ac.uk/rbf/HIPR2/label.htm

我按照维基百科的指示想出了这个:

and I have came up with this, following instructions from Wikipedia:

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个区域!并且它只打印每个对象的几个顶部像素(不是所有像素)。

任何人都可以帮我找到问题是什么以及如何检测我的对象?

我将不胜感激任何帮助。

But the problem is it creates 51 regions! and it only prints a couple of top pixels of each object (not all pixels).
Can anyone please help me to find what the problem is and how can I detect my objects?
I would appreciate any help.

推荐答案

您可能会获得很多地区,因为您似乎没有合并相同的标签。您的代码段中没有用于存储相同标签的代码。该算法是一个两遍算法,其中第一个传递分配标签,第二个传递合并相同的标签。

You probably get a lot of regions because you do not seem to merge equal labels. There is no code for storing equal labels in your code snippet. The algorithm is a two-pass algorithm where the first pass assigns labels and the second pass merges equal labels.

以下是引用的条件检查来自维基百科页面:

Here are the condition checks quoted from the Wikipedia page:


要检查的条件:

Conditions to check:


  1. 左侧(西侧)的像素是否与当前像素具有相同的值?
  1. Does the pixel to the left (West) have the same value as the current pixel?

  1. - 我们在同一地区。为当前像素指定相同的标签

  2. - 检查下一个条件

  1. Yes – We are in the same region. Assign the same label to the current pixel
  2. No – Check next condition


  • 当前像素的北和西两个像素是否与当前像素具有相同的值但不是相同的标签?

  • Do both pixels to the North and West of the current pixel have the same value as the current pixel but not the same label?


    1. - 我们知道North和West像素属于同一个区域,必须合并。将当前像素指定为北和西标签的最小值,并记录它们的等价关系

    2. - 检查下一个条件

    1. Yes – We know that the North and West pixels belong to the same region and must be merged. Assign the current pixel the minimum of the North and West labels, and record their equivalence relationship
    2. No – Check next condition


  • 左边(西边)的像素是否具有不同的值,而北方的像素与当前像素的值是否相同?

  • Does the pixel to the left (West) have a different value and the one to the North the same value as the current pixel?


    1. - 将北像素的标签指定给当前像素

    2. - 检查下一个条件

    1. Yes – Assign the label of the North pixel to the current pixel
    2. No – Check next condition


  • 像素的North和West邻居的像素值是否与当前像素不同?

  • Do the pixel's North and West neighbors have different pixel values than current pixel?


    1. - 创建新的标签ID并将其分配给当前像素

    1. Yes – Create a new label id and assign it to the current pixel



  • 您的第二次条件检查,

    else if(getPixel(image, x-1, y) !=0 && getPixel(image, x, y-1) !=0)
    

    不会检查左边的像素和上边的像素是否有不同的标签。

    does not check if the left pixel and up pixel have different labels.

    此外,就像评论中提到的supersam654一样,第一个 else,如果将永远不会被调用。维基百科页面上的条件检查(1)和(2)似乎应该是相反的顺序。也就是说,首先检查左和上像素是否具有与当前像素相同的值但不是相同的标签。然后,如果检查失败,请检查左侧像素是否与当前值相同。

    Furthermore, just like supersam654 mentions in the comment, the first else if will never be called. It seems like condition check (1) and (2) on the Wikipedia page should be in the opposite order. That is, first check if the left and up pixel have the same value as the current pixel but not the same label. Then, if that check fails, check if the left pixel has the same value as the current.

    请尝试以下操作:


    1. 将标签条件添加到第二次条件检查。

    2. 切换前两次条件检查的顺序。

    3. 跟踪相同的标签(即哪些标签代表相同的区域)。

    4. 对图像进行第二次传递并合并相同的标签。

    1. Add the label condition to your second condition check.
    2. Switch the order of your first two condition checks.
    3. Keep track of equal labels (i.e., which labels represent the same region).
    4. Do a second pass over the image and merge equal labels.

    我希望这有帮助。

    这篇关于Java Image Connective组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆