java图像处理之查找表实现图像处理加速
生活随笔
收集整理的這篇文章主要介紹了
java图像处理之查找表实现图像处理加速
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ??在圖像處理中有時候會遇到對數變換、冪律變換等較為復雜的計算,相應的處理時間也會增多,這時候就需要使用查找表來實現圖像處理加速。由于圖像灰度值是一個[0,255]的有限集合,那么只需要提前對[0,255]內所有數字進行計算,生成以輸入值為索引位置,輸出值為對應值的數組,實際對圖像進行計算時,不需要再進行相應計算,只需要根據對應灰度值為索引查找數組中相應的值。
? ? ? ? 以對數變換為例:
? ? ? ? 對數變換公式:s=c*log(r+1)
? ? ? ? 第一步,初始化一個大小為256的int數組作為查找表,通過循環生成0~255個索引,對每個索引位置根據索引值進行對數變換計算;
? ? ? ? 第二步,對圖像進行操作,根據圖像每個像素取出RGB值后,以RGB值為索引,通過查找表找到RGB值對數變換對應的值,以該值賦值到對應像素上即可完成對數變換。
? ? ? ? 處理一張7592*5304大小的照片,直接使用對數變換對每個RGB值進行操作用時為8548毫秒,使用查找表用時為4657毫秒,圖像越大節省的時間越明顯。
? ? ? ? Demo如下:
/*** 對數變換* @author admin**/ public class LogTransform {private int[] table;//初始化對數變換查找表private int[] initLogTable() {table = new int[256];for (int i = 0; i < 256; i++) {table[i] = (int) Math.round(255 * Math.log(i + 1) / Math.log(256));}return table;}/*** 對數變換* * @param image* @return*/public BufferedImage logTransform(BufferedImage image) {BufferedImage resultImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());initLogTable();for (int i = 0; i < image.getWidth(); i++) {for (int j = 0; j < image.getHeight(); j++) {int rgb = image.getRGB(i, j);int R = (rgb >> 16) & 0xff;int G = (rgb >> 8) & 0xff;int B = rgb & 0xff;rgb = (255 & 0xff) << 24 | (clamp(table[R]) & 0xff) << 16 | (clamp(table[G]) & 0xff) << 8| (clamp(table[B]) & 0xff);resultImage.setRGB(i, j, rgb);}}return resultImage;}// 判斷r,g,b值,大于256返回256,小于0則返回0,0到256之間則直接返回原始值private int clamp(int rgb) {if (rgb > 255)return 255;if (rgb < 0)return 0;return rgb;}public static void main(String[] args) throws Exception{File input = new File("C:/Users/admin/Desktop/1.jpg");File output = new File("C:/Users/admin/Desktop/3.jpg");BufferedImage image = ImageIO.read(input);long time1 = System.currentTimeMillis();BufferedImage result = new LogTransform().logTransform(image);long time2 = System.currentTimeMillis();System.out.println(time2 - time1);ImageIO.write(result, "jpg", output);} }? ? ? ??
總結
以上是生活随笔為你收集整理的java图像处理之查找表实现图像处理加速的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java图像处理之图像裁剪
- 下一篇: java图像处理之实现任意角度图像旋转