简体   繁体   English

在位图的特定位置上使用Renderscript卷积

[英]Using Renderscript convolution on a specific location of bitmap

In matlab there is an option for doing 2D convolutions, that you can use it so you can have each area where convolution happened. 在matlab中,有一个进行2D卷积的选项,您可以使用它,以便可以对发生卷积的每个区域进行处理。 for example if you have done a 3x3 convolution you can say that save the result of each 3x3 window and use it. 例如,如果您进行了3x3卷积,则可以说保存每个3x3窗口的结果并使用它。 the command is like this: 命令是这样的:

X = conv2(A,B,'same');

the 'same' keyword is going to return the central part of the convolution of the same size as A. I want to know is there anyway were i can do something like this with renderscript in android? “ same”关键字将返回与A大小相同的卷积的中心部分。我想知道我是否可以在Android中使用renderscript做类似的事情? I will put a picture from matlab documentation so you can get a better understding of what i mean. 我会从matlab文档中放一张图片,以便您更好地理解我的意思。 again the picture is from matlab documentation wich is free. 同样,图片来自matlab文档,但完全免费。

Matlab二维卷积的相同关键字

You can use the Script.LaunchOptions class. 您可以使用Script.LaunchOptions类。

Using this class you can set limits of execution of kernels. 使用此类,您可以设置内核的执行限制。

Example : you want to run a kernel on a "rectangle" of your data (so, in 2 dimensions), starting at x -index (0-based, inclusive) 3 and ending at the x-index (exclusive) 8 and, on the y side the limits are 11 and 22 : 例如 :你想对你的数据的“矩形”(因此,在2个维),起点为x -index(0为主,含)运行内核3和X指数结束(独家) 8和,在y侧,限制为1122

Script.LaunchOptions launchOptions;
launchOptions = new Script.LaunchOptions();
launchOptions.setX(3, 8);
launchOptions.setY(11, 22);

// Kernel run
myScript.forEach_myKernelName(inAlloc, outAlloc, launchOptions);

Example : you want to apply a kernel over an image, with a 3-pixel-wide border (example directly taken from the FASTExample sample project): 示例 :您想要在具有3像素宽边框的图像上应用内核(示例直接取自FASTExample示例项目):

Script.LaunchOptions fastLaunchOptions;
fastLaunchOptions = new Script.LaunchOptions();
fastLaunchOptions.setX(3, inputImageSize.width - 3);
fastLaunchOptions.setY(3, inputImageSize.height - 3);

// ...

scriptCFast.forEach_fastOptimized(
      grayAllocation, fastKpAllocation, fastLaunchOptions);

Example : you want to apply a ScriptIntrinsicConvolve3x3 kernel over a restricted range: 示例 :您想要在受限范围内应用ScriptIntrinsicConvolve3x3内核:

// Define the convolution
ScriptIntrinsicConvolve3x3 convolve3x3 =
    ScriptIntrinsicConvolve3x3.create(mRS, Element.RGBA_8888(mRS));

// Some coefficients
float[] coefficients = {
        0.7f, 0, 0.5f,
        0, 1.0f, 0,
        0.5f, 0, 1.0f
};
convolve3x3.setCoefficients(coefficients);

// Execute the allocation with limits
Script.LaunchOptions launchOptions;
launchOptions = new Script.LaunchOptions();
launchOptions.setX(3, 8);
launchOptions.setY(11, 22);

convolve3x3.setInput(inputAllocation);
convolve3x3.forEach(convolvedAllocation, launchOptions);

Note : this process just executes a kernel over a certain range, but it is NOT going to create a new, smaller allocation. 注意 :此过程仅在一定范围内执行内核,但不会创建新的较小的分配。 So, after having executed a kernel over certain limits, you should copy the result of it using a copy kernel, like the following one: 因此,在超出一定限制后执行内核后,应使用复制内核来复制其结果,如下所示:

// Store the input allocation
rs_allocation inputAllocation;

// Offset indices, which define the start point for 
// the copy in the input allocation.
int inputOffsetX;
int inputOffsetY;

uchar4 __attribute__((kernel)) copyAllocation(int x, int y) {

    return rsGetElementAt_uchar4(
         inputAllocation, x + inputOffsetX, y + inputOffsetY);

}

Invoked with: 调用:

scriptC_main.set_inputAllocation(convolvedAllocation);
scriptC_main.set_inputOffsetX(offsetX);
scriptC_main.set_inputOffsetY(offsetY);
scriptC_main.forEach_copyAllocation(outputAllocation);

Edit : I specifically created an example for this case, where you can see the following process: 编辑 :我专门为这种情况创建了一个示例 ,您可以在其中看到以下过程:

  • Execution of a limited convolution, over a data set. 在数据集上执行有限的卷积。
  • Copying of the convolution output to a new Allocation. 将卷积输出复制到新的分配。

Reference: RenderScript: parallel computing on Android, the easy way 参考: RenderScript:Android上的并行计算的简单方法

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

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