图像处理------基于阈值模糊

简介: <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

算法思想:

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

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

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

程序效果:


关键代码解释:

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

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

[java]  view plain copy
  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. }  
滤镜完整代码如下:

[java]  view plain copy
  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. }  

相关文章
|
算法 数据可视化
Halcon边缘检测和线条检测(1),文章含自适应/动态二值化等算子
Halcon边缘检测和线条检测(1),文章含自适应/动态二值化等算子
1130 0
|
10月前
|
文字识别 算法 计算机视觉
计算机视觉图像常用基本算法(阈值化、形态学变化、模糊)
计算机视觉图像常用基本算法(阈值化、形态学变化、模糊)
125 0
|
算法 Java 计算机视觉
基于 Laplacian 实现简单的图像模糊检测
基于 Laplacian 实现简单的图像模糊检测
610 0
基于 Laplacian 实现简单的图像模糊检测
|
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
1204 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: 1
1588 0
|
计算机视觉 Java
图像处理------基于Otsu阈值二值化
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-size: 18px;">一:基本原理</span></strong></p> <p style="color: rgb(51, 51, 51); font-fa
2337 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
1862 1
|
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); f
1228 0
|
计算机视觉
图像处理------调整亮度与饱和度
<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-fa
2579 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
2643 0