gpt4 book ai didi

java - 如何锐化android中的图像?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:23:07 24 4
gpt4 key购买 nike

我想锐化图像,我的代码如下:

 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;
}

但我无法获得理想的效果。有人可以提供更多线索,或者告诉我我的代码中有什么错误吗?

谢谢。

最佳答案

您缺少对传递给 Color.rgb() 的 rgb 值的范围检查;在调用 Color.rgb() 方法之前,您需要规范化 [0..255] 范围内的 rgb 值:

public static int rgb (int red, int green, int blue) Since: API Level 1

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

您的卷积矩阵似乎适合形状变换:

 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

如果你觉得你的效果太强了,你也可以试试:

 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

作为替代

关于java - 如何锐化android中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6980418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com