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

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">连接组件标记算法(connected component labeling algorithm)是图像分析中最常用的算法之一,</p><p style="color: rgb(51, 51, 51); font-

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

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

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

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

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

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

 

算法流程如下:

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

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

 

算法实现Class文件解释:

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

同时完成了一些公共方法

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

继承上面的自抽象类

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

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

 

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

 

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

 

算法运行结果:

 

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


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

[java]  view plain copy
  1. public class Label {  
  2.       
  3.     private int index;  
  4.     private Label root;  
  5.       
  6.     public Label(int index) {  
  7.         this.index = index;  
  8.         this.root = this;  
  9.     }  
  10.       
  11.     public Label getRoot() {  
  12.         if(this.root != this) {  
  13.             this.root = this.root.getRoot();  
  14.         }  
  15.         return root;  
  16.     }  
  17.   
  18.     public int getIndex() {  
  19.         return index;  
  20.     }  
  21.   
  22.     public void setIndex(int index) {  
  23.         this.index = index;  
  24.     }  
  25.   
  26.     public void setRoot(Label root) {  
  27.         this.root = root;  
  28.     }  
  29. }  

Pixelnfo的代码如下:

[java]  view plain copy
  1. package com.gloomyfish.image.analysis;  
  2.   
  3. public class PixelInfo {  
  4.     private int value; // pixel value  
  5.     private int xp;  
  6.     private int yp;  
  7.       
  8.     public PixelInfo(int pixelValue, int yp, int xp) {  
  9.         this.value = pixelValue;  
  10.         this.yp = yp;  
  11.         this.xp = xp;  
  12.     }  
  13.       
  14.     public int getValue() {  
  15.         return value;  
  16.     }  
  17.     public void setValue(int value) {  
  18.         this.value = value;  
  19.     }  
  20.     public int getXp() {  
  21.         return xp;  
  22.     }  
  23.     public void setXp(int xp) {  
  24.         this.xp = xp;  
  25.     }  
  26.     public int getYp() {  
  27.         return yp;  
  28.     }  
  29.     public void setYp(int yp) {  
  30.         this.yp = yp;  
  31.     }  
  32. }  
抽象的组件连通标记算法Class如下:

[java]  view plain copy
  1. public abstract class AbstractConnectedComponentLabel {  
  2.       
  3.     protected int width;  
  4.     protected int height;  
  5.     protected Color fgColor;  
  6.     protected int[] inPixels;  
  7.     protected int[][] chessborad;  
  8.     protected Map<Integer, Integer> neighbourMap;  
  9.       
  10.     public int getWidth() {  
  11.         return width;  
  12.     }  
  13.   
  14.     public void setWidth(int width) {  
  15.         this.width = width;  
  16.     }  
  17.   
  18.     public int getHeight() {  
  19.         return height;  
  20.     }  
  21.   
  22.     public void setHeight(int height) {  
  23.         this.height = height;  
  24.     }  
  25.       
  26.     public abstract Map<Integer, List<PixelInfo>> doConntectedLabel();  
  27.   
  28.     public boolean isForeGround(int tr, int tg, int tb) {  
  29.         if(tr == fgColor.getRed() && tg == fgColor.getGreen() && tb == fgColor.getBlue()) {  
  30.             return true;  
  31.         } else {  
  32.             return false;  
  33.         }  
  34.           
  35.     }  
  36.   
  37. }  
实现抽象类的算法one的代码如下:

