问题描述
我要锐化图像,我的code为如下:
I want to sharpen a image, my code is given below:
public Bitmap RuiHuaBitmap(Bitmap bitmap) {
int width, height;
height = bitmap.getHeight();
width = bitmap.getWidth();
int red, green, blue;
int a1, a2, a3, a4, a5, a6, a7, a8, a9;
Bitmap bmpBlurred = Bitmap.createBitmap(width, height,bitmap.getConfig());
Canvas canvas = new Canvas(bmpBlurred);
canvas.drawBitmap(bitmap, 0, 0, null);
for (int i = 1; i < width - 1; i++) {
for (int j = 1; j < height - 1; j++) {
a1 = bitmap.getPixel(i - 1, j - 1);
a2 = bitmap.getPixel(i - 1, j);
a3 = bitmap.getPixel(i - 1, j + 1);
a4 = bitmap.getPixel(i, j - 1);
a5 = bitmap.getPixel(i, j);
a6 = bitmap.getPixel(i, j + 1);
a7 = bitmap.getPixel(i + 1, j - 1);
a8 = bitmap.getPixel(i + 1, j);
a9 = bitmap.getPixel(i + 1, j + 1);
red = (Color.red(a1) + Color.red(a2) + Color.red(a3) + Color.red(a4) + Color.red(a6) + Color.red(a7) + Color.red(a8) + Color.red(a9)) *(-1) + Color.red(a5)*9 ;
green = (Color.green(a1) + Color.green(a2) + Color.green(a3) + Color.green(a4) + Color.green(a6) + Color.green(a7) + Color.green(a8) + Color.green(a9)) *(-1) + Color.green(a5)*9 ;
blue = (Color.blue(a1) + Color.blue(a2) + Color.blue(a3) + Color.blue(a4) + Color.blue(a6) + Color.blue(a7) + Color.blue(a8) + Color.blue(a9)) *(-1) + Color.blue(a5)*9 ;
bmpBlurred.setPixel(i, j, Color.rgb(red, green, blue));
}
}
return bmpBlurred;
}
但我不能得到理想的效果。有人可以给更多的线索,或者告诉我什么是错在我的code?
but I cannot get the ideal effect. Can someone give more clue, or tell me what is mistake in my code?
感谢你。
推荐答案
您缺少转嫁到Color.rgb RGB值范围检查();你需要调用Color.rgb前正常化在[0..255]范围内的RGB值()方法:
You are missing range checking on the rgb values passed to Color.rgb(); you need to normalize rgb values in the [0..255] range before calling Color.rgb() method:
公共静态INT RGB(INT红色,诠释绿色,诠释蓝)自:API级别 1
返回一个颜色-INT由红,绿,蓝分量。阿尔法 组分是含蓄255(完全不透明)。这些元件值 应该是[0..255],但是没有进行范围检查,因此,如果他们 超出范围,则返回的颜色是不确定的。参数红色红 色泽绿青分量的组成部分[0..255] [0..255]的 颜色蓝色蓝色分量[0..255]颜色
Return a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined. Parameters red Red component [0..255] of the color green Green component [0..255] of the color blue Blue component [0..255] of the color
您卷积矩阵似乎是很好的一个残废转变:
Your convolution matrix seems good for a shapen transformation:
0 0 0 0 0
0 -1 -1 -1 0
0 -1 9 -1 0
0 -1 -1 -1 0
0 0 0 0 0
如果你认为你的效果是太强大了,你也可以尝试使用:
if you think your effect is too strong, you could also try with:
0 0 0 0 0
0 0 -1 0 0
0 -1 5 -1 0
0 0 -1 0 0
0 0 0 0 0
作为替代
这篇关于如何提高在Android的一个形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!