java - 在 Android 上使用 RenderScript 的中值过滤器
问题描述
我正在使用 RenderScript 进行图像处理。现在我正在尝试用它为 Android 实现一个中值过滤器。基本上我尝试用它做的是:它获取位图格式的图像,为每个像素及其 8 个邻居(没有边缘情况)运行一个 3x3 过滤器,计算每个颜色通道的中值并相应地设置输出像素。
我已经尝试过它是否适用于这样的简单反转渲染脚本:
uchar4 RS_KERNEL invert(uchar4 in, uint32_t x, uint32_t y) {
uchar4 out = in;
out.r = 255 - in.r;
out.g = 255 - in.g;
out.b = 255 - in.b;
return out;
}
它工作正常。但是当我改变 RenderScript 来计算中位数时,我得到了一个黑色的图片输出。
这是渲染脚本代码:
#pragma version(1)
#pragma rs_fp_relaxed
#pragma rs java_package_name(com.google.android.GoogleCamera)
#include "rs_debug.rsh"
rs_allocation input;
uint32_t width;
uint32_t height;
static uchar medi(uchar colors[]) {
//sorting just to half
for(int i = 0; i < 5; i++){
int minIndex = i;
uchar minValue = colors[i];
//loop until end
for(int j = i+1; j < 9; j++){
if (colors[j] < minValue) {
minIndex = j;
minValue = colors[j];
//swap
colors[j] = colors[i];
colors[i] = minValue;
}
}
}
return colors[4];
}
uchar4 __attribute__((kernel)) median(uchar4 in, uint32_t x, uint32_t y) {
uchar4 out = in;
//edge
if (x == 0 || x == width || y == 0 || y == height)
return out;
//all surronding pixels (TopLeft .. BottomRight)
uchar4 tl = rsGetElementAt_uchar4(input, x-1, y+1);
uchar4 tm = rsGetElementAt_uchar4(input, x, y+1);
uchar4 tr = rsGetElementAt_uchar4(input, x+1, y+1);
uchar4 l = rsGetElementAt_uchar4(input, x-1, y);
uchar4 r = rsGetElementAt_uchar4(input, x+1, y);
uchar4 bl = rsGetElementAt_uchar4(input, x-1, y-1);
uchar4 bm = rsGetElementAt_uchar4(input, x, y-1);
uchar4 br = rsGetElementAt_uchar4(input, x+1, y-1);
//array for each color channel
uchar reds[] = {tl.r, tm.r, tr.r, l.r, r.r, bl.r, bm.r, br.r, in.r};
uchar greens[] = {tl.g, tm.g, tr.g, l.g, r.g, bl.g, bm.g, br.g, in.g};
uchar blues[] = {tl.b, tm.b, tr.b, l.b, r.b, bl.b, bm.b, br.b, in.b};
uchar alphas[] = {tl.a, tm.a, tr.a, l.a, r.a, bl.a, bm.a, br.a, in.a};
//get median values
out.r = medi(reds);
out.g = medi(greens);
out.b = medi(blues);
out.a = medi(alphas);
return out;
}
以防万一调用java部分:
private static Runnable runMedian(final Bitmap original, final File file) {
return new Runnable() {
@Override
public void run() {
//Create new bitmap
Bitmap bitmap = original.copy(original.getConfig(), true);
//Create renderscript
RenderScript rs = RenderScript.create(CameraActivity.staticContext);
//Create allocation from Bitmap
Allocation allocationA = Allocation.createFromBitmap(rs, bitmap);
//Create allocation with same type
Allocation allocationB = Allocation.createTyped(rs, allocationA.getType());
//Create script from rs file
ScriptC_median medianScript = new ScriptC_median(rs);
medianScript.set_input(allocationA);
medianScript.set_width(original.getWidth());
medianScript.set_height(original.getWidth());
medianScript.forEach_median(allocationA, allocationB);
//Copy script result into bitmap
allocationB.copyTo(bitmap);
//Destroy everything to free memory
allocationA.destroy();
allocationB.destroy();
medianScript.destroy();
rs.destroy();
//write resulting bitmap to file
writeFile(file, bitmap, "median");
}
};
}
我希望知道更多的人可以帮助我。最好的祝愿
解决方案
您可能需要修改对边缘的处理:索引 x 从 0 到 width-1,因此边缘位于 width-1(和 height-1)。
在 java 部分,除非宽度和高度相同,否则medianScript.set_height(original.getWidth());
可能应该阅读。original.getHeight()
推荐阅读
- swift - SwiftUI:在 ObservableObject 模型视图中使用函数初始化数据时出错
- clojure - 有没有一种方法可以重用 EDN 中已定义键的值?
- javascript - JSON 会发展到原生支持 Ecmascript 地图对象吗?
- regex - 提取数字并将其附加到不同的单元格中?
- python - Pycharm 覆盖控制台上的输出
- android - Android SDK 工具失败
- jquery - 如何在使用 jquery Ajax 处理数据时不需要上传文件
- amazon-web-services - Custom::LambdaTrigger 资源在使用 Cloud Formation 时卡在 CREATE_IN_PROGRESS
- database - 哪个视图获取有关所有数据字典视图的所有信息?
- rebol - 保存哈希的最佳方法是什么!阻止Rebol?