2014-10-08 58 views
0

我想要执行需要相邻像素的图像处理操作,但我不确定如何从分配中访问它们。我见过的大多数内核都是在一个像素上进行操作,然后进行更新,然后将其返回。有没有办法可以在下面的方法中访问(x,y)的邻居。如何获取Renderscript中位图分配的相邻像素?

uchar4 __attribute__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) { 

    uchar4 neighbor = allocation[x+1][y]; // How do I do this in renderscript? 
    uchar4 otherNeighbor = allocation[x-1][y]; 
    ... 
} 

回答

5

标准输入/输出分配,但你可以通过只创建一个全局变量得到他们类型的rs_allocation。

rs_allocation input; 
uchar4 __attribute__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) { 

uchar4 neighbor = rsGetElementAt_uchar4(input, x+1, y); 
uchar4 otherNeighbor = rsGetElementAt_uchar4(input, x-1, y); 
... 
} 

在Java中,你调用foreach所内核之前,你只需要做:

myScript.set_input(myInputAllocation); 
myScript.forEach_invert(myInputAllocation, myOutputAllocation); 
+1

非常感谢!我能够这样访问邻居。 – WindsurferOak 2014-10-08 23:28:15

+0

有一件事我没有在我的天真代码中做的是边界检查我的访问。如果直接运行该代码,您将尝试访问无效内存(对于任何有效的输入分配)。您应该根据内核的尺寸来限制(x-1)和(x + 1)部分。 – 2014-10-09 00:03:45

+0

感谢您的提示!我已经设置了两个全局变量,maxWidth和maxHeight,并将检查这些变量。 – WindsurferOak 2014-10-09 00:06:38

0

你可以做到这一点:是那些获得隐含连接更难获得的相邻像素

... 
uchar4 neighbor = rsGetElementAt(in, x + 1, y); 
uchar4 otherNeighbor = rsGetElementAt(in, x - 1, y); 
... 
+0

你不能因为不是rs_allocation。隐式连接的标准输入/输出分配难以访问相邻像素,但您可以通过创建一个类型为rs_allocation的全局变量来获得它们。我将在下面提供更完整的答案。 – 2014-10-08 22:10:14

+0

该死的......我很害怕。谢谢澄清,斯蒂芬。 – 2014-10-09 04:50:57