我想在RenderScript中用uchar4作为输入分配,float[]作为输出分配来实现Sobel滤波器。我不太确定是否可以在RenderScript中使用不同类型的输入和输出分配。我想自己开发解决方案,但希望能得到一些关于最佳Renderscript结构的建议,以解决这个问题。在我读到的某个地方,它是可以使用的
浮动attribute((kernel))根(uchar4 *v_in,uint32_t x,uint32_t y) {}
您会推荐这样的方法吗?或者,在不使用内核(也就是函数)的情况下,可以这样做吗?提前谢谢。
我为Sobel (X方向)编写的rs代码现在看起来如下:
#pragma version(1)
#pragma rs java_package_name(com.example.xxx)
#pragma rs_fp_relaxed
rs_allocation gIn;
int32_t width;
int32_t height;
float __attribute__((kernel)) sobelX(uchar4 *v_in, uint32_t x, uint32_t y) {
float out=0;
if (x>0 && y>0 && x<(width-1) && y<(height-1){
uchar4 c11=rsGetElementAt_uchar4(gIn, x-1, y-1);
uchar4 c21=rsGetElementAt_uchar4(gIn, x, y-1);
uchar4 c31=rsGetElementAt_uchar4(gIn, x+1, y-1);
uchar4 c13=rsGetElementAt_uchar4(gIn, x-1, y+1);
uchar4 c23=rsGetElementAt_uchar4(gIn, x, y+1);
uchar4 c33=rsGetElementAt_uchar4(gIn, x+1, y+1);
float4 f11=convert_float4(c11);
float4 f21=convert_float4(c21);
float4 f31=convert_float4(c31);
float4 f13=convert_float4(c13);
float4 f23=convert_float4(c23);
float4 f33=convert_float4(c33);
out= f11.r-f13.r + 2*(f21.r-f23.r) + f31.r-f33.r;
}
return out;
}我正在挣扎的是从Java端传递参数:
float[][] gx = new float[width][height];
ScriptC_sobel script;
script=new ScriptC_sobel(rs);
script.set_width(width) ;
script.set_height(height) ;
script.set_gIn(bmpGray);
Allocation inAllocation = Allocation.createFromBitmap(rs, bmpGray, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
Allocation outAllocation = Allocation.createTyped(rs, float,2) ;
script.forEach_sobelX(inAllocation, outAllocation);
outAllocation.copyTo(gx) ;我理解,为了使用rsGetElementAt函数(访问内核中的相邻数据),我还需要将输入分配设置为脚本全局( rs代码中的rs_allocation gIn )。但是,我不知道如何从Java端处理这种“双分配”。而且,Java代码中的outAllocation语句可能是不正确的。具体而言,我不确定内核是否会以float[]或float的形式返回。
发布于 2015-08-07 21:02:50
可以使用不同类型的输入和输出。就你而言,我实际上建议:
float __attribute__((kernel)) sobel(unchar4 *v_in, uint32_t x, uint32_t y) {}当然,您希望使用内核,这样就可以从多个线程的执行中获益。
另外,请看一下这个在RS中进行3x3卷积的实例。
更新:通常情况下,要使用的最佳输入/输出参数取决于希望此过滤器生成的输出类型--只是大小吗?那么uint输出很可能就足够了。
UPDATE2:如果要使用变量传递输入分配,则在内核参数中不需要它,即:
float __attribute__((kernel)) sobelX(uint32_t x, uint32_t y)脚本的其余部分看起来没问题(条件中缺少括号)。至于Java部分,下面我将演示如何准备输出分配并启动脚本。然后,对输出分配中的每个单元(即每个浮点)调用内核。
float[] gx = new float[width * height];
Type.Builder TypeIn = new Type.Builder(mRS, Element.F32(mRS));
TypeIn.setX(width).setY(height);
Allocation outAllocation = Allocation.createTyped(mRS, TypeIn.create());
mScript.forEach_sobelX(outAllocation);https://stackoverflow.com/questions/31884334
复制相似问题