2016-08-25 92 views
0

在matlab中,有一個選項用於執行2D卷積,您可以使用它來讓每個卷積發生的區域。例如,如果您已經完成了3x3卷積,則可以說保存每個3x3窗口的結果並使用它。命令是這樣的:在位圖的特定位置上使用Renderscript卷積

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

的「相同」的關鍵字是否會返回一個爲A. 我想知道同樣大小的卷積的中央部分有反正是我可以做這樣的事情在Android的renderscript?我將從matlab文檔中提取圖片,以便更好地理解我的意思。再次圖片是從matlab文檔這是免費的。

The same keyword for a 2D convolution of matlab

+0

@ cmaster11可以請你幫助? – Shahryar

+0

@LarrySchiefer可以請你幫忙嗎?你是我在回答renderscript問題的stackoverflow中看到的少數幾個人之一。 – Shahryar

+0

感謝您的通知:P – cmaster11

回答

3

可以使用Script.LaunchOptions類。

使用此類可以設置內核的執行限制。

:你想對你的數據(因此,在2個維),起始於X -index(0爲主,含)3的「矩形」運行的內核,並在X結尾-index(不含)8和,在ÿ側的限制是1122

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

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

:要在一個圖像應用內核,用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); 

:你想通過限制範圍內應用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); 

注意:這個過程只是在一定範圍內執行內核,但它不會創建一個新的,更小的分配。因此,已經執行了某些限制的內核後,你應該使用複製的內核,就像下面的一個複製它的結果:

// 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); 

} 

調用時:

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

編輯:我專門爲此情況創建了example,您可以在其中看到以下過程:

  • 在數據集上執行有限卷積。
  • 將卷積輸出複製到新的分配。

參考:RenderScript: parallel computing on Android, the easy way

+0

爲什麼Y邊界是11和22?你能給我一個可視化的答案嗎? – Shahryar

+0

重要的是我想使用已經爲renderscript編寫的卷積3x3。我可以在這個launchoptions中使用ScriptIntrinsic嗎? – Shahryar

+0

我假設他們以相同的方式工作;) – cmaster11