图像分析之连通组件标记算法

简介: 图像分析之连通组件标记算法

图像处理之连接组件标记算法



连接组件标记算法(connected component labeling algorithm)是图像分析中最常用的算法之一,


算法的实质是扫描一幅图像的每个像素,对于像素值相同的分为相同的组(group),最终得到


图像中所有的像素连通组件。扫描的方式可以是从上到下,从左到右,对于一幅有N个像


素的图像来说,最大连通组件个数为N/2。扫描是基于每个像素单位,对于二值图像而言,


连通组件集合可以是V={1}或者V={0}, 取决于前景色与背景色的不同。对于灰度图像来说,


连图组件像素集合可能是一系列在0 ~ 255之间的灰度值。



算法流程如下:


1.      首先扫描当前像素相邻的八邻域像素值,发现连通像素加以标记。


2.      完全扫描所有像素点之后,根据标记将所有连通组件合并。



算法实现Class文件解释:


AbstractConnectedComponentLabel:一个抽象的Class定义了抽象方法doConntectedLabel()


同时完成了一些公共方法


ConnectedComponentLabelAlgOne:一个容易读懂的连接组件算法完成,没有任何优化,


继承上面的自抽象类


ConnectedComponentLabelAlgTwo:一个快速的连接组件算法,基于算法优化,取当前像素


的四邻域完成扫描与标记合并。



Label与PixelInfo是两个数据结构,用来存储算法计算过程中的中间变量。



ImageLabelFilter用来测试算法的驱动类,ImageAnalysisUI是现实测试结果的UI类



算法运行结果:

1334936488_3384.png


根据标记的索引将组件着色。




定义数据结构的代码如下:

public class Label {
  
  private int index;
  private Label root;
  
  public Label(int index) {
    this.index = index;
    this.root = this;
  }
  
  public Label getRoot() {
    if(this.root != this) {
      this.root = this.root.getRoot();
    }
    return root;
  }
 
  public int getIndex() {
    return index;
  }
 
  public void setIndex(int index) {
    this.index = index;
  }
 
  public void setRoot(Label root) {
    this.root = root;
  }
}

Pixelnfo的代码如下:

package com.gloomyfish.image.analysis;
 
public class PixelInfo {
  private int value; // pixel value
  private int xp;
  private int yp;
  
  public PixelInfo(int pixelValue, int yp, int xp) {
    this.value = pixelValue;
    this.yp = yp;
    this.xp = xp;
  }
  
  public int getValue() {
    return value;
  }
  public void setValue(int value) {
    this.value = value;
  }
  public int getXp() {
    return xp;
  }
  public void setXp(int xp) {
    this.xp = xp;
  }
  public int getYp() {
    return yp;
  }
  public void setYp(int yp) {
    this.yp = yp;
  }
}

抽象的组件连通标记算法Class如下:

public abstract class AbstractConnectedComponentLabel {
  
  protected int width;
  protected int height;
  protected Color fgColor;
  protected int[] inPixels;
  protected int[][] chessborad;
  protected Map<Integer, Integer> neighbourMap;
  
  public int getWidth() {
    return width;
  }
 
  public void setWidth(int width) {
    this.width = width;
  }
 
  public int getHeight() {
    return height;
  }
 
  public void setHeight(int height) {
    this.height = height;
  }
  
  public abstract Map<Integer, List<PixelInfo>> doConntectedLabel();
 
  public boolean isForeGround(int tr, int tg, int tb) {
    if(tr == fgColor.getRed() && tg == fgColor.getGreen() && tb == fgColor.getBlue()) {
      return true;
    } else {
      return false;
    }
    
  }
 
}

