从int获取Bytes以避免变换乐趣 - Java(中值过滤)

gav*_*gav 4 java algorithm image-processing

我正在尝试在Java中对图像执行中值过滤器,但速度非常慢.首先,如果你们有任何人知道我可以使用它的独立实现,那么如果你能让我知道的话会很棒.我正在Android上实现,试图复制JAI的一小部分.

在我的方法中,我采用每个像素,使用提取R,G和B值

r = pixel >> 16 & 0xFF
Run Code Online (Sandbox Code Playgroud)

或类似的,找到内核的中位数并完成

pixel = a | r <<16 | g << 8 | b 
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以从int中获取字节,这样会更快?

亲切的问候,

加文


编辑:完整的代码,以帮助诊断我的低性能请求

对于实际的源文件,请转到这里我可以找到medianFilter的实现.

widthheight变量用于dest的大小,可用作类成员变量.像素被线性化为一维阵列.

private void medianFilterSquare(int[] source, int[] dest, int rWidth,
        int rHeight, int radius) {
    // Source has been reflected into a border of size radius
    // This makes it radius * 2 pixels wider and taller than the dest
    int r,g,b;
    int destOffset, rOffset, kOffset;

    // The first offset into the source to calculate a median for
    // This corresponds to the first pixel in dest
    int rFirst = radius + (rWidth*radius);

    // We use a square kernel with the radius passed
    int neighbours = (radius+radius+1)*(radius+radius+1);

    int index;

    // Arrays to accumulate the values for median calculation
    int[] rs = new int[neighbours];
    int[] gs = new int[neighbours];
    int[] bs = new int[neighbours];

    // Declaring outside the loop helps speed? I'm sure this is done for me
    // by the compiler
    int pixel;

    // Iterate over the destination pixels
    for(int x = 0; x < height; x++){
        for(int y = 0; y < width; y++){
            // Offset into destination
            destOffset = x + (y * width);
            // Offset into source with border size radius
            rOffset = destOffset + rFirst + (y * (radius *2));

            index = 0;

            // Iterate over kernel
            for(int xk = -radius; xk < radius ; xk ++){
                for(int yk = -radius; yk < radius ; yk ++){
                    kOffset = rOffset + (xk + (rWidth*yk));
                    pixel = source[kOffset];
                    // Color.red is equivalent to (pixel>>16) & 0xFF
                    rs[index] = Color.red(pixel);
                    gs[index] = Color.green(pixel);
                    bs[index] = Color.blue(pixel);
                    index++;
                }   
            }
            r = medianFilter(rs);
            g = medianFilter(gs);
            b = medianFilter(bs);

            dest[destOffset] = Color.rgb(r, g, b);
        }           
    }       
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

正如其他人所说的那样,可能是导致问题的原因之一.我会说一件事(这可能是显而易见的,但无论如何) - 不要只是在桌面虚拟机上分析应用程序,并假设瓶颈将在同一个地方.在Dalvik找到完全不同的瓶颈,我不会感到惊讶.

您是否可以使用仍然移位的值?例如,如果你只是掩盖不同的颜色:

int r = pixel & 0xff0000;
int g = pixel & 0xff00;
int b = pixel & 0xff;
Run Code Online (Sandbox Code Playgroud)

你能相应地调整你的处理算法吗?

最后一个想法:我总是发现转移运营商的优先级令人困惑.我强烈建议从可读性的角度出发,将它们括起来:

r = (pixel >> 16) & 0xFF;
pixel = a | (r <<16) | (g << 8) | b;
Run Code Online (Sandbox Code Playgroud)

与性能无关,但如果我是维护者,我当然会欣赏它:)