通過看網上各種大牛的總結,和自己親身測試總結一下Java圖片的灰度處理方法
(1)我們熟知的圖片中的像素點有RGB值。
(2)圖片灰度化的方式大概分為四種,第一種是最大值法(取顏色RGB中的最大值作為灰度值);第二種是最小值法(取顏色RGB的最小值作為灰度值);第三種是均值法(取顏色的RGB的平均值作為灰度值);第四種是加權法灰度化(怎么加權最合適,效果最好,百度百科說的很全面)。
(3)廢話不多說,記錄一下我按照上述四種方法實現的效果和代碼:
原圖
按照上述四種方式分別灰度化后的效果如下面四圖
(4)實例代碼如下
package testhuidu; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class TestHUidu { /** * 顏色分量轉換為RGB值 * @param alpha * @param red * @param green * @param blue * @return */ private static int colorToRGB(int alpha, int red, int green, int blue) { int newPixel = 0; newPixel += alpha; newPixel = newPixel << 8; newPixel += red; newPixel = newPixel << 8; newPixel += green; newPixel = newPixel << 8; newPixel += blue; return newPixel; } public static void main(String[] args) throws IOException { grayImage(1,"ff.jpg", "1.jpg");//最大值法灰度化 grayImage(2,"ff.jpg", "2.jpg");//最小值法灰度化 grayImage(3,"ff.jpg", "3.jpg");//平均值法灰度化 grayImage(4,"ff.jpg", "4.jpg");//加權法灰度化 } /** * 圖片灰度化的方法 * @param status 灰度化方法的種類,1表示最大值法,2表示最小值法,3表示均值法,4加權法 * @param imagePath 需要灰度化的圖片的位置 * @param outPath 灰度化處理后生成的新的灰度圖片的存放的位置 * @throws IOException */ public static void grayImage(int status,String imagePath, String outPath) throws IOException { File file = new File(imagePath); BufferedImage image = ImageIO.read(file); int width = image.getWidth(); int height = image.getHeight(); BufferedImage grayImage = new BufferedImage(width, height, image.getType()); //BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int color = image.getRGB(i, j); final int r = (color >> 16) & 0xff; final int g = (color >> 8) & 0xff; final int b = color & 0xff; int gray=0; if(status==1){ gray=getBigger(r, g, b);//最大值法灰度化 }else if(status==2){ gray=getSmall(r, g, b);//最小值法灰度化 }else if(status==3){ gray=getAvg(r, g, b);//均值法灰度化 }else if(status==4){ gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);//加權法灰度化 } System.out.println("像素坐標:" + " x=" + i + " y=" + j + " 灰度值=" + gray); grayImage.setRGB(i, j, colorToRGB(0, gray, gray, gray)); } } File newFile = new File(outPath); ImageIO.write(grayImage, "jpg", newFile); } //比較三個數的大小 public static int getBigger(int x,int y,int z){ if(x>=y&&x>=z){ return x; }else if(y>=x&&y>=z){ return y; }else if(z>=x&&z>=y){ return z; }else{ return 0; } } //比較三個是的大小取最小數 public static int getSmall(int x,int y,int z){ if(x<=y&&x<=z){ return x; }else if(y>=x&&y>=z){ return y; }else if(z>=x&&z>=y){ return z; }else{ return 0; } } //均值法 public static int getAvg(int x,int y,int z){ int avg=(x+y+z)/3; return avg; } }