zoukankan      html  css  js  c++  java
  • 图像处理------基于阈值模糊 分类: 视频图像处理 2015-07-24 14:49 31人阅读 评论(0) 收藏

    算法思想:

    实现一个高斯卷积模糊但是只运用与周围的像素值与中心像素值差值小于阈值。两个

    像素值之间的距离计算可以选用向量距离即曼哈顿距离或者欧几里德距离。高斯模糊

    采用先XY方向一维高斯模糊完成目的是为了减小计算量。

    程序效果:


    关键代码解释:

    分别完成XY方向的一维高斯模糊

    1. <span style="font-weight: normal;">thresholdBlur( kernel, inPixels, outPixels, width, height, true );  
    2. thresholdBlur( kernel, outPixels, inPixels, height, width, true );</span>  
    计算像素距离,完成像素高斯卷积代码如下:

    1. int d;  
    2. if(euclid) {  
    3.     d = (int)Math.sqrt(a1*a1-a2*a2);  
    4. else {  
    5.     d = a1-a2;  
    6. }  
    7. if ( d >= -threshold && d <= threshold ) {  
    8.     a += f * a2;  
    9.     af += f;  
    10. }  
    11. if(euclid) {  
    12.     d = (int)Math.sqrt(r1*r1-r2*r2);  
    13. else {  
    14.     d = r1-r2;  
    15. }  
    16. if ( d >= -threshold && d <= threshold ) {  
    17.     r += f * r2;  
    18.     rf += f;  
    19. }  
    20. if(euclid) {  
    21.     d = (int)Math.sqrt(g1*g1-g2*g2);  
    22. else {  
    23.     d = g1-g2;  
    24. }  
    25. if ( d >= -threshold && d <= threshold ) {  
    26.     g += f * g2;  
    27.     gf += f;  
    28. }  
    29. if(euclid) {  
    30.     d = (int)Math.sqrt(b1*b1-b2*b2);  
    31. else {  
    32.     d = b1-b2;  
    33. }  
    34. if ( d >= -threshold && d <= threshold ) {  
    35.     b += f * b2;  
    36.     bf += f;  
    37. }  
    滤镜完整代码如下:

    1. package com.gloomyfish.filter.study;  
    2.   
    3. import java.awt.image.BufferedImage;  
    4.   
    5. public class SmartBlurFilter extends AbstractBufferedImageOp {  
    6.   
    7.     private int hRadius = 5;  
    8.     private int threshold = 50;  
    9.     private boolean euclid = false;  
    10.       
    11.     public BufferedImage filter( BufferedImage src, BufferedImage dest ) {  
    12.         int width = src.getWidth();  
    13.         int height = src.getHeight();  
    14.   
    15.         if ( dest == null )  
    16.             dest = createCompatibleDestImage( src, null );  
    17.   
    18.         int[] inPixels = new int[width*height];  
    19.         int[] outPixels = new int[width*height];  
    20.         getRGB( src, 00, width, height, inPixels );  
    21.   
    22.         // generate the Gaussian kernel data  
    23.         float[] kernel = makeKernel(hRadius);  
    24.           
    25.         // do Gaussian X and Y direction with kernel data.  
    26.         // this way will proceed quickly  
    27.         thresholdBlur( kernel, inPixels, outPixels, width, height, true );  
    28.         thresholdBlur( kernel, outPixels, inPixels, height, width, true );  
    29.   
    30.         // set back result data to destination image  
    31.         setRGB( dest, 00, width, height, inPixels );  
    32.         return dest;  
    33.     }  
    34.       
    35.     /** 
    36.      * Convolve with a Gaussian matrix consisting of one row float data 
    37.      */  
    38.     public void thresholdBlur(float[] matrix, int[] inPixels, int[] outPixels, int width, int height, boolean alpha) {  
    39.         int cols = matrix.length;  
    40.         int cols2 = cols/2;  
    41.   
    42.         for (int y = 0; y < height; y++) {  
    43.             int ioffset = y*width; // index to correct row here!!  
    44.             int outIndex = y;  
    45.             for (int x = 0; x < width; x++) {  
    46.                 float r = 0, g = 0, b = 0, a = 0;  
    47.                 int moffset = cols2;  
    48.   
    49.                 int rgb1 = inPixels[ioffset+x];  
    50.                 int a1 = (rgb1 >> 24) & 0xff;  
    51.                 int r1 = (rgb1 >> 16) & 0xff;  
    52.                 int g1 = (rgb1 >> 8) & 0xff;  
    53.                 int b1 = rgb1 & 0xff;  
    54.                 float af = 0, rf = 0, gf = 0, bf = 0;  
    55.                 for (int col = -cols2; col <= cols2; col++) {  
    56.                     float f = matrix[moffset+col];  
    57.   
    58.                     if (f != 0) {  
    59.                         int ix = x+col;  
    60.                         if (!(0 <= ix && ix < width))  
    61.                             ix = x;  
    62.                         int rgb2 = inPixels[ioffset+ix];  
    63.                         int a2 = (rgb2 >> 24) & 0xff;  
    64.                         int r2 = (rgb2 >> 16) & 0xff;  
    65.                         int g2 = (rgb2 >> 8) & 0xff;  
    66.                         int b2 = rgb2 & 0xff;  
    67.   
    68.                         int d;  
    69.                         if(euclid) {  
    70.                             d = (int)Math.sqrt(a1*a1-a2*a2);  
    71.                         } else {  
    72.                             d = a1-a2;  
    73.                         }  
    74.                         if ( d >= -threshold && d <= threshold ) {  
    75.                             a += f * a2;  
    76.                             af += f;  
    77.                         }  
    78.                         if(euclid) {  
    79.                             d = (int)Math.sqrt(r1*r1-r2*r2);  
    80.                         } else {  
    81.                             d = r1-r2;  
    82.                         }  
    83.                         if ( d >= -threshold && d <= threshold ) {  
    84.                             r += f * r2;  
    85.                             rf += f;  
    86.                         }  
    87.                         if(euclid) {  
    88.                             d = (int)Math.sqrt(g1*g1-g2*g2);  
    89.                         } else {  
    90.                             d = g1-g2;  
    91.                         }  
    92.                         if ( d >= -threshold && d <= threshold ) {  
    93.                             g += f * g2;  
    94.                             gf += f;  
    95.                         }  
    96.                         if(euclid) {  
    97.                             d = (int)Math.sqrt(b1*b1-b2*b2);  
    98.                         } else {  
    99.                             d = b1-b2;  
    100.                         }  
    101.                         if ( d >= -threshold && d <= threshold ) {  
    102.                             b += f * b2;  
    103.                             bf += f;  
    104.                         }  
    105.                     }  
    106.                 }  
    107.                 // normalization process here  
    108.                 a = af == 0 ? a1 : a/af;   
    109.                 r = rf == 0 ? r1 : r/rf;  
    110.                 g = gf == 0 ? g1 : g/gf;  
    111.                 b = bf == 0 ? b1 : b/bf;  
    112.                   
    113.                 // return result pixel data  
    114.                 int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;  
    115.                 int ir = PixelUtils.clamp((int)(r+0.5));  
    116.                 int ig = PixelUtils.clamp((int)(g+0.5));  
    117.                 int ib = PixelUtils.clamp((int)(b+0.5));  
    118.                 outPixels[outIndex] = (ia << 24) | (ir << 16) | (ig << 8) | ib;  
    119.                 outIndex += height;  
    120.             }  
    121.         }  
    122.     }  
    123.   
    124.     public void setHRadius(int hRadius) {  
    125.         this.hRadius = hRadius;  
    126.     }  
    127.       
    128.     public void setThreshold(int th) {  
    129.         this.threshold = th;  
    130.     }  
    131.       
    132.     public void setEuclid(boolean apply) {  
    133.         this.euclid = apply;  
    134.     }  
    135.   
    136. }  

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

  • 相关阅读:
    【死磕Java并发】—–J.U.C之AQS(一篇就够了)
    Java并发包基石-AQS详解
    java并发api总结
    用Java对CSV文件进行读写操作
    多线程批量检测未注册域名
    RSA公钥、私钥、签名和验签
    ASCII,Unicode和UTF-8终于找到一个能完全搞清楚的文章了
    Java 常用工具类---- 各种字符集编码判断与转换
    JavaMail| JavaMail配置属性
    QT下的几种透明效果(QPalette背景白色,窗口设置setWindowOpacity,QPainter使用Clear模式绘图)
  • 原文地址:https://www.cnblogs.com/mao0504/p/4705514.html
Copyright © 2011-2022 走看看