图像处理之基于阈值模糊

简介: 图像处理之基于阈值模糊

图像处理之基于阈值模糊

算法思想:

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

像素值之间的距离计算可以选用向量距离即曼哈顿距离或者欧几里德距离。高斯模糊采用先XY方向一维高斯模糊完成目的是为了减小计算量。

程序效果: image.png


关键代码解释:

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

thresholdBlur( kernel, inPixels, outPixels, width, height, true );
thresholdBlur( kernel, outPixels, inPixels, height, width, true );

计算像素距离,完成像素高斯卷积代码如下:

int d;
if(euclid) {
  d = (int)Math.sqrt(a1*a1-a2*a2);
} else {
  d = a1-a2;
}
if ( d >= -threshold && d <= threshold ) {
    a += f * a2;
    af += f;
}
if(euclid) {
  d = (int)Math.sqrt(r1*r1-r2*r2);
} else {
  d = r1-r2;
}
if ( d >= -threshold && d <= threshold ) {
    r += f * r2;
    rf += f;
}
if(euclid) {
  d = (int)Math.sqrt(g1*g1-g2*g2);
} else {
  d = g1-g2;
}
if ( d >= -threshold && d <= threshold ) {
    g += f * g2;
    gf += f;
}
if(euclid) {
  d = (int)Math.sqrt(b1*b1-b2*b2);
} else {
  d = b1-b2;
}
if ( d >= -threshold && d <= threshold ) {
    b += f * b2;
    bf += f;
}

滤镜完整代码如下:

package com.gloomyfish.filter.study;
 
import java.awt.image.BufferedImage;
 
public class SmartBlurFilter extends AbstractBufferedImageOp {
 
  private int hRadius = 5;
  private int threshold = 50;
  private boolean euclid = false;
  
    public BufferedImage filter( BufferedImage src, BufferedImage dest ) {
        int width = src.getWidth();
        int height = src.getHeight();
 
        if ( dest == null )
            dest = createCompatibleDestImage( src, null );
 
        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        getRGB( src, 0, 0, width, height, inPixels );
 
        // generate the Gaussian kernel data
        float[] kernel = makeKernel(hRadius);
        
        // do Gaussian X and Y direction with kernel data.
        // this way will proceed quickly
    thresholdBlur( kernel, inPixels, outPixels, width, height, true );
    thresholdBlur( kernel, outPixels, inPixels, height, width, true );
 
    // set back result data to destination image
        setRGB( dest, 0, 0, width, height, inPixels );
        return dest;
    }
    
