Android的方框模糊算法 [英] Android Box Blur Algorithm

查看:154
本文介绍了Android的方框模糊算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力实现的android一个盒子模糊算法。 在code似乎是美好的,但试图将它的时候,一些地区在模糊的图像有大的黄色和白色的污迹遍布模糊的照片。 谁能帮我找出我做错了吗? 感谢:

下面是我:

 公共静态位图boxBlur(BMP位图,INT范围){
    断言(范围和放大器; 1)== 0:范围必须是奇数。

    位图模糊= Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),
            Config.ARGB_8888);
    帆布C =新的Canvas(模糊);

    INT W = bmp.getWidth();
    INT H = bmp.getHeight();

    INT []个像素=新INT [bmp.getWidth()* bmp.getHeight()];
    bmp.getPixels(像素,0,瓦特,0,0,W,H);

    boxBlurHorizo​​ntal(像素,W,H,范围/ 2);
    boxBlurVertical(像素,W,H,范围/ 2);

    c.drawBitmap(像素,0,W,0.0F,0.0F,W,H,真实,NULL);

    返回模糊;
}

私有静态无效boxBlurHorizo​​ntal(INT []像素,INT W,INT小时,
        INT halfRange){
    INT索引= 0;
    INT [] newColors =新INT [W]

    对于(INT Y = 0; Y< H; Y ++){
        INT命中= 0;
        长R = 0;
        长G = 0;
        长B = 0;
        对于(INT X = -halfRange,X<瓦; X ++){
            INT oldPixel = X  -  halfRange  -  1;
            如果(oldPixel> = 0){
                INT颜色=像素[指数+ oldPixel]。
                如果(颜色!= 0){
                    的R  -  = Color.red(颜色);
                    克 -  = Color.green(彩色);
                    b  -  = Color.blue(颜色);
                }
                hits--;
            }

            INT newPixel = X + halfRange;
            如果(newPixel< W){
                INT颜色=像素[指数+ newPixel]。
                如果(颜色!= 0){
                    R + = Color.red(彩色);
                    G + = Color.green(彩色);
                    B + = Color.blue(彩色);
                }
                点击++;
            }

            如果(X GT; = 0){
                newColors [X] = Color.argb(0xFF的,(字节)(R /次点击)
                        (字节)(克/次点击),(字节)(B /打));
            }
        }

        为(中间体X = 0 X  - 其中;瓦特; X ++){
            像素[指数+ X] = newColors [X]
        }

        指数+ = W;
    }
}

私有静态无效boxBlurVertical(INT []像素,INT W,INT小时,
        INT halfRange){

    INT [] newColors =新INT [H]
    INT oldPixelOffset =  - (halfRange + 1)*宽;
    INT newPixelOffset =(halfRange)*宽;

    为(中间体X = 0 X  - 其中;瓦特; X ++){
        INT命中= 0;
        长R = 0;
        长G = 0;
        长B = 0;
        INT指数= -halfRange * W + X;
        对于(INT Y = -halfRange; Y< H; Y ++){
            INT oldPixel = Y  -  halfRange  -  1;
            如果(oldPixel> = 0){
                INT颜色=像素[指数+ oldPixelOffset]。
                如果(颜色!= 0){
                    的R  -  = Color.red(颜色);
                    克 -  = Color.green(彩色);
                    b  -  = Color.blue(颜色);
                }
                hits--;
            }

            INT newPixel = Y + halfRange;
            如果(newPixel< H){
                INT颜色=像素[指数+ newPixelOffset]。
                如果(颜色!= 0){
                    R + = Color.red(彩色);
                    G + = Color.green(彩色);
                    B + = Color.blue(彩色);
                }
                点击++;
            }

            如果(γ> = 0){
                newColors [Y] = Color.argb(0xFF的,(字节)(R /次点击)
                        (字节)(克/次点击),(字节)(B /打));
            }

            指数+ = W;
        }

        对于(INT Y = 0; Y< H; Y ++){
            像素[Y * W + X] = newColors [Y]
        }
    }
}
 

解决方案

