May 13, 2012

Image processing on Android, the basic logic of Convolution Matrix.


At the beginning when I prepare the post "Create blur edge on bitmap, using BlurMaskFilter", I want to apply blur effect on bitmap actually. But I can't find any build-in API do so it! In the coming posts, I try to do it myself.

I found a page in http://docs.gimp.org/en/plug-in-convmatrix.html, it explain Convolution Matrix very clear and easy understand. So my first step is to implement the Convolution Matrix according to the example.



package com.AndroidImageProcessing;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidImageProcessingActivity extends Activity {
 
 final static int KERNAL_WIDTH = 3;
 final static int KERNAL_HEIGHT = 3;
 final static int MATRIX_WIDTH = 5;
 final static int MATRIX_HEIGHT = 5;
 
 int[][] kernal ={
   {0, 1, 0},
   {0, 0, 0},
   {0, 0, 0}
 };
 
 int[][] dataOriginal = {
   {35, 40, 41, 45, 50},
   {40, 40, 42, 46, 52},
   {42, 46, 50, 55, 55},
   {48, 52, 56, 58, 60},
   {56, 60, 65, 70, 75}
 };
 
 int[][] dataResult = new int[MATRIX_WIDTH][MATRIX_HEIGHT];
 
 TextView textOriginal, textResult;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textOriginal = (TextView)findViewById(R.id.original);
        textResult = (TextView)findViewById(R.id.result);
        
        dataResult = convolution(dataOriginal, kernal);
        
        textOriginal.setText("dataOriginal:\n" + getDataString(dataOriginal) + "\n");
        textResult.setText("dataResult:\n" + getDataString(dataResult) + "\n");
    }
    
    private String getDataString(int[][] d){
     String s = "";
     
     for(int i = 0; i < d.length; i++){
      for(int j = 0; j < d[i].length; j++){
       s += " " + String.format("%02d", d[i][j]);
      }
      s += "\n"; 
     }
     
     return s;
    }
    
    private int[][] convolution(int[][] src, int[][] knl){
     int[][] convolutionResult = new int[MATRIX_WIDTH][MATRIX_HEIGHT];
     
     for(int i = 1; i <= src.length-2; i++){
      for(int j = 1; j <= src[i].length-2; j++){
       
       //get the surround 3*3 pixel of current src[i][j] into a matrix subSrc[][]
       int[][] subSrc = new int[KERNAL_WIDTH][KERNAL_HEIGHT];
       for(int k = 0; k < subSrc.length; k++){
        for(int l = 0; l < subSrc[k].length; l++){
         subSrc[k][l] = src[i-1+k][j-1+l]; 
        }
       }
       
       //subSum = subSrc[][] * knl[][]
       int subSum = 0;
       for(int k = 0; k < knl.length; k++){
        for(int l = 0; l < knl[k].length; l++){
         subSum += subSrc[k][l] * knl[k][l];
        }
       }

       convolutionResult[i][j] = subSum;
      } 
     }
     
     return convolutionResult;
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView
        android:id="@+id/original"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


Next:
- Apply Blur effect on Android, using Convolution Matrix
- Sharpen image, using Convolution Matrix
- Process image in background thread
- Edge Enhance
- Edge Detect
- Gaussian Blur
- Display image in full size
- Unsharp Mask (USM)
- Reduce bitmap by setting inSampleSize of BitmapFactory.Options
- Auto resize bitmap to fit View
- Image Processing with open JPG file dialog
- Inverse bitmap to generate film like picture
- Resize bitmap: Bitmap.createScaledBitmap vs BitmapFactory.Options.inSampleSize



1 comment:

  1. "I can't find any build-in API do so it! In the coming posts, I try to do it myself"
    you're my Idol,bro !

    ReplyDelete

Infolinks In Text Ads