数字图像处理--几种图像均值滤波的java实现
生活随笔
收集整理的這篇文章主要介紹了
数字图像处理--几种图像均值滤波的java实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? 在《數字圖像處理》一書中介紹了用于降低圖像噪聲的均值濾波器,分別有算數均值濾波器、幾何均值濾波器、諧波均值濾波器、逆諧波均值濾波器。除了降噪,均值濾波器也可以模糊圖像,濾波器大小為3、5、7...2n+1,濾波器越大計算量越大,產生的圖像越模糊。這里采用純java對幾種算法進行實現,代碼如下:
實現類:
import java.awt.image.BufferedImage;/*** 幾種均值濾波算法實現 1、算術均值濾波 2、幾何均值濾波 3、諧波均值濾波 4、逆諧波均值濾波* * @author admin*/ public class AverageFilter {// singletonprivate static AverageFilter averageFilter = new AverageFilter();public static AverageFilter getInstance() {return averageFilter;}private AverageFilter() {}/*** 算術均值濾波 濾波器大小:param取值3、5、7、9...(2n+1 ) 產生一幅模糊圖像* * @param image*/public void arithmeticAverageFilter(BufferedImage image, int param) {// 創建一個臨時圖像,為了保證原圖邊緣參與計算,臨時圖像比原圖大param-1BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,image.getType());// 對圖像進行填充,邊緣像素采用最近像素填充nearFillEdge(tempImage, image, param);// 進行卷積運算for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {int r = 0, g = 0, b = 0;// 計算濾波器內所有像素,R、G、B各個分量總和for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {int tempRGB = tempImage.getRGB(i + x, j + y);int tempR = (tempRGB >> 16) & 0xff;int tempG = (tempRGB >> 8) & 0xff;int tempB = tempRGB & 0xff;r += tempR;g += tempG;b += tempB;}}// 采用總和除以濾波器內總像素數量得到均值r = (int) (r / Math.pow(param, 2));g = (int) (g / Math.pow(param, 2));b = (int) (b / Math.pow(param, 2));int rgb = (255 & 0xff) << 24 | (clamp(r) & 0xff) << 16 | (clamp(g) & 0xff) << 8 | (clamp(b) & 0xff);image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);}}}/*** 幾何均值濾波* * @param image* @param param*/public void geometryAverageFilter(BufferedImage image, int param) {// 創建臨時圖像BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,image.getType());// 填充邊緣nearFillEdge(tempImage, image, param);// 進行卷積運算for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {double r = 1.0, g = 1.0, b = 1.0;for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {int tempRGB = tempImage.getRGB(i + x, j + y);double tempR = (tempRGB >> 16) & 0xff;double tempG = (tempRGB >> 8) & 0xff;double tempB = tempRGB & 0xff;r *= Math.pow(tempR + 1, 1.0 / (param * param));g *= Math.pow(tempG + 1, 1.0 / (param * param));b *= Math.pow(tempB + 1, 1.0 / (param * param));}}int rgb = (255 & 0xff) << 24 | (clamp((int) r) & 0xff) << 16 | (clamp((int) g) & 0xff) << 8| (clamp((int) b) & 0xff);image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);}}}/*** 諧波均值濾波* * @param image* @param param*/public void harmonicFilter(BufferedImage image, int param) {// 創建temp圖像BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,image.getType());// 填充邊緣nearFillEdge(tempImage, image, param);// 進行卷積運算for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {double r = 0, g = 0, b = 0;for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {int tempRGB = tempImage.getRGB(i + x, j + y);double tempR = (tempRGB >> 16) & 0xff;double tempG = (tempRGB >> 8) & 0xff;double tempB = tempRGB & 0xff;r += 1 / tempR;g += 1 / tempG;b += 1 / tempB;}}r = param * param / r;g = param * param / g;b = param * param / b;int rgb = (255 & 0xff) << 24 | (clamp((int) r) & 0xff) << 16 | (clamp((int) g) & 0xff) << 8| (clamp((int) b) & 0xff);image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);}}}/*** 逆諧波均值濾波* * @param image* @param param* @param Q* 當Q=0為算術均值濾波,Q=-1為諧波均值濾波;Q為正,消除胡椒噪聲,Q為負,消除鹽粒噪聲*/public void reverseHarmonicFilter(BufferedImage image, int param, int Q) {// 創建temp圖像BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,image.getType());// 填充邊緣nearFillEdge(tempImage, image, param);// 進行卷積運算for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {double r = 0, g = 0, b = 0, r1 = 0, g1 = 0, b1 = 0;for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {int tempRGB = tempImage.getRGB(i + x, j + y);double tempR = (tempRGB >> 16) & 0xff;double tempG = (tempRGB >> 8) & 0xff;double tempB = tempRGB & 0xff;r += Math.pow(tempR, Q + 1);g += Math.pow(tempG, Q + 1);b += Math.pow(tempB, Q + 1);r1 += Math.pow(tempR, Q);g1 += Math.pow(tempG, Q);b1 += Math.pow(tempB, Q);}}r = r / r1;g = g / g1;b = b / b1;int rgb = (255 & 0xff) << 24 | (clamp((int) r) & 0xff) << 16 | (clamp((int) g) & 0xff) << 8| (clamp((int) b) & 0xff);image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);}}}// 判斷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;}// 填充圖像邊緣空白像素,使用最近像素填充private void nearFillEdge(BufferedImage tempImage, BufferedImage image, int param) {for (int i = 0; i < tempImage.getWidth(); i++) {for (int j = 0; j < tempImage.getHeight(); j++) {// 臨時圖像位置沒超過原圖第一個位置,左下角if (i <= (param - 1) / 2 & j <= (param - 1) / 2) {int rgb = image.getRGB(0, 0);tempImage.setRGB(i, j, rgb);}// 臨時圖像位置超過橫坐標最大,小于縱坐標最小,右下角if (i >= tempImage.getWidth() - (param - 1) / 2 - 1 & j <= (param - 1) / 2) {int rgb = image.getRGB(image.getWidth() - 1, 0);tempImage.setRGB(i, j, rgb);}// 臨時圖像位置超過縱坐標最大,小于橫坐標最小,左上角if (j >= tempImage.getHeight() - (param - 1) / 2 - 1 & i <= (param - 1) / 2) {int rgb = image.getRGB(0, image.getHeight() - 1);tempImage.setRGB(i, j, rgb);}// 臨時圖像位置橫縱坐標都超過原圖最大位置,右上角if (i >= tempImage.getWidth() - (param - 1) / 2 - 1& j >= tempImage.getHeight() - (param - 1) / 2 - 1) {int rgb = image.getRGB(image.getWidth() - 1, image.getHeight() - 1);tempImage.setRGB(i, j, rgb);}// 臨時圖像位置橫坐標大于最小,小于最大,縱坐標小于最小,正下方if (i > (param - 1) / 2 & i < tempImage.getWidth() - (param - 1) / 2 - 1 & j <= (param - 1) / 2) {int rgb = image.getRGB(i - (param - 1) / 2, 0);tempImage.setRGB(i, j, rgb);}// 臨時圖像位置橫坐標小于最小,縱坐標大于最小,小于最大,左邊if (j > (param - 1) / 2 & j < tempImage.getHeight() - (param - 1) / 2 - 1 & i <= (param - 1) / 2) {int rgb = image.getRGB(0, j - (param - 1) / 2);tempImage.setRGB(i, j, rgb);}// 右邊if (j > (param - 1) / 2 & j < tempImage.getHeight() - (param - 1) / 2 - 1& i >= tempImage.getWidth() - (param - 1) / 2 - 1) {int rgb = image.getRGB(image.getWidth() - 1, j - (param - 1) / 2);tempImage.setRGB(i, j, rgb);}// 上方if (i > (param - 1) / 2 & i < tempImage.getWidth() - (param - 1) / 2 - 1& j >= tempImage.getHeight() - (param - 1) / 2 - 1) {int rgb = image.getRGB(i - (param - 1) / 2, image.getHeight() - 1);tempImage.setRGB(i, j, rgb);}// 中間if (i > (param - 1) / 2 & i < tempImage.getWidth() - (param - 1) / 2 - 1 & j > (param - 1) / 2& j < tempImage.getHeight() - (param - 1) / 2 - 1) {int rgb = image.getRGB(i - (param - 1) / 2, j - (param - 1) / 2);tempImage.setRGB(i, j, rgb);}}}} }測試類:
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("C:/桌面/AverageFilter/1.jpg");File output = new File("C:/桌面/AverageFilter/2.jpg");BufferedImage image = ImageIO.read(input);AverageFilter.getInstance().reverseHarmonicFilter(image, 3, 2);ImageIO.write(image, "jpg", output);} }?
總結
以上是生活随笔為你收集整理的数字图像处理--几种图像均值滤波的java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java图像处理之幂律变换
- 下一篇: java图像处理之拉普拉斯锐化和一阶微分