Android image processing system 1.4 image sharpening-Edge Detection
Image sharpening-Edge Detection: (Robert Gradient, Sobel Gradient, Laplace Gradient)
@ Author: Zheng Haibo
Related blogs:
1. http://blog.csdn.net/nuptboyzhb/article/details/7925994
2. http://blog.csdn.net/nuptboyzhb/article/details/7926407
1. Robert Gradient
[Figure 1]
2. Sobel Gradient
[Figure 2]
3. Laplace Gradient
[Figure 3]
Key Java code
[Java]
/*
* Robert's operator gradient
*
*/
Public Bitmap Robert tgradient (Bitmap myBitmap ){
// Create new array
Int width = myBitmap. getWidth ();
Int height = myBitmap. getHeight ();
Int [] pix = new int [width * height];
MyBitmap. getPixels (pix, 0, width, 0, 0, width, height );
Matrix dataR = getDataR (pix, width, height );
Matrix dataG = getDataG (pix, width, height );
Matrix dataB = getDataB (pix, width, height );
// Matrix transport Ray = gettransport Ray (pix, width, height );
//////////////////////////////////////// /////////////////
DataR = eachrobert tgradient (dataR, width, height );
DataG = eachrobert tgradient (dataG, width, height );
DataB = eachrobert tgradient (dataB, width, height );
//////////////////////////////////////// ///////////////////////
// Change bitmap to use new array
Bitmap bitmap = makeToBitmap (dataR, dataG, dataB, width, height );
MyBitmap = null;
Pix = null;
Return bitmap;
}
Private Matrix eachrobert tgradient (Matrix tempM, int width, int height ){
Int I, j;
For (I = 0; I <width-1; I ++ ){
For (j = 0; j Int temp = Math. abs (int) tempM. get (I, j)-(int) tempM. get (I, j + 1 ))
+ Math. abs (int) tempM. get (I + 1, j)-(int) tempM. get (I, j + 1 ));
TempM. set (I, j, temp );
}
}
Return tempM;
}
/*
* Sobel operator sharpening
*/
Public Bitmap SobelGradient (Bitmap myBitmap ){
// Create new array
Int width = myBitmap. getWidth ();
Int height = myBitmap. getHeight ();
Int [] pix = new int [width * height];
MyBitmap. getPixels (pix, 0, width, 0, 0, width, height );
Matrix dataR = getDataR (pix, width, height );
Matrix dataG = getDataG (pix, width, height );
Matrix dataB = getDataB (pix, width, height );
Matrix transport Ray = gettransport Ray (pix, width, height );
//////////////////////////////////////// /////////////////
Gradient Ray = eachSobelGradient (gradient Ray, width, height );
DataR = Ray. copy ();
DataG = Ray. copy ();
DataB = Ray. copy ();
//////////////////////////////////////// ///////////////////////
// Change bitmap to use new array
Bitmap bitmap = makeToBitmap (dataR, dataG, dataB, width, height );
MyBitmap = null;
Pix = null;
Return bitmap;
}
Private Matrix eachSobelGradient (Matrix tempM, int width, int height ){
Int I, j;
Matrix resultMatrix = tempM. copy ();
For (I = 1; I <width-1; I ++ ){
For (j = 1; j Int temp1 = Math. abs (int) tempM. get (I + 1, J-1) + 2 * (int) tempM. get (I + 1, j) + (int) tempM. get (I + 1, j + 1 ))
-(Int) tempM. get (I-1, J-1) + 2 * (int) tempM. get (I-1, j) + (int) tempM. get (I-1, J-1 ))));
Int temp2 = Math. abs (int) tempM. get (I-1, j + 1) + 2 * (int) tempM. get (I, j + 1) + (int) tempM. get (I + 1, j + 1 ))
-(Int) tempM. get (I-1, J-1) + 2 * (int) tempM. get (I, J-1) + (int) tempM. get (I + 1, J-1 ))));
Int temp = temp1 + temp2;
ResultMatrix. set (I, j, temp );
}
}
Return resultMatrix;
}
/*
* Laplace sharpening
*/
Public Bitmap LaplaceGradient (Bitmap myBitmap ){
// Create new array
Int width = myBitmap. getWidth ();
Int height = myBitmap. getHeight ();
Int [] pix = new int [width * height];
MyBitmap. getPixels (pix, 0, width, 0, 0, width, height );
Matrix dataR = getDataR (pix, width, height );
Matrix dataG = getDataG (pix, width, height );
Matrix dataB = getDataB (pix, width, height );
Matrix transport Ray = gettransport Ray (pix, width, height );
//////////////////////////////////////// /////////////////
Wedding Ray = eachLaplaceGradient (wedding Ray, width, height );
DataR = Ray. copy ();
DataG = Ray. copy ();
DataB = Ray. copy ();
//////////////////////////////////////// ///////////////////////
// Change bitmap to use new array
Bitmap bitmap = makeToBitmap (dataR, dataG, dataB, width, height );
MyBitmap = null;
Pix = null;
Return bitmap;
}
Private Matrix eachLaplaceGradient (Matrix tempM, int width, int height ){
Int I, j;
Matrix resultMatrix = tempM. copy ();
For (I = 1; I <width-1; I ++ ){
For (j = 1; j Int temp = Math. abs (5 * (int) tempM. get (I, j)-(int) tempM. get (I + 1, j)
-(Int) tempM. get (I-1, j)-(int) tempM. get (I, j + 1)-(int) tempM. get (I, J-1 ));
ResultMatrix. set (I, j, temp );
}
}
Return resultMatrix;
}