使用Renderscript启动选项裁剪图像数据 [英] Crop image data using Renderscript Launch Options

查看:150
本文介绍了使用Renderscript启动选项裁剪图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an byte[] array of a greyvalue image dataIn with dimension width * height. Now, I want to crop the image by applying a translation (dx, dy) and cut-off the out-of-border regions, so that the dataOut has dimension (width-abs(dx))*(height-abs(dy)).

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

In RenderScript I would use a 2-d uchar-Allocation both for the input and the output. In order to efficiently apply the crop operation, it was thinking of using the LaunchOptions with (for example) setX(dx,width) and setY(0, height-dy) and apply a trivial kernel that just takes values from a subset of the original dimensions.

但是,当使用启动选项时,出库仍具有原始尺寸的宽度*高度,即裁剪的部分将仅显示为零-但是,我实际上是希望删除它们,即缩小维度的分配范围。

However, when using the Launch Options, the out-Allocation still has the original size width*height, i.e. the cropped parts will just be shown as zeros - but, I actually want them to be removed, i.e. out-Allocation be of reduced dimension.

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

Question: is there a solution in RS in order to perform this cropping job more elegantly? Thanks for your feedback.

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

UPDATE: I think, I found the solution. It is by defining the out-Allocation as a script global at the reduced dimensions from the outset, passing dx and dy as well as globals and then apply rsSetElementAt_uchar to set the values of the out-Allocation. Will give an udpate later.

推荐答案

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

So, here is my quick rs-crop tool, taking 5ms for cropping a 500k pixel image. It uses LaunchOptions and reduced dimensions for the cropped output. Should you need to crop a Bitmap, just use element type U8_4 and allocation uchar_4 instead of U8 and uchar, respectively.

crop.rs文件:

The crop.rs file:

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

这篇关于使用Renderscript启动选项裁剪图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