图像处理------ 二值膨胀及应用 分类: 视频图像处理 2015-07-24 10:08 46人阅读 评论(0) 收藏

基本原理:

膨胀是图像形态学的两个基本操作之一,另外一个是腐蚀操作。最典型的应用是在二值图像

中使用这两个基本操作,是很多识别技术中重要的中间处理步骤。在灰度图像中根据阈值同

样可以完成膨胀与腐蚀操作。对一幅二值图像f(x,y)完成膨胀操作,与对图像的卷积操作类

似,要有个操作数矩阵,最常见的为3X3的矩阵,与卷积操作不同的,是如果矩阵中的像素

点有任意一个点的值是前景色,则设置中心像素点为前景色,否则不变。

 

程序效果:(上为源图,下为膨胀以后效果)


程序原理:

首先把一幅彩色图像转换为灰度图像,转换方法参见这里

http://blog.csdn.net/jia20003/article/details/7392325

然根据像素平均值作为阈值,转换为二值图像,转换方法参见这里

http://blog.csdn.net/jia20003/article/details/7392325

最后在二值图像上使用膨胀操作,输出处理以后图像

源代码:

  1. package com.gloomyfish.morphology;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.image.BufferedImage;  
  5.   
  6. public class DilateFilter extends BinaryFilter {  
  7.       
  8.     public DilateFilter() {  
  9.         forgeColor = Color.WHITE;  
  10.     }  
  11.       
  12.     private Color forgeColor;  
  13.   
  14.     public Color getForgeColor() {  
  15.         return forgeColor;  
  16.     }  
  17.   
  18.     public void setForgeColor(Color forgeColor) {  
  19.         this.forgeColor = forgeColor;  
  20.     }  
  21.   
  22.     @Override  
  23.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  24.         int width = src.getWidth();  
  25.         int height = src.getHeight();  
  26.   
  27.         if ( dest == null )  
  28.             dest = createCompatibleDestImage( src, null );  
  29.   
  30.         int[] inPixels = new int[width*height];  
  31.         int[] outPixels = new int[width*height];  
  32.         src = super.filter(src, null); // we need to create new one  
  33.         getRGB( src, 00, width, height, inPixels );  
  34.         int index = 0, index1 = 0, newRow = 0, newCol = 0;  
  35.         int ta1 = 0, tr1 = 0, tg1 = 0, tb1 = 0;  
  36.         for(int row=0; row<height; row++) {  
  37.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  38.             for(int col=0; col<width; col++) {  
  39.                 index = row * width + col;  
  40.                 ta = (inPixels[index] >> 24) & 0xff;  
  41.                 tr = (inPixels[index] >> 16) & 0xff;  
  42.                 tg = (inPixels[index] >> 8) & 0xff;  
  43.                 tb = inPixels[index] & 0xff;  
  44.                 boolean dilation = false;  
  45.                 for(int offsetY=-1; offsetY<=1; offsetY++) {  
  46.                     for(int offsetX=-1; offsetX<=1; offsetX++) {  
  47.                         if(offsetY==0 && offsetX==0) {  
  48.                             continue;  
  49.                         }  
  50.                         newRow = row + offsetY;  
  51.                         newCol = col + offsetX;  
  52.                         if(newRow <0 || newRow >=height) {  
  53.                             newRow = 0;  
  54.                         }  
  55.                         if(newCol < 0 || newCol >=width) {  
  56.                             newCol = 0;  
  57.                         }  
  58.                         index1 = newRow * width + newCol;  
  59.                         ta1 = (inPixels[index1] >> 24) & 0xff;  
  60.                         tr1 = (inPixels[index1] >> 16) & 0xff;  
  61.                         tg1= (inPixels[index1] >> 8) & 0xff;  
  62.                         tb1 = inPixels[index1] & 0xff;  
  63.                         if(tr1 == forgeColor.getRed() && tg1 == tb1) {  
  64.                             dilation = true;  
  65.                             break;  
  66.                         }  
  67.                     }  
  68.                     if(dilation){  
  69.                         break;  
  70.                     }  
  71.                 }  
  72.                   
  73.                 if(dilation) {  
  74.                     tr = tg = tb = forgeColor.getRed();  
  75.                 } else {  
  76.                     tr = tg = tb = 255 - forgeColor.getRed();  
  77.                 }  
  78.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
  79.             }  
  80.         }  
  81.         setRGB( dest, 00, width, height, outPixels );  
  82.         return dest;  
  83.     }  
  84.   
  85. }  
其实,膨胀还可以被用来进行对二值图像完成边缘提取,其基本做法如下:

1. 对一副黑白的图像完成膨胀操作

2.将膨胀以后的图像与原来的图像在每个像素位上相减

3.显示相减以后的图像,即得到边缘。

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/mao0504/p/4706360.html