图像处理(基于android)---滤镜实现的一点看法和总结

来源:互联网 发布:淘宝联盟的返现为什么 编辑:程序博客网 时间:2024/05/16 06:46

唠叨几句

我们知道,滤镜在目前社交App中发布用户图像处理中占据了一定的使用频率,有的效果很炫,可以稍微提高用户对app的粘合度。经过传说中的”逆向工程”技术发现,目前存在的大部分滤镜效果其实不是真正的滤镜,对于一个图像处理的小白菜鸟来说,真想说一句,”你大爷的!”。当然,开个玩笑。当下,滤镜处理技术分为两种:

  1. 真实滤镜

    对于图像处理届的大神介绍,真正的滤镜其实就是颜色矩阵变换的结果,本质上讲,就是对图像像素点的A,R,G,B的数据变换处理。

    实现真实滤镜的方法也很多,比如,通过RGB变换公式,颜色矩阵变换(其本质也是RGB变换公式),还有改变图像的色相(色调),亮度,饱和度。
    测试图:

这里写图片描述

 1. RGB变换公式 例如:  反色效果(照片底片) r' = 255 - r; g' = 255 - g; b' = 255 - b; 怀旧效果 r' = 0.393 * r + 0.769 * g + 0.189 * b; g' = 0.349 * r + 0.686 * g + 0.168 * b; b' = 0.272 * r + 0.534 * g + 0.131 * b; 2. 颜色矩阵变换

这里写图片描述

矩阵的运算规则是矩阵A的一行乘以矩阵C的一列作为矩阵R的一行,

C矩阵是图片中包含的ARGB信息,R矩阵是用颜色矩阵应用于C之后的新的颜色分量,运算结果如下:

R’ = a*R + b*G + c*B + d*A + e;
G’ = f*R + g*G + h*B + i*A + j;
B’ = k*R + l*G + m*B + n*A + o;
A’ = p*R + q*G + r*B + s*A + t;

private void functionFilter()    {        float[] maxtriFilter = new float[20];        for (int index = 0; index < 20; index++)        {            EditText et = ((EditText) mGridLayout.getChildAt(index));            if (index < 15 && (index + 2) % 5 == 0)            {                maxtriFilter[index] = 0.0f;                et.setText("0");                continue;            }            if (index >= 15 && index != 18)            {                maxtriFilter[index] = 0.0f;                et.setText("0");                continue;            }            if (index == 18)            {                maxtriFilter[index] = 1.0f;                et.setText("1");                continue;            }            float value = Float.valueOf((float) (Math.random() * 255.0f));            maxtriFilter[index] = value;            et.setText("" + maxtriFilter[index]);        }        Bitmap bmp = Bitmap.createBitmap(mBitmapSrc.getWidth(),                mBitmapSrc.getHeight(), Config.ARGB_8888);        Canvas canvas = new Canvas(bmp);        Paint paint = new Paint();        ColorMatrix mColorMatrix = new ColorMatrix();        mColorMatrix.set(maxtriFilter);        paint.setColorFilter(new ColorMatrixColorFilter(mColorMatrix));        canvas.drawBitmap(mBitmapSrc, 0, 0, paint);        pictureShow.setImageBitmap(bmp);    }

这里写图片描述

注:感觉特不爽。应该是随机数太大了,那么换成0 ~1的小数试试

float value = Float.valueOf((float) (Math.random()));

而且 颜色分量也可以去掉了,要不然程度太大了,但也可以加上。

private void functionFilter()    {        float[] maxtriFilter = new float[20];        for (int index = 0; index < 20; index++)        {            if (index < 15 && ((index + 2) % 5 == 0 || (index + 1) % 5 == 0))            {                maxtriFilter[index] = 0.0f;                continue;            }            if (index >= 15 && index != 18)            {                maxtriFilter[index] = 0.0f;                continue;            }            if (index == 18)            {                maxtriFilter[index] = 1.0f;                continue;            }            float value = Float.valueOf((float) (Math.random()));            maxtriFilter[index] = value;        }        for (int index = 0; index < 20; index++)        {            EditText et = ((EditText) mGridLayout.getChildAt(index));            et.setText("" + maxtriFilter[index]);        }        Bitmap bmp = Bitmap.createBitmap(mBitmapSrc.getWidth(),                mBitmapSrc.getHeight(), Config.ARGB_8888);        Canvas canvas = new Canvas(bmp);        Paint paint = new Paint();        ColorMatrix mColorMatrix = new ColorMatrix();        mColorMatrix.set(maxtriFilter);        paint.setColorFilter(new ColorMatrixColorFilter(mColorMatrix));        canvas.drawBitmap(mBitmapSrc, 0, 0, paint);        pictureShow.setImageBitmap(bmp);    }

这里写图片描述

效果是不是很炫。那么我加上颜色分量试试

if (index < 15 && ((index + 2) % 5 == 0                     //|| (index + 1) % 5 == 0                    )                    )            {                maxtriFilter[index] = 0.0f;                continue;            }

这里写图片描述

效果也不错。

未完待续
3. 改变色相,亮度,饱和度

  1. 图层混合滤镜
0 0