
Si recuerdan que hace tiempo les dije que a una imagen la deberíamos de ver como una matriz dependiendo del tipo de imagen que es, si es una de colores es una matriz en 3 dimensiones, si es una en escala de grises pues debería ser una matriz con 2 dimensiones. Les recomiendo bajar el libro de "Digital Image Processing" del gran maestro

Pero para los flojos; aquí unas imágenes sacadas de ese libro para mayor entendimiento de lo que quiero decir con ver una imagen con una matriz de datos.

El origen o inicio de nuestra matriz claro esta dependiendo del lenguaje de programacion que estamos usando sera el valor I(0,0) o I(1,1) de ahi hasta los limites que en este caso es la dimension de la imagen digamos que una imagen es de 35x35 pixeles; eso seria nuestro limite, si no nos marcara error al correr el codigo


Pongamos el caso de esa fotografía lo que veríamos o deberíamos tener en nuestra matriz son las intensidades de cada pixel de la imagen apenas en el pequeño recuadro del origen seleccionado.

En esta se nota mas lo de las intensidades.
Bueno, por tal motivo hoy les explicare como pasar de un Bitmap a una matriz y regresar de una matriz a un Bitmap, para ir empezando a adentrando a los filtros algo mas complicado en lo que es procesamiento de imágenes.
Nuestro código no necesita muchas librerías ya que la mayoría de las cosas la vamos a hacer como dicen por ahí "ir a pie".
Cita:
Estas son las únicas que uso. Declaro mis variables globales dentro de mi Activityimport android.app.Activity;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
Cita:
Luego voy por la imagen insertándolo en mi variable ImagImageView view; Bitmap Imag;
Cita:
De igual forma le saco sus dimensiones para poder usarlas luego, cargo mi view donde mostrare mi imagen. Imag = BitmapFactory.decodeResource(getResources(), R.drawable.tumblr);
int picw = Imag.getWidth(); int pich = Imag.getHeight();
view = (ImageView)findViewById(R.id.ImView);
int picw = Imag.getWidth(); int pich = Imag.getHeight();
view = (ImageView)findViewById(R.id.ImView);
La magia del asunto para crear una matriz en java es con la siguiente linea de codigo
Cita:
Bueno ya tenemos lo inicial para empezar todo el asunto, ahora a crear nuestra función de matrizacion que raro suena eso int matriz[][][] = new int[picw][pich][4];

Mi función la llame BitToMat, ustedes pónganle el nombre que quieran; que es lo que va a recibir nuestra función