发现问题!
该行:
newColors [X] = Color.argb(0xFF的,(字节)(R /次点击),(字节)(克/打),(字节)(B /打));

我转换的平均值为字节,他们应该是整数。
 它更改为:

  newColors [X] = Color.argb(0xFF的,(INT)(R /次点击),(INT)(克/打),(INT)(B /打)) ;
 

I have been trying to implement a box blur algorithm in android. The code seems to be fine but when trying to apply it, some areas in the blurred image have big yellow and white smudges all over the blurred photo. Can anyone help me find out what i'm doing wrong? Thanks:

Here is what i have:

public static Bitmap boxBlur(Bitmap bmp, int range) {
    assert (range & 1) == 0 : "Range must be odd.";

    Bitmap blurred = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
            Config.ARGB_8888);
    Canvas c = new Canvas(blurred);

    int w = bmp.getWidth();
    int h = bmp.getHeight();

    int[] pixels = new int[bmp.getWidth() * bmp.getHeight()];
    bmp.getPixels(pixels, 0, w, 0, 0, w, h);

    boxBlurHorizontal(pixels, w, h, range / 2);
    boxBlurVertical(pixels, w, h, range / 2);

    c.drawBitmap(pixels, 0, w, 0.0F, 0.0F, w, h, true, null);

    return blurred;
}

private static void boxBlurHorizontal(int[] pixels, int w, int h,
        int halfRange) {
    int index = 0;
    int[] newColors = new int[w];

    for (int y = 0; y < h; y++) {
        int hits = 0;
        long r = 0;
        long g = 0;
        long b = 0;
        for (int x = -halfRange; x < w; x++) {
            int oldPixel = x - halfRange - 1;
            if (oldPixel >= 0) {
                int color = pixels[index + oldPixel];
                if (color != 0) {
                    r -= Color.red(color);
                    g -= Color.green(color);
                    b -= Color.blue(color);
                }
                hits--;
            }

            int newPixel = x + halfRange;
            if (newPixel < w) {
                int color = pixels[index + newPixel];
                if (color != 0) {
                    r += Color.red(color);
                    g += Color.green(color);
                    b += Color.blue(color);
                }
                hits++;
            }

            if (x >= 0) {
                newColors[x] = Color.argb(0xFF, (byte) (r / hits),
                        (byte) (g / hits), (byte) (b / hits));
            }
        }

        for (int x = 0; x < w; x++) {
            pixels[index + x] = newColors[x];
        }

        index += w;
    }
}

private static void boxBlurVertical(int[] pixels, int w, int h,
        int halfRange) {

    int[] newColors = new int[h];
    int oldPixelOffset = -(halfRange + 1) * w;
    int newPixelOffset = (halfRange) * w;

    for (int x = 0; x < w; x++) {
        int hits = 0;
        long r = 0;
        long g = 0;
        long b = 0;
        int index = -halfRange * w + x;
        for (int y = -halfRange; y < h; y++) {
            int oldPixel = y - halfRange - 1;
            if (oldPixel >= 0) {
                int color = pixels[index + oldPixelOffset];
                if (color != 0) {
                    r -= Color.red(color);
                    g -= Color.green(color);
                    b -= Color.blue(color);
                }
                hits--;
            }

            int newPixel = y + halfRange;
            if (newPixel < h) {
                int color = pixels[index + newPixelOffset];
                if (color != 0) {
                    r += Color.red(color);
                    g += Color.green(color);
                    b += Color.blue(color);
                }
                hits++;
            }

            if (y >= 0) {
                newColors[y] = Color.argb(0xFF, (byte) (r / hits),
                        (byte) (g / hits), (byte) (b / hits));
            }

            index += w;
        }

        for (int y = 0; y < h; y++) {
            pixels[y * w + x] = newColors[y];
        }
    }
}

解决方案

Found the problem!
The line:
newColors[x] = Color.argb(0xFF, (byte) (r / hits), (byte) (g / hits), (byte) (b / hits));

I converted the averages to bytes, where they should have been ints.
Changed it to:

newColors[x] = Color.argb(0xFF, (int) (r / hits), (int) (g / hits), (int) (b / hits));

这篇关于Android的方框模糊算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