java图像处理之拉普拉斯锐化和一阶微分梯度锐化
生活随笔
收集整理的這篇文章主要介紹了
java图像处理之拉普拉斯锐化和一阶微分梯度锐化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?? 拉普拉斯是使用二階微分銳化圖像,以3*3濾波器中心像素與上下左右像素計算差值,計算公式為:
? ? 一階微分梯度銳化,以3*3濾波器中心像素上方三個像素之和減去下方三個像素之和的絕對值,與左邊三個像素減去右邊三個像素之和的絕對值,將兩個絕對值相加作為中心像素值,公式:
通過java代碼實現兩種銳化算法,代碼如下:
package ImageFilter;import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List;public class ImageSharpen {// singletonprivate static ImageSharpen imageSharpen = new ImageSharpen();private ImageSharpen() {}public static ImageSharpen getInstance() {return imageSharpen;}/*** 圖像銳化 二階微分銳化,拉普拉斯算子 定義一個3*3濾波器,計算中心像素與上下左右四個像素差值* * @param image*/public BufferedImage lapLaceSharpDeal(BufferedImage image) {BufferedImage tempImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());for (int i = 1; i < image.getWidth() - 1; i++) {for (int j = 1; j < image.getHeight() - 1; j++) {int rgb = image.getRGB(i, j);int rgb1 = image.getRGB(i - 1, j);int rgb2 = image.getRGB(i + 1, j);int rgb3 = image.getRGB(i, j - 1);int rgb4 = image.getRGB(i, j + 1);int[] R = new int[] { (rgb1 >> 16) & 0xff, (rgb2 >> 16) & 0xff, (rgb3 >> 16) & 0xff,(rgb4 >> 16) & 0xff, (rgb >> 16) & 0xff };int[] G = new int[] { (rgb1 >> 8) & 0xff, (rgb2 >> 8) & 0xff, (rgb3 >> 8) & 0xff, (rgb4 >> 8) & 0xff,(rgb >> 8) & 0xff };int[] B = new int[] { rgb1 & 0xff, rgb2 & 0xff, rgb3 & 0xff, rgb4 & 0xff, rgb & 0xff };double dR = R[0] + R[1] + R[2] + R[3] - 4 * R[4];double dG = G[0] + G[1] + G[2] + G[3] - 4 * G[4];double dB = B[0] + B[1] + B[2] + B[3] - 4 * B[4];double r = R[4] - dR;double g = G[4] - dG;double b = B[4] - dB;rgb = (255 & 0xff) << 24 | (clamp((int) r) & 0xff) << 16 | (clamp((int) g) & 0xff) << 8| (clamp((int) b) & 0xff);tempImage.setRGB(i, j, rgb);}}return tempImage;}/*** 一階微分梯度銳化*/public BufferedImage degreeSharpDeal(BufferedImage image) {BufferedImage tempImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());for (int i = 1; i < image.getWidth() - 1; i++) {for (int j = 1; j < image.getHeight() - 1; j++) {List<Integer> rList = new ArrayList<>();List<Integer> gList = new ArrayList<>();List<Integer> bList = new ArrayList<>();for (int x = -1; x < 2; x++) {for (int y = -1; y < 2; y++) {int rgb = image.getRGB(i + x, j + y);int R = (rgb >> 16) & 0xff;int G = (rgb >> 8) & 0xff;int B = rgb & 0xff;rList.add(R);gList.add(G);bList.add(B);}}int r = getResult(rList);int g = getResult(gList);int b = getResult(bList);r = rList.get(4) + r / 4;g = gList.get(4) + g / 4;b = bList.get(4) + b / 4;int rgb = (255 & 0xff) << 24 | (clamp(r) & 0xff) << 16 | (clamp(g) & 0xff) << 8 | (clamp(b) & 0xff);tempImage.setRGB(i, j, rgb);}}return tempImage;}// 執行一階微分計算private int getResult(List<Integer> list) {int result = Math.abs(list.get(0) + list.get(3) + list.get(6) - list.get(2) - list.get(5) - list.get(8))+ Math.abs(list.get(0) + list.get(1) + list.get(2) - list.get(6) - list.get(7) - list.get(8));return result;}// 判斷a,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;} }測試類:
package ImageFilter;import java.awt.image.BufferedImage; import java.io.File;import javax.imageio.ImageIO;public class test {public static void main(String[] args) throws Exception{File input = new File("E:/桌面/AverageFilter/1.jpg");File output = new File("E:/桌面/AverageFilter/3.jpg");BufferedImage image = ImageIO.read(input);BufferedImage image2 = ImageSharpen.getInstance().degreeSharpDeal(image);ImageIO.write(image2, "jpg", output);} }原圖:
拉普拉斯銳化結果:
一階微分梯度銳化結果:
原圖是一張比較模糊的圖像,對比兩種銳化方式,不難看出,拉普拉斯銳化效果明顯,但是容易加重噪點;一階微分梯度銳化對于圖像較平坦區域有所保留,對圖像邊緣銳化效果更加突出,因此在圖像分割和邊緣檢測方面會用到更多梯度處理。
總結
以上是生活随笔為你收集整理的java图像处理之拉普拉斯锐化和一阶微分梯度锐化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数字图像处理--几种图像均值滤波的jav
- 下一篇: java图像处理,拷贝图像EXIF信息