图像处理------图像梯度效果

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>基本思想:</strong></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-heig

基本思想:

利用X方向与Y方向分别实现一阶微分,求取振幅,实现图像梯度效果。

使用的两种微分算子分别为Prewitt与Sobel,其中Soble在X, Y两个方向算子分别为:


Prewitt在X, Y方向上梯度算子分别为:


二:程序思路及实现

梯度滤镜提供了两个参数:

– 方向,用来要决定图像完成X方向梯度计算, Y方向梯度计算,或者是振幅计算

– 算子类型,用来决定是使用sobel算子或者是prewitt算子。

计算振幅的公式可以参见以前《图像处理之一阶微分应用》的文章


 三:运行效果

原图像如下:


基于Prewitt与sobel算子的XY方向振幅效果如下:



该滤镜的源代码如下:

[java]  view plain copy
  1. package com.process.blur.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. /** 
  5.  *  
  6.  * @author gloomy-fish 
  7.  * @date 2012-06-11 
  8.  *  
  9.  * prewitt operator  
  10.  * X-direction 
  11.  * -1, 0, 1 
  12.  * -1, 0, 1 
  13.  * -1, 0, 1 
  14.  *  
  15.  * Y-direction 
  16.  * -1, -1, -1 
  17.  *  0,  0,  0 
  18.  *  1,  1,  1 
  19.  *   
  20.  * sobel operator 
  21.  * X-direction 
  22.  * -1, 0, 1 
  23.  * -2, 0, 2 
  24.  * -1, 0, 1 
  25.  *  
  26.  * Y-direction 
  27.  * -1, -2, -1 
  28.  *  0,  0,  0 
  29.  *  1,  2,  1 
  30.  * 
  31.  */  
  32. public class GradientFilter extends AbstractBufferedImageOp {  
  33.   
  34.     // prewitt operator  
  35.     public final static int[][] PREWITT_X = new int[][]{{-101}, {-101}, {-101}};  
  36.     public final static int[][] PREWITT_Y = new int[][]{{-1, -1, -1}, {0,  0,  0}, {1,  1,  1}};  
  37.       
  38.     // sobel operator  
  39.     public final static int[][] SOBEL_X = new int[][]{{-101}, {-202}, {-101}};  
  40.     public final static int[][] SOBEL_Y = new int[][]{{-1, -2, -1}, {0,  0,  0}, {1,  2,  1}};  
  41.       
  42.     // direction parameter  
  43.     public final static int X_DIRECTION = 0;  
  44.     public final static int Y_DIRECTION = 2;  
  45.     public final static int XY_DIRECTION = 4;  
  46.     private int direction;  
  47.     private boolean isSobel;  
  48.       
  49.     public GradientFilter() {  
  50.         direction = XY_DIRECTION;  
  51.         isSobel = true;  
  52.     }  
  53.       
  54.     public void setSoble(boolean sobel) {  
  55.         this.isSobel = sobel;  
  56.     }  
  57.   
  58.     public int getDirection() {  
  59.         return direction;  
  60.     }  
  61.   
  62.     public void setDirection(int direction) {  
  63.         this.direction = direction;  
  64.     }  
  65.       
  66.     @Override  
  67.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  68.         int width = src.getWidth();  
  69.         int height = src.getHeight();  
  70.   
  71.         if (dest == null )  
  72.             dest = createCompatibleDestImage( src, null );  
  73.   
  74.         int[] inPixels = new int[width*height];  
  75.         int[] outPixels = new int[width*height];  
  76.         getRGB( src, 00, width, height, inPixels );  
  77.         int index = 0, index2 = 0;  
  78.         double xred = 0, xgreen = 0, xblue = 0;  
  79.         double yred = 0, ygreen = 0, yblue = 0;  
  80.         int newRow, newCol;  
  81.         for(int row=0; row<height; row++) {  
  82.             int ta = 255, tr = 0, tg = 0, tb = 0;  
  83.             for(int col=0; col<width; col++) {  
  84.                 index = row * width + col;  
  85.                 for(int subrow = -1; subrow <= 1; subrow++) {  
  86.                     for(int subcol = -1; subcol <= 1; subcol++) {  
  87.                         newRow = row + subrow;  
  88.                         newCol = col + subcol;  
  89.                         if(newRow < 0 || newRow >= height) {  
  90.                             newRow = row;  
  91.                         }  
  92.                         if(newCol < 0 || newCol >= width) {  
  93.                             newCol = col;  
  94.                         }  
  95.                         index2 = newRow * width + newCol;  
  96.                         tr = (inPixels[index2] >> 16) & 0xff;  
  97.                         tg = (inPixels[index2] >> 8) & 0xff;  
  98.                         tb = inPixels[index2] & 0xff;  
  99.                           
  100.                         if(isSobel) {  
  101.                             xred += (SOBEL_X[subrow + 1][subcol + 1] * tr);  
  102.                             xgreen +=(SOBEL_X[subrow + 1][subcol + 1] * tg);  
  103.                             xblue +=(SOBEL_X[subrow + 1][subcol + 1] * tb);  
  104.                               
  105.                             yred += (SOBEL_Y[subrow + 1][subcol + 1] * tr);  
  106.                             ygreen +=(SOBEL_Y[subrow + 1][subcol + 1] * tg);  
  107.                             yblue +=(SOBEL_Y[subrow + 1][subcol + 1] * tb);  
  108.                         } else {  
  109.                             xred += (PREWITT_X[subrow + 1][subcol + 1] * tr);  
  110.                             xgreen +=(PREWITT_X[subrow + 1][subcol + 1] * tg);  
  111.                             xblue +=(PREWITT_X[subrow + 1][subcol + 1] * tb);  
  112.                               
  113.                             yred += (PREWITT_Y[subrow + 1][subcol + 1] * tr);  
  114.                             ygreen +=(PREWITT_Y[subrow + 1][subcol + 1] * tg);  
  115.                             yblue +=(PREWITT_Y[subrow + 1][subcol + 1] * tb);  
  116.                         }  
  117.                     }  
  118.                 }  
  119.                   
  120.                 double mred = Math.sqrt(xred * xred + yred * yred);  
  121.                 double mgreen = Math.sqrt(xgreen * xgreen + ygreen * ygreen);  
  122.                 double mblue = Math.sqrt(xblue * xblue + yblue * yblue);  
  123.                 if(XY_DIRECTION == direction)   
  124.                 {  
  125.                     outPixels[index] = (ta << 24) | (clamp((int)mred) << 16) | (clamp((int)mgreen) << 8) | clamp((int)mblue);  
  126.                 }   
  127.                 else if(X_DIRECTION == direction)  
  128.                 {  
  129.                     outPixels[index] = (ta << 24) | (clamp((int)yred) << 16) | (clamp((int)ygreen) << 8) | clamp((int)yblue);  
  130.                 }   
  131.                 else if(Y_DIRECTION == direction)   
  132.                 {  
  133.                     outPixels[index] = (ta << 24) | (clamp((int)xred) << 16) | (clamp((int)xgreen) << 8) | clamp((int)xblue);  
  134.                 }   
  135.                 else   
  136.                 {  
  137.                     // as default, always XY gradient  
  138.                     outPixels[index] = (ta << 24) | (clamp((int)mred) << 16) | (clamp((int)mgreen) << 8) | clamp((int)mblue);  
  139.                 }  
  140.                   
  141.                 // cleanup for next loop  
  142.                 newRow = newCol = 0;  
  143.                 xred = xgreen = xblue = 0;  
  144.                 yred = ygreen = yblue = 0;  
  145.                   
  146.             }  
  147.         }  
  148.         setRGB(dest, 00, width, height, outPixels );  
  149.         return dest;  
  150.     }  
  151.       
  152.     public static int clamp(int value) {  
  153.         return value < 0 ? 0 : (value > 255 ? 255 : value);  
  154.     }  
  155.   
  156. }  
