Showing posts with label ScriptIntrinsicConvolve3x3. Show all posts
Showing posts with label ScriptIntrinsicConvolve3x3. Show all posts

interactive exercise of ScriptIntrinsicConvolve3x3

Last example show how to "Sharpen and blur bitmap using ScriptIntrinsicConvolve3x3", here is a interactive exercise of ScriptIntrinsicConvolve3x3. You can adjust coefficients of ScriptIntrinsicConvolve3x3, and view the result.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicConvolve3x3;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

ImageView image1, image2;
SeekBar coeff0, coeff1, coeff2;
SeekBar coeff3, coeff4, coeff5;
SeekBar coeff6, coeff7, coeff8;
SeekBar devBy;
Button btnBlur, btnOrg, btnSharpen;

TextView textCoeff;

float[] matrix = {
0, 0, 0,
0, 1, 0,
0, 0, 0
};

Bitmap bitmapOriginal, bitmapCoeff;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

coeff0 = (SeekBar)findViewById(R.id.coeff0);
coeff1 = (SeekBar)findViewById(R.id.coeff1);
coeff2 = (SeekBar)findViewById(R.id.coeff2);
coeff3 = (SeekBar)findViewById(R.id.coeff3);
coeff4 = (SeekBar)findViewById(R.id.coeff4);
coeff5 = (SeekBar)findViewById(R.id.coeff5);
coeff6 = (SeekBar)findViewById(R.id.coeff6);
coeff7 = (SeekBar)findViewById(R.id.coeff7);
coeff8 = (SeekBar)findViewById(R.id.coeff8);
devBy = (SeekBar)findViewById(R.id.coeffdivby);
textCoeff = (TextView)findViewById(R.id.textcoeff);
btnBlur = (Button)findViewById(R.id.blur);
btnOrg = (Button)findViewById(R.id.org);
btnSharpen = (Button)findViewById(R.id.sharpen);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);

bitmapOriginal = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

coeff0.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff1.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff2.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff3.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff4.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff5.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff6.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff7.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff8.setOnSeekBarChangeListener(OnCoeffChangeListener);
devBy.setOnSeekBarChangeListener(OnCoeffChangeListener);

ReloadImage();

btnBlur.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
coeff0.setProgress(1+10);
coeff1.setProgress(1+10);
coeff2.setProgress(1+10);
coeff3.setProgress(1+10);
coeff4.setProgress(1+10);
coeff5.setProgress(1+10);
coeff6.setProgress(1+10);
coeff7.setProgress(1+10);
coeff8.setProgress(1+10);
devBy.setProgress(9-1);
ReloadImage();
}});

btnOrg.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
coeff0.setProgress(0+10);
coeff1.setProgress(0+10);
coeff2.setProgress(0+10);
coeff3.setProgress(0+10);
coeff4.setProgress(1+10);
coeff5.setProgress(0+10);
coeff6.setProgress(0+10);
coeff7.setProgress(0+10);
coeff8.setProgress(0+10);
devBy.setProgress(1-1);
ReloadImage();
}});

btnSharpen.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
coeff0.setProgress(0+10);
coeff1.setProgress(-1+10);
coeff2.setProgress(0+10);
coeff3.setProgress(-1+10);
coeff4.setProgress(5+10);
coeff5.setProgress(-1+10);
coeff6.setProgress(0+10);
coeff7.setProgress(-1+10);
coeff8.setProgress(0+10);
devBy.setProgress(1-1);
ReloadImage();
}});
}

OnSeekBarChangeListener OnCoeffChangeListener = new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
ReloadImage();
}};

private void ReloadImage(){
updateMatrix();
bitmapCoeff = createBitmap_convolve(bitmapOriginal, matrix);
image1.setImageBitmap(bitmapCoeff);
image2.setImageBitmap(bitmapCoeff);
}

