MetaSnarf
MetaSnarf

Reputation: 6187

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.

Upvotes: 2

Views: 4972

Answers (3)

AkiraZombie
AkiraZombie

Reputation: 56

Improving the first answer here, adding a seekbar (slider) to change the sharpness of an image.

public Bitmap doSharpen(Bitmap original, float multiplier) {
    float[] sharp = { 0, -multiplier, 0, -multiplier, 5f*multiplier, -multiplier, 0, -multiplier, 0};
    Bitmap bitmap = Bitmap.createBitmap(
            original.getWidth(), original.getHeight(),
            Bitmap.Config.ARGB_8888);

    RenderScript rs = RenderScript.create(MainActivity.this);

    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(sharp);
    convolution.forEach(allocOut);

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

    return bitmap;

}

I had to modify it a little according to my project. Instead of taking the convolution matrix in method, i declared it inside the method. We are going to take the changed variable of seekbar instead.

sharpnessSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            Bitmap sharpenedBitmap = doSharpen(btmp,(float)i);
            imageV.setImageBitmap(sharpenedBitmap);
        }

Inside the setOnSeekBarChangeListener we get the changed value which is int i and pass it with doSharpen method so that it can be used in the convolution matrix which is defined inside doSharpen() Method.

Upvotes: 0

Blerim Blerii
Blerim Blerii

Reputation: 233

Here are the values that i used for color temperature with a seekbar. (you asked them into a comment of one question that i asked)

public static Bitmap doTemperature(Bitmap bmp, float temp){

     ColorMatrix cm = new ColorMatrix(new float[]
             {
             1, 0, 0, temp, 0,
             0, 1, 0, temp/2, 0,
             0, 0, 1, temp/4, 0,
             0, 0, 0, 1, 0

             });

     Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
     Canvas canvas = new Canvas(ret);
     Paint paint = new Paint();
     paint.setColorFilter(new ColorMatrixColorFilter(cm));
     canvas.drawBitmap(bmp, 0, 0, paint);
     return ret;
 }
//you call it with this method
private void loadBitmapTemp() {
    //private Seekbar temp_value; 
    temp_value = (SeekBar) findViewById(R.id.temp_value);
    temp_value.setMax(100);
    temp_value.setProgress(50);

    int progressTemp = temp_value.getProgress();
    progressTemp -= 50;
    float temp = (float) progressTemp / 220;
    progress_text.setVisibility(View.VISIBLE);
    progress_text.setText(" " + progressTemp * 2);
    yourBitmap = doTemperature(getbitmap, temp));
}

Upvotes: 0

Blerim Blerii
Blerim Blerii

Reputation: 233

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!

Upvotes: 16

Related Questions