灰度图均衡化的效果

灰度图的均衡化的效果。

基本公式:f(a)=H[a].(K-1)/MN

H[a]表示累积直方图。

K表示亮度的取值数,这里是256

MN表示图像的像素总数


灰度图均衡化案例:

main.xml跟前文的一样。


activity:

public class ImageJAndroidActivity extends Activity {
    ImageView sourceImage;
    ImageView destinationImage;
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                sourceImage.setImageBitmap((Bitmap)msg.obj);
            }
        }
        
    };
    @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);
          //表示直方图  
          int[] level=new int[256];
          int index=0;
          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;
                     index++;
                }
          }   
           
             
         //累积直方图
           for(int i=1;i<level.length;i++){
                   level[i]=level[i-1]+level[i];
            }
               
         //均衡化图像
          for(int y=0;y<height;y++){
                  for(int x=0;x<width;x++){
                       int a=pixel[y*width+x]&0xff;
                       int b=level[a]*(256-1)/(width*height);
                       pixel[y*width+x]=(b<<24)|(b<<16)|(b<<8)|b;
                  }
           }
           Bitmap newBitmap=Bitmap.createBitmap(width, height,Config.RGB_565);
           newBitmap.setPixels(pixel, 0, width,0, 0, width, height);
           destinationImage.setImageBitmap(newBitmap);
    }
   


效果:





彩色图片的均衡化。必须将每个通道都采用这种方式均衡化。

由于图片的处理比较慢,所以我将图片的处理放在子线程中了。

public class ImageJAndroidActivity extends Activity {
    ImageView sourceImage;
    ImageView destinationImage;
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                destinationImage.setImageBitmap((Bitmap)msg.obj);
            }
        }
        
    };
    @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){
        new Thread(){
            public void run(){
                   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[][] pixels=new int[3][width*height];
                   int[] alas=new int[width*height];
                   for(int y=0;y<height;y++){
                       for(int x=0;x<width;x++){
                           //透明度通道不变
                           int a=pixel[y*width+x]>>24&0xff;
                           alas[y*width+x]=a;
                           //只需要对色彩通道进行均衡化
                           int red=pixel[y*width+x]>>16&0xff;
                           pixels[0][y*width+x]=red;
                           int green=pixel[y*width+x]>>8&0xff;
                           pixels[1][y*width+x]=green;
                           int blue=pixel[y*width+x]&0xff;
                           pixels[2][y*width+x]=blue;
                       }
                   }
                  pixels=equalization(pixels,width,height);
                  for(int i=0;i<width*height;i++){
                      pixel[i]=alas[i]<<24|pixels[0][i]<<16|pixels[1][i]<<8|pixels[2][i];
                  }
                  Bitmap newBitmap=Bitmap.createBitmap(width, height,Config.RGB_565);
                  newBitmap.setPixels(pixel, 0, width, 0, 0, width, height);
                  Message message=Message.obtain();
                  message.what=1;
                  message.obj=newBitmap;
                  handler.sendMessage(message);
            }    
        }.start();
        
          
    }
    
    public int[][] equalization(int[][] pixels,int width,int height){
        int K=256;
        int M=width*height;
        int[][] histogram=new int[3][256];;
        for(int i=0;i<pixels.length;i++){
            
            for(int j=0;j<pixels[i].length;j++){
                
                int a=pixels[i][j];
                histogram[i][a]=histogram[i][a]+1;
                
              
            }
        }

        //累积直方图
        for(int i=0;i<histogram.length;i++){
            for(int m=1;m<histogram[i].length;m++){
                histogram[i][m]=histogram[i][m-1]+histogram[i][m];
            }
        }
        
        for(int i=0;i<pixels.length;i++){
            for(int j=0;j<pixels[i].length;j++){
                //均衡化
                int b=pixels[i][j];
                int c=histogram[i][b]*(K-1)/M;
                pixels[i][j]=c;
            }
        }
        return pixels;
    }
   

得到的效果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值