android - Android 图像锐化的颜色矩阵

标签 android matrix colormatrix image-effects

我有一个类使用颜色矩阵在 ImageView 上添加图像效果。我有几个用于对比度、曝光、温度和饱和度的颜色矩阵。 例如:

public static Bitmap changeContrast(Bitmap bmp, float contrast)
{
    ColorMatrix cm = new ColorMatrix(new float[]
            {
                    contrast, 0, 0, 0, 0,
                    0, contrast, 0, 0, 0,
                    0, 0, contrast, 0, 0,
                    0, 0, 0, 1, 0
            });

    return getBitmapFromColorMatrix(cm, bmp);
}

现在我的问题是图像的锐化。这些似乎没有颜色矩阵。请帮忙。

我尝试使用 here 中的代码锐化图像但处理时间太长,我不知道应该将哪些有效值传递给 sharpenImage(Bitmap src, double weight) 方法的权重参数。

最佳答案

可能为时已晚,但我只是想分享一种快速锐化图像的方法。

 public static Bitmap doSharpen(Bitmap original, float[] radius) {  
      Bitmap bitmap = Bitmap.createBitmap(
              original.getWidth(), original.getHeight(),
              Bitmap.Config.ARGB_8888);

          RenderScript rs = RenderScript.create(yourContext);

          Allocation allocIn = Allocation.createFromBitmap(rs, original);
          Allocation allocOut = Allocation.createFromBitmap(rs, bitmap);

          ScriptIntrinsicConvolve3x3 convolution
              = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs));
          convolution.setInput(allocIn);
          convolution.setCoefficients(radius);
          convolution.forEach(allocOut);

          allocOut.copyTo(bitmap);       
          rs.destroy();   

          return bitmap;                   

}

这是我创建的 3 种不同的锐化类型:

// low
private static void loadBitmapSharp() {
    float[] sharp = { -0.60f, -0.60f, -0.60f, -0.60f, 5.81f, -0.60f,
            -0.60f, -0.60f, -0.60f };
//you call the method above and just paste the bitmap you want to apply it and the float of above
    yourbitmap = doSharpen(getbitmap, sharp));
}

// medium
private static void loadBitmapSharp1() {
    float[] sharp = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f, -1.0f, 0.0f, -1.0f,
            0.0f

    }; 
//you call the method above and just paste the bitmap you want to apply it and the float of above
    yourbitmap = doSharpen(getbitmap, sharp));
}

// high
private static void loadBitmapSharp2() {
    float[] sharp = { -0.15f, -0.15f, -0.15f, -0.15f, 2.2f, -0.15f, -0.15f,
            -0.15f, -0.15f
    };
 //you call the method above and just paste the bitmap you want to apply it and the float of above
    yourbitmap = doSharpen(getbitmap, sharp));
}

您也可以将它们直接应用到没有空位的位图上,它快速简单并且有很好的效果!

关于android - Android 图像锐化的颜色矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175032/

相关文章:

android - 无法运行安卓应用

c++ - 访问 XMMatrix 的 float - () 运算符不起作用

c# - 澄清 ColorMatrix 转换的工作原理

android - RadioGroup 中的自定义 RadioButton 没有独占选择

java - 如何从选择了上下文菜单选项的 View 中获取?

android - 在特定时间取消重复闹钟

java - 交换 2 个特定的 JButton

c++ - OpenGL 矩阵乘法 C++

c# - 如何处理 ColorMatrix 溢出?

css - 使用 feColorMatrix 匹配 SVG 中的颜色