相关文章
|
编解码 算法 计算机视觉
小波分析法图像处理----图像增强
小波分析法图像处理----图像增强
721 0
小波分析法图像处理----图像增强
|
资源调度 计算机视觉 Java
图像处理------图像加噪
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">图像噪声源于现实世界中数字信号总会受到各种各样的干扰,最终接受的图像和源于的数字信号之间总</p> <p style="color: rgb(51, 51, 51); font-family: Arial; fon
1633 0
|
Java 计算机视觉 编解码
图像处理------高斯金字塔
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">一:图像金字塔基本操作</span></p> <p style="color: rgb(51, 51, 51); font-family: Arial;
2150 0
|
Java 计算机视觉 算法
图像处理------移动模糊
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">卷积模糊或者卷积平滑滤波,可以消除图像噪声,也可以产生一些常见的图像模糊特效,但</p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-siz
1234 0
|
计算机视觉 Java
图像处理------直方图均衡化
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;"><strong>基本思想</strong>:</span></p> <p style="color: rgb(51, 51, 51); font-fam
1469 0
|
计算机视觉 Java
图像处理------光束效果
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>原理:</strong></p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height
1411 0
|
计算机视觉 Java 数据处理
图像处理------理解卷积
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>一:什么是卷积</strong></p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-he
2384 0
|
计算机视觉 Java
图像处理------边缘褪化效果
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">很多图像处理软件都提供边缘褪化效果滤镜,其实原理非常的简单,网上搜索了一把,</p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size:
1026 0
|
算法 Java 计算机视觉
图像处理------基于像素的皮肤检测技术
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">基于像素的皮肤检测技术</p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">
2143 0
|
算法 Java 计算机视觉
图像处理------简单脸谱检测算法
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>介绍基于皮肤检测之后的,寻找最大连通区域,完成脸谱检测的算法。大致的算法步骤如下:</strong></p> <p style="color: rgb(51, 51, 51); font-famil
1614 0