灰度图像的直方图

直方图:直方图的作用是通过研究质量波动状况之后,就能掌握过程的状况,从而确定在什么地方集中力量进行质量改进工作。

下面是获取一副灰度图像的直方图,并将其显示出来


灰度图的直方图案例:

main.xml在此处省略,跟前文一样的。


activity:

public class ImageJAndroidActivity extends Activity {
    ImageView sourceImage;
    ImageView destinationImage;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sourceImage=(ImageView) findViewById(R.id.source);
        destinationImage=(ImageView) findViewById(R.id.destination);
       
       
    }
    
    //显示直方图
    public void remove(View v){
         Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.gg);    
         int width=bitmap.getWidth();
         int height=bitmap.getHeight();
         int[] pixel=new int[width*height];
         bitmap.getPixels(pixel, 0, width, 0, 0, width, height);
         //将彩色图灰度化
         for(int y=0;y<height;y++){
             for(int x=0;x<width;x++){
                 int a=pixel[y*width+x]>>24&0xff;
                 int r=pixel[y*width+x]>>16&0xff;
                 int g=pixel[y*width+x]>>8&0xff;
                 int b=pixel[y*width+x]&0xff;
                 
                 int grey=(int)(0.299 * r + 0.587 * g + 0.114 * b);
                 pixel[y*width+x]=(a<<24)|grey<<16|grey<<8|grey;
             }
         }
         int[] level=new int[256];
         for(int y=0;y<height;y++){
             for(int x=0;x<width;x++){
                 int a=pixel[y*width+x]&0xff;
                 level[a]=level[a]+1;
             }
         }
        
        
         //显示直方图
         int[] histogram=new int[256*256];
         //直方图的背景是白色
         for(int i=0;i<histogram.length;i++){
             histogram[i]=Color.WHITE;
         }
        
         for(int x=0;x<256;x++){
             for(int y=255;y>=255-level[x]/10;y--){
                 histogram[y*256+x]=Color.BLACK;
             }
         }
         Bitmap bim=Bitmap.createBitmap(256, 256, Config.RGB_565);
         bim.setPixels(histogram, 0, 256, 0, 0, 256, 256);
         destinationImage.setImageBitmap(bim);
    }



RGB彩色图的直方图案例:

彩色图像的直方图通常指的是图像强度(亮度)直方图或者单个颜色通道的直方图

activity:

//彩图直方图
    public void remove(View v){
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.gg);    
         int width=bitmap.getWidth();
           int height=bitmap.getHeight();
           int[] pixel=new int[width*height];
           bitmap.getPixels(pixel, 0, width, 0, 0, width, height);
           //表示红色通道的数组
           int[] red=new int[width*height];
           int index=0;
           for(int y=0;y<height;y++){
               for(int x=0;x<width;x++){
                   int r =pixel[y*width+x]>>16&0xff;
                   red[index]=r;
                   index++;
               }
           }
           int[] level=new int[256];
           for(int y=0;y<height;y++){
             for(int x=0;x<width;x++){
                 int a=pixel[y*width+x]&0xff;
                 level[a]=level[a]+1;
             }
        }
           //显示直方图
            int[] histogram=new int[256*256];
            //直方图的背景是白色
            for(int i=0;i<histogram.length;i++){
                histogram[i]=Color.WHITE;
            }
           
            for(int x=0;x<256;x++){
                for(int y=255;y>=255-level[x]/20;y--){
                    histogram[y*256+x]=Color.BLACK;
                }
            }
            Bitmap bim=Bitmap.createBitmap(256, 256, Config.RGB_565);
            bim.setPixels(histogram, 0, 256, 0, 0, 256, 256);
         destinationImage.setImageBitmap(bim);
    }

效果:(由于直方图的某一值有可能过大,所以将所有的值都缩小了20.直方图画的不是满标准。)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值