imagefilter - RenderScript Sobel lmplementation, different in- and output types -
i want implement sobel filter in renderscript uchar4 input allocation , float[] output allocation. not quite sure whether possible use different types input , output allocations in renderscript. want develop solution myself, grateful advice on best renderscript structure takle problem. somewhere read, possible use
float attribute((kernel)) root(uchar4 *v_in, uint32_t x, uint32_t y) { }
would recommend such approach or can done without using kernel, i.e. function? in advance.
my rs code sobel (x direction) looks follows:
#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; }
what struggling passing parameters java side:
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) ;
i understand that, in order use rsgetelementat function (to access neighboring data within kernel) need set input allocation script global (rs_allocation gin in rs code). however, i'm not sure how handle "double allocation" java side. outallocation statement in java code not correct. specifiyally not sure, whether kernel returned float[] or float[][].
it possible use different types input , output. in case, suggest:
float __attribute__((kernel)) sobel(unchar4 *v_in, uint32_t x, uint32_t y) {}
you want use kernel, performance can benefit execution multiple threads.
also, have @ example of doing 3x3 convolution in rs.
update: generally, best in/out parameters use depend on type of output want filter generate - magnitude? uint
output suffice.
update2: if going use variable pass input allocation, don't need in kernel parameters, i.e.:
float __attribute__((kernel)) sobelx(uint32_t x, uint32_t y)
the rest of script looks ok (sans missing parenthesis in conditional). java part, below pasting demonstration of how should prepare output allocation , start script. kernel invoked every cell (i.e. every float) in output allocation.
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);
Comments
Post a Comment