圖像處理------ 二值膨脹及應用 分類: 視頻圖像處理 2015-07-24 10:08 46人閱讀 評論(0) 收藏

基本原理:java

膨脹是圖像形態學的兩個基本操做之一,另一個是腐蝕操做。最典型的應用是在二值圖像ide

中使用這兩個基本操做,是不少識別技術中重要的中間處理步驟。在灰度圖像中根據閾值同this

樣能夠完成膨脹與腐蝕操做。對一幅二值圖像f(x,y)完成膨脹操做,與對圖像的卷積操做類url

似,要有個操做數矩陣,最多見的爲3X3的矩陣,與卷積操做不一樣的,是若是矩陣中的像素spa

點有任意一個點的值是前景色,則設置中心像素點爲前景色,不然不變。.net

 

程序效果:(上爲源圖,下爲膨脹之後效果)orm


程序原理:blog

首先把一幅彩色圖像轉換爲灰度圖像,轉換方法參見這裏ip

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

然根據像素平均值做爲閾值,轉換爲二值圖像,轉換方法參見這裏

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

最後在二值圖像上使用膨脹操做,輸出處理之後圖像

源代碼:

[java]  view plain copy
  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.顯示相減之後的圖像,即獲得邊緣。

相關文章
相關標籤/搜索