繁体   English   中英

使用Renderscript启动选项裁剪图像数据

[英]Crop image data using Renderscript Launch Options

我有一个灰度值图像dataIn的byte []数组,其尺寸为width * height。 现在,我想通过应用平移(dx,dy)裁剪图像并切除无边界区域,以便dataOut具有维度(width-abs(dx))*(height-abs(dy ))。

在RenderScript中,我将对输入和输出使用2-d uchar-Allocation。 为了有效地应用裁剪操作,正在考虑将LaunchOptions与(例如)setX(dx,width)和setY(0,height-dy)结合使用,并应用一个琐碎的内核,该内核仅从原始尺寸。

但是,当使用“启动选项”时,出库仍具有原始大小的宽度*高度,即裁剪的部分将仅显示为零-但是,我实际上希望将其删除,即出库的尺寸减小了。

问题:RS中是否有解决方案,以便更优雅地执行此裁剪工作? 感谢您的反馈意见。

更新:我想,我找到了解决方案。 它是通过从一开始就以缩小的尺寸将out-Allocation定义为全局脚本,传递dx和dy以及globals,然后应用rsSetElementAt_uchar来设置out-Allocation的值。 待会儿再给。

因此,这是我快速的rs-crop工具,需要5ms的时间来裁剪500k像素的图像。 对于裁切后的输出,它使用LaunchOptions并减小尺寸。 如果需要裁剪位图,只需分别使用元素类型U8_4和分配uchar_4而不是分别使用U8和uchar。

crop.rs文件:

#pragma version(1)
#pragma rs java_package_name(com.xxx.yyy)
#pragma rs_fp_relaxed

int32_t width;
int32_t height;

rs_allocation croppedImg;
uint xStart, yStart;

void __attribute__((kernel)) doCrop(uchar in,uint32_t x, uint32_t y) {
    rsSetElementAt_uchar(croppedImg,in, x-xStart, y-yStart);
}

Java部分:

// data1 is the byte[] array with (grayvalue) data of size 
// width*height you want to crop.

// define crop shift (dx, dy) here
int dx=0;       // (-width < dx < width);
int dy=250;     // (- height < dy < height);

int xStart=0, xEnd=0;
int yStart=0, yEnd=0;

// x direction
if (dx<0) {
    xStart= Math.abs(dx);
    xEnd=width;
}
else {
    xStart = 0;
    xEnd = width - Math.abs(dx);
}

// same for y direction
if (dy<0) {
    yStart= Math.abs(dy);
    yEnd=height;
}
else {
    yStart = 0;
    yEnd = height - Math.abs(dy);
}

// initiate rs and crop script
RenderScript rs = RenderScript.create(this);
ScriptC_crop mcropScr=new ScriptC_crop (rs);

// define allocations. Note the reduced size of cropAlloc 
Type.Builder typeUCHAR = new Type.Builder(rs, Element.U8(rs));  
typeUCHAR.setX(width).setY(height);

inAlloc = Allocation.createTyped(rs, typeUCHAR.create());
inAlloc.copyFrom(data1);

Type.Builder TypeUCHARCropped = new Type.Builder(rs, Element.U8(rs));
TypeUCHARCropped.setX(xEnd-xStart).setY(yEnd-yStart);

Allocation cropAlloc = Allocation.createTyped(rs,  TypeUCHARCropped.create());
mcropScr.set_croppedImg(cropAlloc);
mcropScr.set_xStart(xStart);
mcropScr.set_yStart(yStart);

Script.LaunchOptions lo = new Script.LaunchOptions();
lo.setX(xStart, xEnd);
lo.setY(yStart, yEnd);

mcropScr.forEach_doCrop(inAlloc, lo);


byte[] data1_cropped =new byte[(xEnd-xStart)*(yEnd-yStart)];
cropAlloc.copyTo(data1_cropped);

与其他答案的想法类似,但这与Google的Intrinsics API风格相符:

#pragma version(1)
#pragma rs java_package_name(com.sicariusnoctis.collaborativeintelligence)
#pragma rs_fp_relaxed

rs_allocation input;
uint32_t xStart, yStart;

uchar4 RS_KERNEL crop(uint32_t x, uint32_t y) {
    return rsGetElementAt_uchar4(input, x + xStart, y + yStart);
}

建立:

fun setup(
    width: Int, height: Int,
    new_width: Int, new_height: Int,
    xStart: Int, yStart: Int
) {
    val inputType = Type.createXY(rs, Element.RGBA_8888(rs), width, height)
    val outputType = Type.createXY(rs, Element.RGBA_8888(rs), new_width, new_height)

    inputAllocation = Allocation.createTyped(rs, inputType, Allocation.USAGE_SCRIPT)
    outputAllocation = Allocation.createTyped(rs, outputType, Allocation.USAGE_SCRIPT)

    crop = ScriptC_crop(rs)
    crop._xStart = xStart.toLong()
    crop._yStart = yStart.toLong()
    crop._input = inputAllocation
}

并执行:

fun execute(inputArray: ByteArray): ByteArray {
    inputAllocation.copyFrom(inputArray)
    crop.forEach_crop(outputAllocation)

    val outputArray = ByteArray(outputAllocation.bytesSize)
    outputAllocation.copyTo(outputArray)
    return outputArray
}

暂无
暂无

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

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