  /**
   * Convolve with a Gaussian matrix consisting of one row float data
   */
  public void thresholdBlur(float[] matrix, int[] inPixels, int[] outPixels, int width, int height, boolean alpha) {
    int cols = matrix.length;
    int cols2 = cols/2;
 
    for (int y = 0; y < height; y++) {
      int ioffset = y*width; // index to correct row here!!
            int outIndex = y;
      for (int x = 0; x < width; x++) {
        float r = 0, g = 0, b = 0, a = 0;
        int moffset = cols2;
 
                int rgb1 = inPixels[ioffset+x];
                int a1 = (rgb1 >> 24) & 0xff;
                int r1 = (rgb1 >> 16) & 0xff;
                int g1 = (rgb1 >> 8) & 0xff;
                int b1 = rgb1 & 0xff;
        float af = 0, rf = 0, gf = 0, bf = 0;
                for (int col = -cols2; col <= cols2; col++) {
          float f = matrix[moffset+col];
 
          if (f != 0) {
            int ix = x+col;
            if (!(0 <= ix && ix < width))
              ix = x;
            int rgb2 = inPixels[ioffset+ix];
                        int a2 = (rgb2 >> 24) & 0xff;
                        int r2 = (rgb2 >> 16) & 0xff;
                        int g2 = (rgb2 >> 8) & 0xff;
                        int b2 = rgb2 & 0xff;
 
            int d;
            if(euclid) {
              d = (int)Math.sqrt(a1*a1-a2*a2);
            } else {
              d = a1-a2;
            }
                        if ( d >= -threshold && d <= threshold ) {
                            a += f * a2;
                            af += f;
                        }
                        if(euclid) {
              d = (int)Math.sqrt(r1*r1-r2*r2);
            } else {
              d = r1-r2;
            }
                        if ( d >= -threshold && d <= threshold ) {
                            r += f * r2;
                            rf += f;
                        }
                        if(euclid) {
              d = (int)Math.sqrt(g1*g1-g2*g2);
            } else {
              d = g1-g2;
            }
                        if ( d >= -threshold && d <= threshold ) {
                            g += f * g2;
                            gf += f;
                        }
                        if(euclid) {
              d = (int)Math.sqrt(b1*b1-b2*b2);
            } else {
              d = b1-b2;
            }
                        if ( d >= -threshold && d <= threshold ) {
                            b += f * b2;
                            bf += f;
                        }
          }
        }
                // normalization process here
                a = af == 0 ? a1 : a/af; 
                r = rf == 0 ? r1 : r/rf;
                g = gf == 0 ? g1 : g/gf;
                b = bf == 0 ? b1 : b/bf;
                
                // return result pixel data
        int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
        int ir = PixelUtils.clamp((int)(r+0.5));
        int ig = PixelUtils.clamp((int)(g+0.5));
        int ib = PixelUtils.clamp((int)(b+0.5));
        outPixels[outIndex] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
                outIndex += height;
      }
    }
  }
 
  public void setHRadius(int hRadius) {
    this.hRadius = hRadius;
  }
  
  public void setThreshold(int th) {
    this.threshold = th;
  }
  
    public void setEuclid(boolean apply) {
      this.euclid = apply;
    }
 
}
相关文章
|
存储 C++
【C++】——基础编程
【C++】——基础编程
|
SQL 安全 Java
SpringData JPA(2)
SpringData JPA(2)
185 1
|
传感器 监控 数据中心
|
6月前
|
数据可视化 网络安全 数据库
YashanDB安装初始环境调整
本文档来源于YashanDB官网,主要介绍YashanDB安装前的环境准备与配置。内容涵盖端口开放规则(包括默认端口号和分配原则)、防火墙设置(关闭或添加白名单)、SSH服务开启以及共享集群部署时的环境清理等关键步骤。确保按照指引正确配置端口和环境,可有效避免安装过程中因网络或环境问题导致的失败。适用于单机、分布式及共享集群等多种部署形态。
|
网络协议 网络架构
ensp中默认路由和静态路由实验
默认路由的作用是将无法匹配路由表中其他路由表项的数据包转发到指定下一跳路由器。在实际网络中,默认路由通常用于简化路由配置,通常在网络边缘的路由器上配置 静态路由的作用是将特定网络的数据包转发到指定下一跳路由器。在实际网络中,静态路由通常用于更精细地控制数据包的转发,通常在网络核心路由器上配置。
622 6
ensp中默认路由和静态路由实验
|
机器学习/深度学习 算法 算法框架/工具
深度学习在图像识别中的应用及代码示例
【9月更文挑战第32天】本文将深入探讨深度学习在图像识别领域的应用,包括其原理、技术、优势以及挑战。我们将通过一个简单的代码示例,展示如何使用深度学习技术进行图像识别。无论你是初学者还是有经验的开发者,都可以从中获得启发和帮助。让我们一起探索这个充满无限可能的领域吧!
211 8
|
JavaScript 前端开发
【vue】 vue2 canvas实现在图片上选点,画区域并将 坐标传递给后端
【vue】 vue2 canvas实现在图片上选点,画区域并将 坐标传递给后端
699 0
|
存储 安全 程序员
|
安全 Linux 网络安全
CenOS6.5安全加固及性能优化
CenOS6.5安全加固及性能优化
CenOS6.5安全加固及性能优化
|
监控 Devops jenkins
流行的软件质量保证体系工具
流行的软件质量保证体系工具
311 0