简体   繁体   中英

Adding 2 Images with Android Renderscript

I am trying to increase exposure time of my camera by adding the values of 2 back to back capture. It took far too long to calculate with Java so I decided to attempt Renderscript. I seem to have all of the bugs worked out but the end result has some pretty weird effects 图像变蓝和变绿

Most of the chair and floor in this image are fine but areas get mostly bright green and blue discolouration.

Here is the Renderscript file:

#pragma version(1)
#pragma rs java_package_name(edu.marquette.mcw.smartme)

rs_allocation extra_alloc;

uchar4 __attribute__((kernel)) combine(uchar4 a, uint32_t x, uint32_t y)
{
    // get second image
    uchar4 b = rsGetElementAt_uchar4(extra_alloc, x, y);

    // combine red value
    uint32_t red = a.r + b.r;
    //set to max value if it exceeds it
    if(255 - a.r < b.r)
    {
        red = 255;
    }
    a.r = red;

    // combine green value
    uint32_t green = a.g + b.g;
    if(255 - a.g < b.g)
    {
        green = 255;
    }
    a.g = green;

    // combine blue value
    uint32_t blue = a.b + b.b;
    if(255 - a.b < b.b)
    {
        blue = 255;
    }
    a.b = blue;

    // return result
    return a;
}

And here is the Java code that executes the Renderscript:

// Load Script
RenderScript RS = RenderScript.create(callingFrag.getActivity());
ScriptC_combine script = new ScriptC_combine(RS);

output = Bitmap.createBitmap(combined.getWidth(), combined.getHeight(), combined.getConfig());

// Send in bitmaps
Allocation aAllocation = Allocation.createFromBitmap(RS, combined);
Allocation bAllocation = Allocation.createFromBitmap(RS, exposures.remove(0));
Allocation outputAllocation = Allocation.createFromBitmap(RS, output);
script.set_extra_alloc(bAllocation);

// Run Script
script.forEach_combine(aAllocation, outputAllocation);
App.log("Combine Time: " + (System.currentTimeMillis() - start));
outputAllocation.copyTo(output);

I couldn't find anything in the logs that appeared to be a warning/error. Any ideas of how this can be solved?

I eventually found a library that covers exactly what I need and more in only about 1 line.

easyRS allows me to combined 2 images with just:

Blend.add(rs, inputBitmap, inputBitmap2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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