图像处理之一阶微分应用 .

来源:互联网 发布:arm linux 版本 编辑:程序博客网 时间:2024/04/27 19:06

转自:http://blog.csdn.net/jia20003/article/details/7562092

图像处理之一阶微分应用

一:数学背景

首先看一下一维的微分公式Δf = f(x+1) – f(x), 对于一幅二维的数字图像f(x,y)而言,需要完

成XY两个方向上的微分,所以有如下的公式:

分别对X,Y两个方向上求出它们的偏微分,最终得到梯度Delta F.

对于离散的图像来说,一阶微分的数学表达相当于两个相邻像素的差值,根据选择的梯度算

子不同,效果可能有所不同,但是基本原理不会变化。最常见的算子为Roberts算子,其它

常见还有Sobel,Prewitt等算子。以Roberts算子为例的X,Y的梯度计算演示如下图:


二:图像微分应用

图像微分(梯度计算)是图像边缘提取的重要的中间步骤,根据X,Y方向的梯度向量值,可以

得到如下两个重要参数振幅magnitude, 角度theta,计算公式如下:


Theta = tan-1(yGradient/xGradient)

magnitude表示边缘强度信息

theta预言边缘的方向走势。

假如对一幅数字图像,求出magnitude之后与原来每个像素点对应值相加,则图像边缘将被

大大加强,轮廓更加明显,是一个很典型的sharp filter的效果。

 

三:程序效果

X, Y梯度效果,及magnitude效果


图像微分的Sharp效果:


四:程序源代码

[java] view plaincopyprint?
  1. package com.process.blur.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4.   
  5. // roberts operator   
  6. // X direction 1, 0   
  7. //             0,-1   
  8. // Y direction 0, 1   
  9. //            -1, 0   
  10.   
  11. public class ImageGradientFilter extends AbstractBufferedImageOp {  
  12.     public final static int X_DIRECTION = 0;  
  13.     public final static int Y_DIRECTION = 2;  
  14.     public final static int XY_DIRECTION = 4;  
  15.       
  16.     private boolean sharp;  
  17.     private int direction;  
  18.       
  19.     public ImageGradientFilter() {  
  20.         direction = XY_DIRECTION; // default;  
  21.         sharp = false;  
  22.     }  
  23.       
  24.     public boolean isSharp() {  
  25.         return sharp;  
  26.     }  
  27.   
  28.     public void setSharp(boolean sharp) {  
  29.         this.sharp = sharp;  
  30.     }  
  31.   
  32.     public int getDirection() {  
  33.         return direction;  
  34.     }  
  35.   
  36.     public void setDirection(int direction) {  
  37.         this.direction = direction;  
  38.     }  
  39.   
  40.     @Override  
  41.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  42.         int width = src.getWidth();  
  43.         int height = src.getHeight();  
  44.   
  45.         if (dest == null )  
  46.             dest = createCompatibleDestImage( src, null );  
  47.   
  48.         int[] inPixels = new int[width*height];  
  49.         int[] outPixels = new int[width*height];  
  50.         getRGB( src, 00, width, height, inPixels );  
  51.         int index = 0;  
  52.         double mred, mgreen, mblue;  
  53.         int newX, newY;  
  54.         int index1, index2, index3;  
  55.         for(int row=0; row<height; row++) {  
  56.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  57.             for(int col=0; col<width; col++) {  
  58.                 index = row * width + col;  
  59.                   
  60.                 // base on roberts operator  
  61.                 newX = col + 1;  
  62.                 newY = row + 1;  
  63.                 if(newX > 0 && newX < width) {  
  64.                     newX = col + 1;  
  65.                 } else {  
  66.                     newX = 0;  
  67.                 }  
  68.                   
  69.                 if(newY > 0 && newY < height) {  
  70.                     newY = row + 1;  
  71.                 } else {  
  72.                     newY = 0;  
  73.                 }  
  74.                 index1 = newY * width + newX;  
  75.                 index2 = row * width + newX;  
  76.                 index3 = newY * width + col;  
  77.                 ta = (inPixels[index] >> 24) & 0xff;  
  78.                 tr = (inPixels[index] >> 16) & 0xff;  
  79.                 tg = (inPixels[index] >> 8) & 0xff;  
  80.                 tb = inPixels[index] & 0xff;  
  81.                   
  82.                 int ta1 = (inPixels[index1] >> 24) & 0xff;  
  83.                 int tr1 = (inPixels[index1] >> 16) & 0xff;  
  84.                 int tg1 = (inPixels[index1] >> 8) & 0xff;  
  85.                 int tb1 = inPixels[index1] & 0xff;  
  86.                   
  87.                 int xgred = tr -tr1;  
  88.                 int xggreen = tg - tg1;  
  89.                 int xgblue = tb - tb1;  
  90.                   
  91.                 int ta2 = (inPixels[index2] >> 24) & 0xff;  
  92.                 int tr2 = (inPixels[index2] >> 16) & 0xff;  
  93.                 int tg2 = (inPixels[index2] >> 8) & 0xff;  
  94.                 int tb2 = inPixels[index2] & 0xff;  
  95.                   
  96.                 int ta3 = (inPixels[index3] >> 24) & 0xff;  
  97.                 int tr3 = (inPixels[index3] >> 16) & 0xff;  
  98.                 int tg3 = (inPixels[index3] >> 8) & 0xff;  
  99.                 int tb3 = inPixels[index3] & 0xff;  
  100.                   
  101.                 int ygred = tr2 - tr3;  
  102.                 int yggreen = tg2 - tg3;  
  103.                 int ygblue = tb2 - tb3;  
  104.                   
  105.                 mred = Math.sqrt(xgred * xgred + ygred * ygred);  
  106.                 mgreen = Math.sqrt(xggreen * xggreen + yggreen * yggreen);  
  107.                 mblue = Math.sqrt(xgblue * xgblue + ygblue * ygblue);  
  108.                 if(sharp) {  
  109.                     tr = (int)(tr + mred);  
  110.                     tg = (int)(tg + mgreen);  
  111.                     tb = (int)(tb + mblue);  
  112.                     outPixels[index] = (ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);  
  113.                 } else {  
  114.                     outPixels[index] = (ta << 24) | (clamp((int)mred) << 16) | (clamp((int)mgreen) << 8) | clamp((int)mblue);  
  115.                     // outPixels[index] = (ta << 24) | (clamp((int)ygred) << 16) | (clamp((int)yggreen) << 8) | clamp((int)ygblue);  
  116.                     // outPixels[index] = (ta << 24) | (clamp((int)xgred) << 16) | (clamp((int)xggreen) << 8) | clamp((int)xgblue);  
  117.                 }  
  118.                   
  119.                   
  120.             }  
  121.         }  
  122.         setRGB(dest, 00, width, height, outPixels );  
  123.         return dest;  
  124.     }  
  125.   
  126.     public static int clamp(int c) {  
  127.         if (c < 0)  
  128.             return 0;  
  129.         if (c > 255)  
  130.             return 255;  
  131.         return c;  
  132.     }  
  133. }  
原创粉丝点击