. f Android make Image Sharper | Android image Blur Issue ~ Android Developers Blog

Sunday, 29 December 2013

Android make Image Sharper | Android image Blur Issue

Hello Droid Guys,
    Today , I am going to share a sample code which helps you in making image
more sharper and clear also helps you in fixing image Blur issue .This is based on
Convolution Matrix Theorem.

About Convolution Matrix : Check this link

Image Before:


Image After :


 1. ImageHelper.java : This is our helper class which helps in processing image
     sharper/


  1. package com.exampl.imagerun;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Color;  
  5.    
  6. public class ImageHelper  
  7. {  
  8.     public static final int SIZE = 3;  
  9.    
  10.     public double[][] Matrix;  
  11.     public double Factor = 1;  
  12.     public double Offset = 1;  
  13.    
  14.    //Constructor with argument of size  
  15.     public ImageHelper(int size) {  
  16.         Matrix = new double[size][size];  
  17.     }  
  18.    
  19.     public void setAll(double value) {  
  20.         for (int x = 0; x < SIZE; ++x) {  
  21.             for (int y = 0; y < SIZE; ++y) {  
  22.                 Matrix[x][y] = value;  
  23.             }  
  24.         }  
  25.     }  
  26.    
  27.     public void applyConfig(double[][] config) {  
  28.         for(int x = 0; x < SIZE; ++x) {  
  29.             for(int y = 0; y < SIZE; ++y) {  
  30.                 Matrix[x][y] = config[x][y];  
  31.             }  
  32.         }  
  33.     }  
  34.    
  35.     public static Bitmap computeConvolution3x3(Bitmap src, ImageHelper matrix) {  
  36.         int width = src.getWidth();  
  37.         int height = src.getHeight();  
  38.         Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());  
  39.    
  40.         int A, R, G, B;  
  41.         int sumR, sumG, sumB;  
  42.         int[][] pixels = new int[SIZE][SIZE];  
  43.    
  44.         for(int y = 0; y < height - 2; ++y) {  
  45.             for(int x = 0; x < width - 2; ++x) {  
  46.    
  47.                 // get pixel matrix  
  48.                 for(int i = 0; i < SIZE; ++i) {  
  49.                     for(int j = 0; j < SIZE; ++j) {  
  50.                         pixels[i][j] = src.getPixel(x + i, y + j);  
  51.                     }  
  52.                 }  
  53.    
  54.                 // get alpha of center pixel  
  55.                 A = Color.alpha(pixels[1][1]);  
  56.    
  57.                 // init color sum  
  58.                 sumR = sumG = sumB = 0;  
  59.    
  60.                 // get sum of RGB on matrix  
  61.                 for(int i = 0; i < SIZE; ++i) {  
  62.                     for(int j = 0; j < SIZE; ++j) {  
  63.                         sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);  
  64.                         sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);  
  65.                         sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);  
  66.                     }  
  67.                 }  
  68.    
  69.                 // get final Red  
  70.                 R = (int)(sumR / matrix.Factor + matrix.Offset);  
  71.                 if(R < 0) { R = 0; }  
  72.                 else if(R > 255) { R = 255; }  
  73.    
  74.                 // get final Green  
  75.                 G = (int)(sumG / matrix.Factor + matrix.Offset);  
  76.                 if(G < 0) { G = 0; }  
  77.                 else if(G > 255) { G = 255; }  
  78.    
  79.                 // get final Blue  
  80.                 B = (int)(sumB / matrix.Factor + matrix.Offset);  
  81.                 if(B < 0) { B = 0; }  
  82.                 else if(B > 255) { B = 255; }  
  83.    
  84.                 // apply new pixel  
  85.                 result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));  
  86.             }  
  87.         }  
  88.    
  89.         // final image  
  90.         return result;  
  91.     }  
  92. }  

 Now, In our Activity class we need to add following code:

  1. ImageView imageView=(ImageView)findViewById(R.id.image);  


Now set image on image view

  1. image.setImageBitmap(sharpenImage(BitmapFactory.  

decodeResource(getResources(),images[i]),12));

And, here we are using Convolution Matrix Theorem to make image sharper.

  1. public Bitmap sharpenImage(Bitmap src, double weight) {  
  2.    // set sharpness configuration  
  3.    double[][] SharpConfig = new double[][] { { 0, -20 },  
  4.    { -2, weight, -2 }, { 0, -20 } };  
  5.    // create convolution matrix instance  
  6.    ImageHelper convMatrix = new ImageHelper(3);  
  7.    // apply configuration  
  8.    convMatrix.applyConfig(SharpConfig);  
  9.    // set weight according to factor  
  10.    convMatrix.Factor = weight - 8;  
  11.    return ImageHelper.computeConvolution3x3(src, convMatrix);  
  12. }  


Hope this will help some one.
Enjoy Coding :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

1 comments:

  1. Hi Mukesh Yadav, this blog help me a lot. Thank you.
    Regards
    Murali.

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.