简体   繁体   English

如何在android中锐化图像?

[英]How to sharpen a image in android?

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

but I cannot get the ideal effect. 但我无法达到理想的效果。 Can someone give more clue, or tell me what is mistake in my code? 有人可以提供更多线索,或者告诉我代码中的错误是什么?

Thank you. 谢谢。

You are missing range checking on the rgb values passed to Color.rgb(); 您缺少对传递给Color.rgb()的rgb值的范围检查; you need to normalize rgb values in the [0..255] range before calling Color.rgb() method: 在调用Color.rgb()方法之前,需要规范化[0..255]范围内的rgb值:

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

Return a color-int from red, green, blue components. 从红色,绿色,蓝色组件返回color-int。 The alpha component is implicity 255 (fully opaque). alpha分量是重要的255(完全不透明)。 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 . 这些组件值应为[0..255], 但未执行范围检查,因此如果它们超出范围,则返回的颜色未定义 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..255]颜色绿色绿色分量[0..255]颜色蓝色蓝色分量[0..255]颜色

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

as an alternative 作为备选

 red = (Color.red(a1) + (Color.red(a2))*(-1) + Color.red(a3) + 
       (Color.red(a4))*(-1) + (Color.red(a6))*(-1) + Color.red(a7) + 
       (Color.red(a8))*(-1) + Color.red(a9))    + Color.red(a5)*5 ;
 if(red < 0 )
    red = 0;
 if(red > 255)
    red = 255;

green = (Color.green(a1) + (Color.green(a2)) *(-1) + Color.green(a3) + 
        (Color.green(a4)) *(-1) + (Color.green(a6))*(-1) + Color.green(a7) + 
        (Color.green(a8))*(-1) + Color.green(a9)) + Color.green(a5)*5 ;
if(green < 0 )
    green = 0;
if(green > 255)
    green = 255;

blue = (Color.blue(a1) + (Color.blue(a2))*(-1) + Color.blue(a3) + 
       (Color.blue(a4))*(-1) + (Color.blue(a6))*(-1) + Color.blue(a7) + 
       (Color.blue(a8)) *(-1) + Color.blue(a9))   + Color.blue(a5)*5 ;
if(blue < 0 )
    blue = 0;
if(blue > 255)
    blue = 255;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM