【轉】三、android圖片特效處理之銳利化效果

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

這篇將講到圖片特效處理的銳利化效果。跟前面一樣是對像素點進行處理,演算法是通用的。

演算法原理:

一、簡單演算法:分別擷取當前像素點和八個周圍像素點的RGB值,先求出當前像素點的RGB值與八個像素點RGB值的和的平均數,再乘以相應的係數,然後在與當前像素點之和。

例:

ABC

DEF

GHI

對E點進行銳利化:

float delta = 0.3;E.r = (E.r - (A.r + B.r + C.r + D.r + F.r + G.r + H.r + I.r) / 8) * delta + E.r;

 

E.g,E.b類似,delta建議取0.3,具體多少無所謂,試一下就知道了。但按照上面原理,沒有達到預期的效果,改變delta的值也不行,所以後面代碼就不貼出來了,感興趣的可以研究一下。

 

二、拉普拉斯變換:將拉普拉斯矩陣中的項與相應點的RGB值之積再乘以相應的係數的和作為當前點的RGB值。

例:用上面的例子,還是對E點進行銳利化。

// 拉普拉斯矩陣int[] laplacian = new int[] { -1, -1, -1, -1, 9, -1, -1, -1, -1 };float delta = 0.3; E.r = A.r * laplacian[0] * delta + B.r * laplacian[1] * delta + C.r * laplacian[2] * delta + D.r * laplacian[3] * delta + E.r * laplacian[4] * delta + F.r * laplacian[5] * delta + G.r * laplacian[6] * delta + H.r * laplacian[7] * delta + I.r * laplacian[8] * delta;// E.g和E.b值類似

下面看:

原圖:

處理後:

 

貌似處理有點問題,中間會看到很多的豎線,很明顯,可能是沒有最佳化好,因為採用了getPiexels() 和setPixels()方法,所以一維數組的對應圖片的寬高有點麻煩。

下面貼代碼,僅供參數,同樣注意圖片的大小,數組大小不能超過虛擬機器規定值。

/**     * 圖片銳利化(拉普拉斯變換)     * @param bmp     * @return     */    private Bitmap sharpenImageAmeliorate(Bitmap bmp)    {        long start = System.currentTimeMillis();        // 拉普拉斯矩陣        int[] laplacian = new int[] { -1, -1, -1, -1, 9, -1, -1, -1, -1 };                int width = bmp.getWidth();        int height = bmp.getHeight();        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);                int pixR = 0;        int pixG = 0;        int pixB = 0;                int pixColor = 0;                int newR = 0;        int newG = 0;        int newB = 0;                int idx = 0;        float alpha = 0.3F;        int[] pixels = new int[width * height];        bmp.getPixels(pixels, 0, width, 0, 0, width, height);        for (int i = 1, length = height - 1; i < length; i++)        {            for (int k = 1, len = width - 1; k < len; k++)            {                idx = 0;                for (int m = -1; m <= 1; m++)                {                    for (int n = -1; n <= 1; n++)                    {                        pixColor = pixels[(i + n) * width + k + m];                        pixR = Color.red(pixColor);                        pixG = Color.green(pixColor);                        pixB = Color.blue(pixColor);                                                newR = newR + (int) (pixR * laplacian[idx] * alpha);                        newG = newG + (int) (pixG * laplacian[idx] * alpha);                        newB = newB + (int) (pixB * laplacian[idx] * alpha);                        idx++;                    }                }                                newR = Math.min(255, Math.max(0, newR));                newG = Math.min(255, Math.max(0, newG));                newB = Math.min(255, Math.max(0, newB));                                pixels[i * width + k] = Color.argb(255, newR, newG, newB);                newR = 0;                newG = 0;                newB = 0;            }        }                bitmap.setPixels(pixels, 0, width, 0, 0, width, height);        long end = System.currentTimeMillis();        Log.d("may", "used time="+(end - start));        return bitmap;    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.