android圖片特效處理之銳化效果
阿新 • • 發佈:2018-11-16
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
這篇將講到圖片特效處理的銳化效果。跟前面一樣是對畫素點進行處理,演算法是通用的。
演算法原理:
一、簡單演算法:分別獲取當前畫素點和八個周圍畫素點的RGB值,先求出當前畫素點的RGB值與八個畫素點RGB值的和的平均數,再乘以相應的係數,然後在與當前畫素點之和。
例:
ABC
DEF
GHI
對E點進行銳化:
[java] view plain copy print ?- 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;
- 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點進行銳化。
[java] view plain copy print ?- // 拉普拉斯矩陣
- int[] laplacian = newint[] { -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值類似
- // 拉普拉斯矩陣
- 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; }