HSV空间中的Renderscript直方图均衡,结果不尽如人意。

时间:2017-08-10 15:37:23

标签: android image-processing renderscript

我正在尝试使用本教程https://medium.com/@qhutch/android-simple-and-fast-image-processing-with-renderscript-2fa8316273e1但是在HSV空间而不是在YUV空间中操作。到目前为止这是我的代码。请注意,我正在使用HSV tripples的整数表示(我已经证实这可以更早地工作)。正如我所提到的,我在matlab中使用了同一图像上的histeq函数。 Android中直方图均衡产生的图像背景比matlab中的背景更暗。这是什么原因? (有问题的图像是http://www.eecs.qmul.ac.uk/~phao/CIP/Images/CamerMan.tif转换为png。

相关代码。

RegHistEq.rs

int32_t histogram[256];
float cummulativeSum[256]; 
int size;
uchar4 __attribute__((kernel)) root(uchar4 in) {
uchar4 out;
//Get HSV values.
 out.a = in.a;
 uchar rgbMin = min(min(in.r,in.g),in.b);
 uchar rgbMax = max(max(in.r,in.g),in.b);
 out.s2 = rgbMax;
 rsAtomicInc(&histogram[out.s2]);
 if (out.s2 == 0)
 {
    out.s0 = 0;
    out.s1= 0;
    return out;
 }

out.s1 = 255 * ((long)(rgbMax - rgbMin)) / out.s2;
if (out.s1 == 0)
{
  out.s0 = 0;
  return out;
 }

 if (rgbMax == in.r)
 {
    out.s0 = 0 + 43 * (in.g - in.b) / (rgbMax - rgbMin);
 }
 else if (rgbMax == in.g)
 {
  out.s0 = 85 + 43 * (in.b - in.r) / (rgbMax - rgbMin);
  }
  else
  {
    out.s0 = 171 + 43 * (in.r - in.g) / (rgbMax - rgbMin);
   }
    return out;

  }

uchar4 __attribute__((kernel)) remaptoRGB(uchar4 in)
{
uchar4 out;
uchar region, remainder, p, q, t;
uchar newV = 255*cummulativeSum[in.s2];
   if (in.s1 == 0)
    {
        out.r = newV;
        out.g = newV;
        out.b = newV;
        out.a = in.a;
        return out;
    }

    region = in.s0 / 43;
    remainder = (in.s0 - (region * 43)) * 6;

    p = (newV * (255 - in.s1)) >> 8;
    q = (newV * (255 - ((in.s1 * remainder) >> 8))) >> 8;
    t = (newV * (255 - ((in.s1 * (255 - remainder)) >> 8))) >> 8;

    switch (region)
    {
        case 0:
            out.r = newV; out.g = t; out.b = p;
            break;
        case 1:
            out.r = q; out.g = newV; out.b = p;
            break;
        case 2:
            out.r = p; out.g = newV; out.b = t;
            break;
        case 3:
            out.r = p; out.g = q; out.b = newV;
            break;
        case 4:
            out.r = t; out.g = p; out.b = newV;
            break;
        default:
            out.r = newV; out.g = p; out.b = q;
            break;
    }
        out.a = in.a;
        return out;

  }
void init() {
//initialize the array with zeros
for (int i = 0; i < 256; i++) {
    histogram[i] = 0;
    cummulativeSum[i] = 0.0f;
}
}

 void createRemapArray() {
//create map for v
float sum = 0;
for (int i = 0; i < 256; i++) {
    sum += histogram[i];
    cummulativeSum[i] = sum / (size);
 }
}

java代码。

 Bitmap image =  originalImage;
 int width = image.getWidth();
 int height = image.getHeight();
 Bitmap res = image.copy(image.getConfig(), true);
  RenderScript rs = RenderScript.create(this);
    Allocation allocationA = Allocation.createFromBitmap(rs, res);
    Allocation allocationB = Allocation.createTyped(rs, allocationA.getType());
    ScriptC_RegHistEq histEqScript = new ScriptC_RegHistEq(rs);
     allocationA.copyTo(res);

0 个答案:

没有答案