Cita:
Luego que le podemos hacer a esto, pues muchas cosas la verdad ya tenemos nuestra imagen en forma de matriz, podemos hacer lo que vimos en las 3 partes anteriores aplicando matriz y los 2 for's. Un ejemplo es la de pasar a escala de gris.private int[][][] BitToMat(Bitmap bmp)
{
int picw = bmp.getWidth(); int pich = bmp.getHeight();
int[] pix = new int[picw * pich];
bmp.getPixels(pix, 0, picw, 0, 0, picw, pich);
int matriz[][][] = new int[picw][pich][4];
for (int y = 0; y < pich; y++)
for (int x = 0; x < picw; x++)
{
int index = y * picw + x;
matriz[x][y][0] = (pix[index] >> 24) & 0xff;
matriz[x][y][1] = (pix[index] >> 16) & 0xff;
matriz[x][y][2] = (pix[index] >> 8) & 0xff;
matriz[x][y][3] = pix[index] & 0xff;
}
return matriz;
}
{
int picw = bmp.getWidth(); int pich = bmp.getHeight();
int[] pix = new int[picw * pich];
bmp.getPixels(pix, 0, picw, 0, 0, picw, pich);
int matriz[][][] = new int[picw][pich][4];
for (int y = 0; y < pich; y++)
for (int x = 0; x < picw; x++)
{
int index = y * picw + x;
matriz[x][y][0] = (pix[index] >> 24) & 0xff;
matriz[x][y][1] = (pix[index] >> 16) & 0xff;
matriz[x][y][2] = (pix[index] >> 8) & 0xff;
matriz[x][y][3] = pix[index] & 0xff;
}
return matriz;
}
Cita:
Y para mandar a nuestro view, debemos hacer nuestra función que pasara la imagen de una matriz a un bitmap. private int[][][] GrayScale(int mat[][][], int w, int h)
{
int matriz[][][] = new int[w][h][4];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
int r = mat[x][y][1];
int g = mat[x][y][2];
int b = mat[x][y][3];
int R = (int)(0.299*r + 0.587*g + 0.114*b);
matriz[x][y][1] = R; matriz[x][y][2] = R; matriz[x][y][3] = R;
}
return matriz;
}
{
int matriz[][][] = new int[w][h][4];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
int r = mat[x][y][1];
int g = mat[x][y][2];
int b = mat[x][y][3];
int R = (int)(0.299*r + 0.587*g + 0.114*b);
matriz[x][y][1] = R; matriz[x][y][2] = R; matriz[x][y][3] = R;
}
return matriz;
}
Cita:
Y tendremos el mismo resultado; pero ahora desde una perspectiva de matrices. Casi se me olvida, la parte que faltaría complementar en nuestro main seria lo siguiente.private Bitmap MatToBit(int mat[][][], int w, int h)
{
int[] pix = new int[w * h];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
int index = y * w + x;
pix[index] = 0xff000000 | (mat[x][y][1] << 16) | (mat[x][y][2] << 8) | mat[x][y][3];
}
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
bm.setPixels(pix, 0, w, 0, 0, w, h);
return bm;
}
{
int[] pix = new int[w * h];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
int index = y * w + x;
pix[index] = 0xff000000 | (mat[x][y][1] << 16) | (mat[x][y][2] << 8) | mat[x][y][3];
}
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
bm.setPixels(pix, 0, w, 0, 0, w, h);
return bm;
}
Cita:
Les regalo un efecto que me gusto mucho aplicar en imágenes, se le conoce como Blur y no es la banda de música que por cierto a todos se les vino en la cabeza la canción de "Song 2" jaja matriz = BitToMat(Imag);
Bitmap bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_4444);
bm = MatToBit(GrayScale(matriz, picw, pich), picw, pich);
view.setImageBitmap(bm);
Bitmap bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_4444);
bm = MatToBit(GrayScale(matriz, picw, pich), picw, pich);
view.setImageBitmap(bm);

Cita:
El efecto seria algo como esto private int[][][] Blur(int mat[][][], int w, int h)
{
int r1 = 0, r2 = 0, r3 = 0;
int salida[][][] = new int[w][h][4];
for (int y = 1; y < h-1; y++)
{
for (int x = 1; x < w-1; x++)
{
for(int i = -1; i<1; i++)
for(int j = -1; j<1; j++)
{
r1 = r1 + mat[x+j][y+i][1];
r2 = r2 + mat[x+j][y+i][2];
r3 = r3 + mat[x+j][y+i][3];
}
salida[x][y][1] = (int) Math.round(r1/9);
salida[x][y][2] = (int) Math.round(r2/9);
salida[x][y][3] = (int) Math.round(r3/9);
r1 = 0; r2 = 0; r3 = 0;
}
}
return salida;
}
{
int r1 = 0, r2 = 0, r3 = 0;
int salida[][][] = new int[w][h][4];
for (int y = 1; y < h-1; y++)
{
for (int x = 1; x < w-1; x++)
{
for(int i = -1; i<1; i++)
for(int j = -1; j<1; j++)
{
r1 = r1 + mat[x+j][y+i][1];
r2 = r2 + mat[x+j][y+i][2];
r3 = r3 + mat[x+j][y+i][3];
}
salida[x][y][1] = (int) Math.round(r1/9);
salida[x][y][2] = (int) Math.round(r2/9);
salida[x][y][3] = (int) Math.round(r3/9);
r1 = 0; r2 = 0; r3 = 0;
}
}
return salida;
}

Recuerden pasar a mis publicaciones anteriores para intentar lo que se vio en ellas y aplicar aquí.
http://www.forosdelweb.com/f165/proc...-parte-935033/
http://www.forosdelweb.com/f165/proc...-parte-934774/
http://www.forosdelweb.com/f165/proc...ndroid-933993/