[java]  view plain copy
  1. import java.awt.Color;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. public class ConnectedComponentLabelAlgOne extends AbstractConnectedComponentLabel {  
  8.   
  9.     public ConnectedComponentLabelAlgOne(Color fgColor, int[] srcPixel, int width, int height) {  
  10.         this.fgColor = fgColor;  
  11.         this.width = width;  
  12.         this.height = height;  
  13.         this.inPixels = srcPixel;  
  14.         this.chessborad = new int[height][width];  
  15.         for(int i=0; i<height; i++) {  
  16.             for(int j=0; j<width; j++) {  
  17.                 chessborad[i][j] = 0;  
  18.             }  
  19.         }  
  20.         this.neighbourMap = new HashMap<Integer, Integer>();   
  21.     }  
  22.       
  23.     // assume the input image data is binary image.  
  24.     public Map<Integer, List<PixelInfo>> doConntectedLabel() {  
  25.         System.out.println("start to do connected component labeling algorithm");  
  26.         int index = 0;  
  27.         int labelCount = 0;  
  28.         Label currentLabel = new Label(0);  
  29.         HashMap<Integer, Label> allLabels = new HashMap<Integer, Label>();  
  30.         for(int row=0; row<height; row++) {  
  31.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  32.             for(int col=0; col<width; col++) {  
  33.                 index = row * width + col;  
  34.                 ta = (inPixels[index] >> 24) & 0xff;  
  35.                 tr = (inPixels[index] >> 16) & 0xff;  
  36.                 tg = (inPixels[index] >> 8) & 0xff;  
  37.                 tb = inPixels[index] & 0xff;  
  38.                 if(isForeGround(tr, tg, tb)) {  
  39.                     getNeighboringLabels(row, col);  
  40.                     if(neighbourMap.size() == 0) {  
  41.                         currentLabel.setIndex(++labelCount);  
  42.                         allLabels.put(labelCount,new Label(labelCount));  
  43.   
  44.                     } else {  
  45.                         for(Integer pixelLabel : neighbourMap.keySet().toArray(new Integer[0])) {  
  46.                             currentLabel.setIndex(pixelLabel);  
  47.                             break;  
  48.                         }  
  49.                         mergeLabels(currentLabel.getIndex(), neighbourMap, allLabels);  
  50.                     }  
  51.                     chessborad[row][col] = currentLabel.getIndex();  
  52.                 }  
  53.             }  
  54.         }  
  55.           
  56.         Map<Integer, List<PixelInfo>> connectedLabels = consolidateAllLabels(allLabels);  
  57.         return connectedLabels;  
  58.     }  
  59.       
  60.     private Map<Integer, List<PixelInfo>> consolidateAllLabels(HashMap<Integer, Label> allLabels) {  
  61.         Map<Integer, List<PixelInfo>> patterns = new HashMap<Integer, List<PixelInfo>>();  
  62.         int patternNumber;  
  63.         List<PixelInfo> shape;  
  64.         for (int i = 0; i < this.height; i++)  
  65.         {  
  66.             for (int j = 0; j < this.width; j++)  
  67.             {  
  68.                 patternNumber = chessborad[i][j];  
  69.                 if (patternNumber != 0)  
  70.                 {  
  71.                     patternNumber = allLabels.get(patternNumber).getRoot().getIndex();  
  72.                     if (!patterns.containsKey(patternNumber))  
  73.                     {  
  74.                         shape = new ArrayList<PixelInfo>();  
  75.                         shape.add(new PixelInfo(Color.BLUE.getRGB(), i, j));  
  76.                     }  
  77.                     else  
  78.                     {  
  79.                         shape = patterns.get(patternNumber);  
  80.                         shape.add(new PixelInfo(Color.BLUE.getRGB(), i, j));  
  81.                     }  
  82.                     patterns.put(patternNumber, shape);  
  83.                 }  
  84.             }  
  85.         }  
  86.         return patterns;  
  87.     }  
  88.   
  89.     private void mergeLabels(int index, Map<Integer, Integer> neighbourMap,  
  90.             HashMap<Integer, Label> allLabels) {  
  91.         Label root = allLabels.get(index).getRoot();  
  92.         Label neighbour;  
  93.         for(Integer key : neighbourMap.keySet().toArray(new Integer[0])) {  
  94.              if (key != index)  
  95.              {  
  96.                  neighbour = allLabels.get(key);  
  97.                  if(neighbour.getRoot() != root) {  
  98.                      neighbour.setRoot(neighbour.getRoot());// thanks zhen712,  
  99.                  }  
  100.              }  
  101.         }  
  102.           
  103.     }  
  104.   
  105.     /** 
  106.      * get eight neighborhood pixels 
  107.      *  
  108.      * @param row 
  109.      * @param col 
  110.      * @return 
  111.      */  
  112.     public  void getNeighboringLabels(int row, int col) {  
  113.         neighbourMap.clear();  
  114.         for(int i=-1; i<=1; i++) {  
  115.             int yp = row + i;  
  116.             if(yp >=0 && yp < this.height) {  
  117.                 for(int j=-1; j<=1; j++) {  
  118.                     if(i == 0 && j==0continue// ignore/skip center pixel/itself  
  119.                     int xp = col + j;  
  120.                     if(xp >=0 && xp < this.width) {  
  121.                         if(chessborad[yp][xp] != 0) {  
  122.                             if(!neighbourMap.containsKey(chessborad[yp][xp])) {  
  123.                                 neighbourMap.put(chessborad[yp][xp],0);  
  124.                             }  
  125.                         }  
  126.                     }  
  127.                 }  
  128.             }  
  129.         }  
  130.     }  
  131. }  
相关文章
|
27天前
|
机器学习/深度学习 人工智能 监控
AI算法分析,智慧城管AI智能识别系统源码
AI视频分析技术应用于智慧城管系统,通过监控摄像头实时识别违法行为,如违规摆摊、垃圾、违章停车等,实现非现场执法和预警。算法平台检测街面秩序(出店、游商、机动车、占道)和市容环境(垃圾、晾晒、垃圾桶、路面不洁、漂浮物、乱堆物料),助力及时处理问题,提升城市管理效率。
AI算法分析,智慧城管AI智能识别系统源码
|
1月前
|
算法
经典控制算法——PID算法原理分析及优化
这篇文章介绍了PID控制算法,这是一种广泛应用的控制策略,具有简单、鲁棒性强的特点。PID通过比例、积分和微分三个部分调整控制量,以减少系统误差。文章提到了在大学智能汽车竞赛中的应用,并详细解释了PID的基本原理和数学表达式。接着,讨论了数字PID的实现,包括位置式、增量式和步进式,以及它们各自的优缺点。最后,文章介绍了PID的优化方法,如积分饱和处理和微分项优化,以及串级PID在电机控制中的应用。整个内容旨在帮助读者理解PID控制的原理和实际运用。
72 1
|
1月前
|
算法 调度
【算法设计与分析】— —基础概念题(one)可作为日常联系或期末复习
【算法设计与分析】— —基础概念题(one)可作为日常联系或期末复习
47 1
|
1月前
|
机器学习/深度学习 算法 数据库
KNN和SVM实现对LFW人像图像数据集的分类应用
KNN和SVM实现对LFW人像图像数据集的分类应用
33 0
|
2天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
11 0
|
3天前
|
算法 定位技术 Windows
R语言最大流最小割定理和最短路径算法分析交通网络流量拥堵问题
R语言最大流最小割定理和最短路径算法分析交通网络流量拥堵问题
10 4
|
3天前
|
文字识别 算法 计算机视觉
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
12 0
|
26天前
|
算法
TOP-K问题和向上调整算法和向下调整算法的时间复杂度问题的分析
TOP-K问题和向上调整算法和向下调整算法的时间复杂度问题的分析
17 1
|
28天前
|
算法
PID算法原理分析及优化
这篇文章介绍了PID控制方法,一种广泛应用于机电、冶金等行业的经典控制算法。PID通过比例、积分、微分三个部分调整控制量,以适应系统偏差。文章讨论了比例调节对系统响应的直接影响,积分调节如何消除稳态误差,以及微分调节如何减少超调。还提到了数字PID的实现,包括位置式、增量式和步进式,并探讨了积分饱和和微分项的优化策略。最后,文章简述了串级PID在电机控制中的应用,并强调了PID控制的灵活性和实用性。
38 1
|
1月前
|
存储 人工智能 算法
大数乘法的几种算法分析及比较(2014腾讯南京笔试题)
大数乘法的几种算法分析及比较(2014腾讯南京笔试题)