gpt4 book ai didi

java - 从 int 中获取字节以避免移位乐趣 - Java(中值过滤)

转载 作者:搜寻专家 更新时间:2023-11-01 01:58:57 24 4
gpt4 key购买 nike

我正在尝试用 Java 对图像执行中值滤波器,但速度非常慢。首先,如果你们中有人知道我可以使用的独立实现,请告诉我,那就太好了。我在 Android 上实现,试图复制 JAI 的一小部分。

在我的方法中,我获取每个像素,使用

提取 R、G 和 B 值
r = pixel >> 16 & 0xFF

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

pixel = a | r <<16 | g << 8 | b 

有什么方法可以更快地从 int 中获取字节?

亲切的问候,

加文


编辑:根据要求帮助诊断我的低性能的完整代码

有关实际源文件,请转到 here这就是我的 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);
}
}
}

最佳答案

正如其他人所说,这可能是导致问题的原因。我要说的一件事(这可能是显而易见的,但无论如何)——不要只是在桌面 VM 上分析应用程序并假设瓶颈将在同一个地方。如果在 Dalvik 中发现完全不同的瓶颈,我一点也不会感到惊讶。

您是否可以使用仍然发生变化的值来工作?例如,如果您只是不同颜色的 mask :

int r = pixel & 0xff0000;
int g = pixel & 0xff00;
int b = pixel & 0xff;

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

最后一个想法:我总是觉得移位运算符的优先级令人困惑。我强烈建议您从可读性的角度将它们括起来:

r = (pixel >> 16) & 0xFF;
pixel = a | (r <<16) | (g << 8) | b;

与性能无关,但如果我是维护者,我当然会很感激 :)

关于java - 从 int 中获取字节以避免移位乐趣 - Java(中值过滤),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1024709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com