Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color Matrix for Image Sharpening in android

I have a class that uses a color matrix to add image effects on an ImageView. I have several color matrices for contrast,exposure,temperature and saturation. Ex:

public static Bitmap changeContrast(Bitmap bmp, float contrast)
{
    ColorMatrix cm = new ColorMatrix(new float[]
            {
                    contrast, 0, 0, 0, 0,
                    0, contrast, 0, 0, 0,
                    0, 0, contrast, 0, 0,
                    0, 0, 0, 1, 0
            });

    return getBitmapFromColorMatrix(cm, bmp);
}

Now my problem is, the sharpening of the image. There seems to be no color matrix for these. Please help.

I tried sharpening the image using the code from here but it takes too long to process and I do not know what valid values should be passed to the weight parameter of the sharpenImage(Bitmap src, double weight) method.

like image 859
MetaSnarf Avatar asked Dec 04 '25 17:12

MetaSnarf


1 Answers

It may be too late but i'd just wanted to share a fast way to sharpen an image.

 public static Bitmap doSharpen(Bitmap original, float[] radius) {  
      Bitmap bitmap = Bitmap.createBitmap(
              original.getWidth(), original.getHeight(),
              Bitmap.Config.ARGB_8888);

          RenderScript rs = RenderScript.create(yourContext);

          Allocation allocIn = Allocation.createFromBitmap(rs, original);
          Allocation allocOut = Allocation.createFromBitmap(rs, bitmap);

          ScriptIntrinsicConvolve3x3 convolution
              = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs));
          convolution.setInput(allocIn);
          convolution.setCoefficients(radius);
          convolution.forEach(allocOut);

          allocOut.copyTo(bitmap);       
          rs.destroy();   

          return bitmap;                   

}

And here are 3 different sharpen types that i created:

// low
private static void loadBitmapSharp() {
    float[] sharp = { -0.60f, -0.60f, -0.60f, -0.60f, 5.81f, -0.60f,
            -0.60f, -0.60f, -0.60f };
//you call the method above and just paste the bitmap you want to apply it and the float of above
    yourbitmap = doSharpen(getbitmap, sharp));
}

// medium
private static void loadBitmapSharp1() {
    float[] sharp = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f, -1.0f, 0.0f, -1.0f,
            0.0f

    }; 
//you call the method above and just paste the bitmap you want to apply it and the float of above
    yourbitmap = doSharpen(getbitmap, sharp));
}

// high
private static void loadBitmapSharp2() {
    float[] sharp = { -0.15f, -0.15f, -0.15f, -0.15f, 2.2f, -0.15f, -0.15f,
            -0.15f, -0.15f
    };
 //you call the method above and just paste the bitmap you want to apply it and the float of above
    yourbitmap = doSharpen(getbitmap, sharp));
}

You can also apply them direct to a bitmap without a void, its fast simple and there are good results!

like image 193
Blerim Blerii Avatar answered Dec 07 '25 18:12

Blerim Blerii