zoukankan      html  css  js  c++  java
  • Java图片的灰度处理方法

    通过看网上各种大牛的总结,和自己亲身测试总结一下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;
    	}
    }
    

      

  • 相关阅读:
    Sublime Text3下使用Python,REPL的安装与快捷键设置方法
    2018最新版本Sublime Text3注册码(仅供测试交流使用)
    Simple website approach using a Headless CMS: Part 3
    Simple website approach using a Headless CMS: Part 2
    Simple Website Approach Using a Headless CMS: Part 1
    Top 19 Headless CMS for Modern Publishers
    Headless CMS
    12位至今仍在世的重要工程师,让我们来认识这些程序界的大前辈
    Linux操作系统(第二版)(RHEL 8/CentOS 8)—内容简介—前言—清华大学出版社—张同光
    List of open source real-time operating systems
  • 原文地址:https://www.cnblogs.com/deng-c-q/p/8710252.html
Copyright © 2011-2022 走看看