医学图像处理:绘制直方图(Java)

图像处理:绘制直方图。
效果:
【输出前:】
在这里插入图片描述【输出后:】
在这里插入图片描述
来看看代码怎么写:
【上代码】

import  java.awt.Color;
import  java.awt.Graphics2D;
import  java.awt.image.BufferedImage;
import  java.io.File;
import  java.io.IOException;

import  javax.imageio.ImageIO;

public  class  Zft  {

        public  static  void  main(String[]  args)  throws  Exception  {
                //  TODO  Auto-generated  method  stub
                BufferedImage  bi=ImageIO.read(new  File("1.jpg"));        
                BufferedImage  nbi=getZftImage(bi,0);
                ImageIO.write(nbi,  "jpg",  new  File("1b.jpg"));
        }

        public  static  BufferedImage  getZftImage(BufferedImage  bi,int  type)  {
                //  TODO  Auto-generated  method  stub
                int[]  zft=getZftSz(bi,type);
                return  drawZft(zft,type);
        }
        
        /**
          *  根据BufferedImage  bi图像,得到对应各灰阶的像素个数的数组
          *  @param        BufferedImage  bi        输入图像
          *  @param        int  type                        要得到的通道类型。0灰度;1红色;2绿色;3蓝色
          *  @return        int[]                                返回0-255各灰阶像素的个数
          *  */
        public  static  int[]  getZftSz(BufferedImage  bi,int  type)  {
                int  w=bi.getWidth();
                int  h=bi.getHeight();
                
                int[]  zft=new  int[256];
//                for(int  i=0;i<256;i++){
//                        zft[i]=0;
//                        System.out.println(i+":"+zft[i]);
//                }
                
                //统计各灰阶像素的个数
                for(int  y=0;y<h;y++){
                        for(int  x=0;x<w;x++){ 
                        int pixel=bi.getRGB(x, y);
                         Color c=new Color(pixel);
				                                
                         int gray=0;
				         switch (type){
				                                case 0:
				                                	gray=(int)   (0.3*c.getRed()+0.59*c.getGreen()+0.11*c.getBlue());
				                                	break;
				                                case 1:
				                                	gray=c.getRed();
                                                                        break;
				                                case 2:
				                                	gray=c.getGreen();
				                                	break;
				                                case 3:
				                                	gray=c.getBlue();
				                                	break;
				                                default:
				                                	break;
				                                }
				                                
				                                zft[gray]++;
				                                                        }
                }
                //查看统计结果是否正确
//                for(int  i=0;i<256;i++){
//                        System.out.println(i+":"+zft[i]);
//                }
                return  zft;
        }
        
        /**
          *  根据各灰阶像素个数,绘制直方图
          *  @param        int[]  zft        0-255各灰阶像素的个数的数组
          *  @param        int  type        绘制的颜色。0黑色;1红色;2绿色;3蓝色
          *  @return        BufferedImage        返回直方图的图像
          *  */
        public  static  BufferedImage  drawZft(int[]  zft,int  type)  {
                int  width=256;
                int  height=100; 
                 BufferedImage nbi=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
		                Graphics2D g2=(Graphics2D)nbi.getGraphics();
		                g2.setBackground(Color.WHITE);
		                g2.clearRect(0, 0, width, height);
		                g2.setPaint(Color.BLACK);
		                
		                switch(type){
		                case 1:
		                	g2.setPaint(Color.RED);
		                	break;
		                case 2:
		                	g2.setPaint(Color.GREEN);
		                	break;
		                case 3:
		                	g2.setPaint(Color.BLUE);
		                	break;
		                default:
		                	break;
		                }
		                
		                int max=1;
		                for(int i=0;i<256;i++){
		                	if(max<zft[i]){
		                		max=zft[i];
		                	}
		                }
		                
		                for(int i=0;i<width;i++){
		                	int y2=height-zft[i]*height/max;
		                	g2.drawLine(i, 100, i, y2);
		                }
		                

      return  nbi;                                
        }

} 

				
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值