实现抽象类的算法one的代码如下:

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class ConnectedComponentLabelAlgOne extends AbstractConnectedComponentLabel {
 
  public ConnectedComponentLabelAlgOne(Color fgColor, int[] srcPixel, int width, int height) {
    this.fgColor = fgColor;
    this.width = width;
    this.height = height;
    this.inPixels = srcPixel;
    this.chessborad = new int[height][width];
    for(int i=0; i<height; i++) {
      for(int j=0; j<width; j++) {
        chessborad[i][j] = 0;
      }
    }
    this.neighbourMap = new HashMap<Integer, Integer>(); 
  }
  
  // assume the input image data is binary image.
  public Map<Integer, List<PixelInfo>> doConntectedLabel() {
    System.out.println("start to do connected component labeling algorithm");
    int index = 0;
    int labelCount = 0;
    Label currentLabel = new Label(0);
    HashMap<Integer, Label> allLabels = new HashMap<Integer, Label>();
        for(int row=0; row<height; row++) {
          int ta = 0, tr = 0, tg = 0, tb = 0;
          for(int col=0; col<width; col++) {
            index = row * width + col;
            ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                if(isForeGround(tr, tg, tb)) {
                  getNeighboringLabels(row, col);
                  if(neighbourMap.size() == 0) {
                    currentLabel.setIndex(++labelCount);
                    allLabels.put(labelCount,new Label(labelCount));
 
                  } else {
                    for(Integer pixelLabel : neighbourMap.keySet().toArray(new Integer[0])) {
                      currentLabel.setIndex(pixelLabel);
                      break;
                    }
                    mergeLabels(currentLabel.getIndex(), neighbourMap, allLabels);
                  }
                  chessborad[row][col] = currentLabel.getIndex();
                }
          }
        }
        
        Map<Integer, List<PixelInfo>> connectedLabels = consolidateAllLabels(allLabels);
    return connectedLabels;
  }
  
  private Map<Integer, List<PixelInfo>> consolidateAllLabels(HashMap<Integer, Label> allLabels) {
    Map<Integer, List<PixelInfo>> patterns = new HashMap<Integer, List<PixelInfo>>();
        int patternNumber;
        List<PixelInfo> shape;
        for (int i = 0; i < this.height; i++)
        {
            for (int j = 0; j < this.width; j++)
            {
                patternNumber = chessborad[i][j];
                if (patternNumber != 0)
                {
                    patternNumber = allLabels.get(patternNumber).getRoot().getIndex();
                    if (!patterns.containsKey(patternNumber))
                    {
                        shape = new ArrayList<PixelInfo>();
                        shape.add(new PixelInfo(Color.BLUE.getRGB(), i, j));
                    }
                    else
                    {
                        shape = patterns.get(patternNumber);
                        shape.add(new PixelInfo(Color.BLUE.getRGB(), i, j));
                    }
                    patterns.put(patternNumber, shape);
                }
            }
        }
    return patterns;
  }
 
  private void mergeLabels(int index, Map<Integer, Integer> neighbourMap,
      HashMap<Integer, Label> allLabels) {
    Label root = allLabels.get(index).getRoot();
    Label neighbour;
    for(Integer key : neighbourMap.keySet().toArray(new Integer[0])) {
       if (key != index)
             {
         neighbour = allLabels.get(key);
         if(neighbour.getRoot() != root) {
           neighbour.setRoot(neighbour.getRoot());// thanks zhen712,
         }
             }
    }
    
  }
 
  /**
   * get eight neighborhood pixels
   * 
   * @param row
   * @param col
   * @return
   */
  public  void getNeighboringLabels(int row, int col) {
    neighbourMap.clear();
    for(int i=-1; i<=1; i++) {
      int yp = row + i;
      if(yp >=0 && yp < this.height) {
        for(int j=-1; j<=1; j++) {
          if(i == 0 && j==0) continue; // ignore/skip center pixel/itself
          int xp = col + j;
          if(xp >=0 && xp < this.width) {
            if(chessborad[yp][xp] != 0) {
              if(!neighbourMap.containsKey(chessborad[yp][xp])) {
                neighbourMap.put(chessborad[yp][xp],0);
              }
            }
          }
        }
      }
    }
  }
}

测试代码可以参考以前的文章 -  

Java数字图像处理基础知识 - 必读

相关文章
|
机器学习/深度学习 算法 机器人
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
1133 0
|
编解码 算法
改进SIFT算法实现光学图像和SAR图像配准
改进SIFT算法实现光学图像和SAR图像配准
|
数据采集 机器学习/深度学习 算法
别急着上算法,咱先把数据整明白:大数据分析的5个基本步骤,你都搞对了吗?
别急着上算法,咱先把数据整明白:大数据分析的5个基本步骤,你都搞对了吗?
1209 4
|
机器学习/深度学习 边缘计算 算法
NOMA和OFDMA优化算法分析
NOMA和OFDMA优化算法分析
708 127
|
机器学习/深度学习 算法 自动驾驶
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
824 8
|
运维 监控 JavaScript
基于 Node.js 图结构的局域网设备拓扑分析算法在局域网内监控软件中的应用研究
本文探讨图结构在局域网监控系统中的应用,通过Node.js实现设备拓扑建模、路径分析与故障定位,提升网络可视化、可追溯性与运维效率,结合模拟实验验证其高效性与准确性。
770 3
|
存储 监控 算法
基于文化优化算法图像量化(Matlab代码实现)
基于文化优化算法图像量化(Matlab代码实现)
636 1
|
存储 边缘计算 算法
【太阳能学报EI复现】基于粒子群优化算法的风-水电联合优化运行分析(Matlab代码实现)
【太阳能学报EI复现】基于粒子群优化算法的风-水电联合优化运行分析(Matlab代码实现)
268 0
|
存储 算法 生物认证
基于Zhang-Suen算法的图像细化处理FPGA实现,包含testbench和matlab验证程序
本项目基于Zhang-Suen算法实现图像细化处理,支持FPGA与MATLAB双平台验证。通过对比,FPGA细化效果与MATLAB一致,可有效减少图像数据量,便于后续识别与矢量化处理。算法适用于字符识别、指纹识别等领域,配套完整仿真代码及操作说明。
|
编解码 算法 5G
MIMO雷达空间谱估计中Capon算法与MUSIC算法的对比分析及实现
MIMO雷达空间谱估计中Capon算法与MUSIC算法的对比分析及实现
1216 2

热门文章

最新文章