private void updateMatrix(){
float div = devBy.getProgress() + 1;
matrix[0] = (coeff0.getProgress()-10)/div;
matrix[1] = (coeff1.getProgress()-10)/div;
matrix[2] = (coeff2.getProgress()-10)/div;
matrix[3] = (coeff3.getProgress()-10)/div;
matrix[4] = (coeff4.getProgress()-10)/div;
matrix[5] = (coeff5.getProgress()-10)/div;
matrix[6] = (coeff6.getProgress()-10)/div;
matrix[7] = (coeff7.getProgress()-10)/div;
matrix[8] = (coeff8.getProgress()-10)/div;

textCoeff.setText(
matrix[0] + " , " + matrix[1] + " , " + matrix[2] + " , \n" +
matrix[3] + " , " + matrix[4] + " , " + matrix[5] + " , \n" +
matrix[6] + " , " + matrix[7] + " , " + matrix[8]);
}

private Bitmap createBitmap_convolve(Bitmap src, float[] coefficients) {

Bitmap result = Bitmap.createBitmap(src.getWidth(),
src.getHeight(), src.getConfig());

RenderScript renderScript = RenderScript.create(this);

Allocation input = Allocation.createFromBitmap(renderScript, src);
Allocation output = Allocation.createFromBitmap(renderScript, result);

ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3
.create(renderScript, Element.U8_4(renderScript));
convolution.setInput(input);
convolution.setCoefficients(coefficients);
convolution.forEach(output);

output.copyTo(result);
renderScript.destroy();
return result;
}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<SeekBar
android:id="@+id/coeff0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<SeekBar
android:id="@+id/coeff3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="11" />

<SeekBar
android:id="@+id/coeff5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<SeekBar
android:id="@+id/coeff6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />
</LinearLayout>

<SeekBar
android:id="@+id/coeffdivby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="19"
android:progress="0" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/blur"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Blur"/>
<Button
android:id="@+id/org"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Original"/>
<Button
android:id="@+id/sharpen"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sharpen"/>

</LinearLayout>

<TextView
android:id="@+id/textcoeff"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

</LinearLayout>



download filesDownload the files.

Sharpen and blur bitmap, convolution using ScriptIntrinsicConvolve3x3

This example show how to create sharpen and blur bitmap, by convolution using ScriptIntrinsicConvolve3x3.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicConvolve3x3;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

ImageView imageA1, imageA2, imageB1, imageB2, imageC1, imageC2;

Bitmap bitmapOriginal, bitmapBlur, bitmapSharpen;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageA1 = (ImageView) findViewById(R.id.imagea1);
imageA2 = (ImageView) findViewById(R.id.imagea2);
imageB1 = (ImageView) findViewById(R.id.imageb1);
imageB2 = (ImageView) findViewById(R.id.imageb2);
imageC1 = (ImageView) findViewById(R.id.imagec1);
imageC2 = (ImageView) findViewById(R.id.imagec2);

bitmapOriginal = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

imageA1.setImageBitmap(bitmapOriginal);
imageA2.setImageBitmap(bitmapOriginal);

// create sharpen bitmap from blur bitmap
bitmapSharpen = createBitmap_convolve(bitmapOriginal, matrix_sharpen);
imageB1.setImageBitmap(bitmapSharpen);
imageB2.setImageBitmap(bitmapSharpen);

// create blur bitmap from original bitmap
bitmapBlur = createBitmap_convolve(bitmapOriginal, matrix_blur);
imageC1.setImageBitmap(bitmapBlur);
imageC2.setImageBitmap(bitmapBlur);

}

float[] matrix_blur =
{ 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f,
1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f,
1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f};

float[] matrix_sharpen =
{ 0, -1, 0,
-1, 5, -1,
0, -1, 0};

private Bitmap createBitmap_convolve(Bitmap src, float[] coefficients) {

Bitmap result = Bitmap.createBitmap(src.getWidth(),
src.getHeight(), src.getConfig());

RenderScript renderScript = RenderScript.create(this);

Allocation input = Allocation.createFromBitmap(renderScript, src);
Allocation output = Allocation.createFromBitmap(renderScript, result);

ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3
.create(renderScript, Element.U8_4(renderScript));
convolution.setInput(input);
convolution.setCoefficients(coefficients);
convolution.forEach(output);

output.copyTo(result);
renderScript.destroy();
return result;
}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ImageView
android:id="@+id/imagea1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imagea2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imageb2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ImageView
android:id="@+id/imagec1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imagec2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

Next:
interactive exercise of ScriptIntrinsicConvolve3x3