简体   繁体   English

Renderscript-获取邻居像素

[英]Renderscript - getting neighbor pixel

I'm starting to explore power of renderscript. 我开始探索renderscript的功能。

Trying with 2D image data, I can convert pixel to some other pixel. 尝试使用2D图像数据,我可以将像素转换为其他像素。 However, how is it possible to get neighbor pixels from input allocation? 但是,如何从输入分配中获取相邻像素呢?

I thing how is for example built-in convolve3x3 filter done, when it needs neighbor pixels to operate, and it nicely clamps pixels at edge of image. 我有什么事情要做的,例如内置的convolve3x3滤镜需要邻居像素进行操作,并且很好地将像素固定在图像边缘时。

Assuming I have function 假设我有功能

void root(const uchar4 *v_in, uchar4 *v_out) {
   float4 f4 = rsUnpackColor8888(*v_in);
   // do something on pixel
   uchar4 u4 = rsPackColorTo8888(f4);
   *v_out = u4;
}

am I really supposed to index v_in like v_in[1] or v_in[k] to get other pixels, or is there some clever rs* function to get adjacent horizontal/vertical pixels, while providing proper clamp to image size, so that I don't index v_in array out of its size? 我是否真的应该像v_in [1]或v_in [k]那样索引v_in以获取其他像素,或者是否有一些巧妙的rs *函数来获取相邻的水平/垂直像素,同时提供了对图像尺寸的适当钳位,所以我不索引v_in数组超出其大小吗?

If you want to look at neighboring pixels (and you are using rs_allocations), you should just use a single global rs_allocation rather than passing it as *v_in. 如果要查看相邻像素(并且您正在使用rs_allocations),则应仅使用单个全局rs_allocation,而不要将其作为* v_in传递。 This would look like: 看起来像:

rs_allocation in;

// Using the new kernel syntax where v_out becomes the return value.
uchar4 __attribute__((kernel)) doSomething(uint32_t x, uint32_t y) {
  uchar4 u4 = rsGetElementAt_uchar4(in, x, y);  // You can adjust x,y here to get neighbor values too.
  float4 f4 = rsUnpackColor8888(u4);
  ...
  return rsPackColorTo8888(f4);
}

Unfortunately, there is no nice way to get automatic clamping with a regular rs_allocation, but you can adjust your code to do the edge clamp manually. 不幸的是,没有一种好的方法可以通过常规的rs_allocation获得自动夹紧,但是您可以调整代码以手动进行边缘夹紧。 Keep maxX, maxY as global variables passed to the Script, and then dynamically check whether or not you are in range before any rsGetElementAt*(). 将maxX,maxY保留为传递给脚本的全局变量,然后动态检查是否在任何rsGetElementAt *()之前。 If you do want automatic clamping/wrapping behaviors, you can also check out the rs_sampler and rsSample() APIs. 如果确实需要自动钳位/包装行为,则还可以签出rs_sampler和rsSample()API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM