日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图像处理之基于阈值模糊

發布時間:2025/7/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图像处理之基于阈值模糊 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

圖像處理之基于閾值模糊

算法思想:

實現一個高斯卷積模糊但是只運用與周圍的像素值與中心像素值差值小于閾值。兩個

像素值之間的距離計算可以選用向量距離即曼哈頓距離或者歐幾里德距離。高斯模糊

采用先XY方向一維高斯模糊完成目的是為了減小計算量。

程序效果:


關鍵代碼解釋:

分別完成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; } }



轉載于:https://blog.51cto.com/gloomyfish/1400330

總結

以上是生活随笔為你收集整理的图像处理之基于阈值模糊的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。