javaandroidc99medianrenderscript

Median filter with RenderScript on Android


I'm taking my first steps into image processing with RenderScript. Now I'm trying to implement a Median filter for Android with it. Basically what I try to do with it is: It gets an image in bitmap format, runs a 3x3 filter for every pixel with its 8 neighbours (without edge cases), calculates the median for each color channel and sets the output pixel accordingly.

I've tried if it works with a simple invert renderscript like this:

uchar4 RS_KERNEL invert(uchar4 in, uint32_t x, uint32_t y) {
  uchar4 out = in;
  out.r = 255 - in.r;
  out.g = 255 - in.g;
  out.b = 255 - in.b;
  return out;
}

and it works fine. But when I change the RenderScript to calculate the median I just got a black picture output.

Here's the RenderScript code:

#pragma version(1)
#pragma rs_fp_relaxed
#pragma rs java_package_name(com.google.android.GoogleCamera)

#include "rs_debug.rsh"

rs_allocation input;
uint32_t width;
uint32_t height;

static uchar medi(uchar colors[]) {
    //sorting just to half
    for(int i = 0; i < 5; i++){
        int minIndex = i;
        uchar minValue = colors[i];
        //loop until end
        for(int j = i+1; j < 9; j++){
            if (colors[j] < minValue) {
                minIndex = j;
                minValue = colors[j];
                //swap
                colors[j] = colors[i];
                colors[i] = minValue;
            }
        }
    }
    return colors[4];
}

uchar4 __attribute__((kernel)) median(uchar4 in, uint32_t x, uint32_t y) {
    uchar4 out = in;

    //edge
    if (x == 0 || x == width || y == 0 || y == height)
        return out;

    //all surronding pixels (TopLeft .. BottomRight)
    uchar4 tl = rsGetElementAt_uchar4(input, x-1, y+1);
    uchar4 tm = rsGetElementAt_uchar4(input, x, y+1);
    uchar4 tr = rsGetElementAt_uchar4(input, x+1, y+1);
    uchar4 l = rsGetElementAt_uchar4(input, x-1, y);
    uchar4 r = rsGetElementAt_uchar4(input, x+1, y);
    uchar4 bl = rsGetElementAt_uchar4(input, x-1, y-1);
    uchar4 bm = rsGetElementAt_uchar4(input, x, y-1);
    uchar4 br = rsGetElementAt_uchar4(input, x+1, y-1);

    //array for each color channel
    uchar reds[] = {tl.r, tm.r, tr.r, l.r, r.r, bl.r, bm.r, br.r, in.r};
    uchar greens[] = {tl.g, tm.g, tr.g, l.g, r.g, bl.g, bm.g, br.g, in.g};
    uchar blues[] = {tl.b, tm.b, tr.b, l.b, r.b, bl.b, bm.b, br.b, in.b};
    uchar alphas[] = {tl.a, tm.a, tr.a, l.a, r.a, bl.a, bm.a, br.a, in.a};

    //get median values
    out.r = medi(reds);
    out.g = medi(greens);
    out.b = medi(blues);
    out.a = medi(alphas);

    return out;
}

and just in case the invoking java part:

private static Runnable runMedian(final Bitmap original, final File file) {
        return new Runnable() {
            @Override
            public void run() {
                //Create new bitmap
                Bitmap bitmap = original.copy(original.getConfig(), true);

                //Create renderscript
                RenderScript rs = RenderScript.create(CameraActivity.staticContext);

                //Create allocation from Bitmap
                Allocation allocationA = Allocation.createFromBitmap(rs, bitmap);

                //Create allocation with same type
                Allocation allocationB = Allocation.createTyped(rs, allocationA.getType());

                //Create script from rs file
                ScriptC_median medianScript = new ScriptC_median(rs);

                medianScript.set_input(allocationA);
                medianScript.set_width(original.getWidth());
                medianScript.set_height(original.getWidth());
                medianScript.forEach_median(allocationA, allocationB);

                //Copy script result into bitmap
                allocationB.copyTo(bitmap);

                //Destroy everything to free memory
                allocationA.destroy();
                allocationB.destroy();
                medianScript.destroy();
                rs.destroy();

                //write resulting bitmap to file
                writeFile(file, bitmap, "median");
            }
        };
    }

I hope somebody who knows a little bit more can help me out. Best wishes


Solution

  • You may need to revise your handling of the edges: the index x runs from 0 to width-1, so the edge is at width-1 (and height-1).

    In the java part, medianScript.set_height(original.getWidth()); should probably read original.getHeight() unless width and height are the same.