天天看点

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

算法思想:

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

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

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

程序效果:

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

关键代码解释:

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

<span style="font-weight: normal;">thresholdblur( kernel, inpixels, outpixels, width, height, true );  

thresholdblur( kernel, outpixels, inpixels, height, width, true );</span>  

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

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;  

    d = (int)math.sqrt(r1*r1-r2*r2);  

    d = r1-r2;  

    r += f * r2;  

    rf += f;  

    d = (int)math.sqrt(g1*g1-g2*g2);  

    d = g1-g2;  

    g += f * g2;  

    gf += f;  

    d = (int)math.sqrt(b1*b1-b2*b2);  

    d = b1-b2;  

    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;  

                            d = (int)math.sqrt(r1*r1-r2*r2);  

                            d = r1-r2;  

                            r += f * r2;  

                            rf += f;  

                            d = (int)math.sqrt(g1*g1-g2*g2);  

                            d = g1-g2;  

                            g += f * g2;  

                            gf += f;  

                            d = (int)math.sqrt(b1*b1-b2*b2);  

                            d = b1-b2;  

                            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;