图像处理------ 二值腐蚀 分类: 视频图像处理 2015-07-24 10:09 40人阅读 评论(0) 收藏

概述:

腐蚀是图像形态学的两个基本操作之一,另外一个是膨胀(Dilate)。二值图像上的腐蚀是腐蚀最典

型的运用,但是腐蚀操作同样可以运用于灰度图像。二值图像腐蚀操作最基本的效果是腐蚀图像

中前景色区域的边缘。使得前景图像区域变小,前景图像内部的背景区域被放大。

 

基本原理:

腐蚀操作要求有待处理的2D图像F(x,y)以及操作数矩阵(类似卷积操作中的Kernel矩阵),常见的

为3X3的操作数矩阵。二值图像腐蚀操作的数学定义如下:

1.      假设X是二值图像中所有像素欧几里德坐标的集合,K为3X3的操作数矩阵

2.       Kx表示操作数处理X的结果,x表示起始像素点

3.      腐蚀操作K对X的所有像素点操作,Kx是X所有像素点的子集。

一个二值图像腐蚀的例子如下,操作数矩阵为3X3,起始点为中心像素点,前景像素表示为1,背

景像素表示为0.图示如下:


当操作数在像素矩阵上移动时,任何一个在操作数之下的输入像素为背景像素时,则设置中心像素

为背景像素0,否则中心像素[0,0]下的输入像素值不变。

 

三:程序效果


四:源代码

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

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

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