我正在尝试在RenderScript中实现中值滤镜。但是代码的工作并不稳定。读取元素不是来自当前行rsGetElementAt_uchar4(inputAlloc,x,y + a)会导致错误。问题是什么 ?是否有使用RenderScript的此类过滤器的示例?
#pragma version(1)
#pragma rs java_package_name(a.myapplication)
#pragma rs_fp_relaxed
rs_allocation inputAlloc;
int bWidht, bHeight;
static uchar4 arrpix[9];
static uchar4 buff;
uchar4 __attribute__((kernel)) median(uchar4 in, uint32_t x, uint32_t y)
{
uchar4 arrpix[9];
uchar4 buff;
if((x<bWidht) && (y<bHeight)){
arrpix[0] = rsGetElementAt_uchar4(inputAlloc, x -1 , y - 1);
arrpix[1] = rsGetElementAt_uchar4(inputAlloc, x , y - 1);
arrpix[2] = rsGetElementAt_uchar4(inputAlloc, x +1 , y - 1);
arrpix[3] = rsGetElementAt_uchar4(inputAlloc, x -1 , y );
arrpix[4] = in;
arrpix[5] = rsGetElementAt_uchar4(inputAlloc, x +1 , y );
arrpix[6] = rsGetElementAt_uchar4(inputAlloc, x -1 , y + 1);
arrpix[7] = rsGetElementAt_uchar4(inputAlloc, x , y + 1);
arrpix[8] = rsGetElementAt_uchar4(inputAlloc, x +1 , y + 1);
for(int i=0; i<4; i++)
for(int i=0; i<=8; i++){
if(arrpix[i].r>arrpix[i+1].r){
buff.r = arrpix[i].r; arrpix[i].r = arrpix[i+1].r;
arrpix[i+1].r = buff.r;}
if(arrpix[i].g>arrpix[i+1].g){
buff.g = arrpix[i].g; arrpix[i].g = arrpix[i+1].g;
arrpix[i+1].g = buff.g;}
if(arrpix[i].b>arrpix[i+1].b){
buff.b = arrpix[i].b; arrpix[i].b = arrpix[i+1].b;
arrpix[i+1].b = buff.b;}
}
}
return arrpix[4];
}
答案 0 :(得分:1)
您需要检查x&gt; 0和y&gt; 0,因为0-1 = -1
底部循环看起来也不完全正确。你能修复间距吗?你的意思是在两个循环中使用i吗?