图像的点运算

来源:互联网 发布:linux如何安装rpm包 编辑:程序博客网 时间:2024/06/05 04:25

利用点运算进行两个图像的融合:

采用基本的公式:A(u,v)=a*B(u,v)+(1-a)*C(u,v);

0≤a≤1,当a=0时,C是不透明的因此C将B整个图像进行遮挡了,当a=1时,B是不透明的,因此B将整个图像进行遮挡了。


两个图像融合的案例:

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){
                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){
        Bitmap bitmap1=BitmapFactory.decodeResource(getResources(),R.drawable.gg);    
        Bitmap bitmap2=BitmapFactory.decodeResource(getResources(),R.drawable.jj);
        int width=bitmap1.getWidth();
        int height=bitmap1.getHeight();
        int[] pixel1=new int[width*height];
        int[] pixel2=new int[width*height];
        int[] newPixel=new int[width*height];
        bitmap1.getPixels(pixel1, 0, width, 0, 0, width, height);
        bitmap2.getPixels(pixel2, 0, width, 0, 0, width, height);
        for(int y=0;y<height;y++){
            for(int x=0;x<width;x++){
                int a=(pixel1[y*width+x]>>16&0xff);
                int red=(int)(0.7*(pixel1[y*width+x]>>16&0xff)+0.3*(pixel2[y*width+x]>>16&0xff));
                int green=(int)(0.7*(pixel1[y*width+x]>>8&0xff)+0.3*(pixel2[y*width+x]>>8&0xff));
                int blue=(int)(0.7*(pixel1[y*width+x]&0xff)+0.3*(pixel2[y*width+x]&0xff));
                newPixel[y*width+x]=a<<24|red<<16|green<<8|blue;
            }
        }
        Bitmap newBitmap=Bitmap.createBitmap(width, height, Config.RGB_565);
        newBitmap.setPixels(newPixel, 0, width, 0, 0, width, height);
        destinationImage.setImageBitmap(newBitmap);
    }


    两幅图片如下:

   


融合之后 的效果为:



两个图片进行合成:

原来的图片是:

现在要达到的效果是将上面图片的白色背景去掉(透明度为0)

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){
        Bitmap bitmap1=BitmapFactory.decodeResource(getResources(),R.drawable.gg);    
        Bitmap bitmap2=BitmapFactory.decodeResource(getResources(),R.drawable.s);
        int width1=bitmap1.getWidth();
        int height1=bitmap1.getHeight();
        int width2=bitmap2.getWidth();
        int height2=bitmap2.getHeight();
        int[] pixel1=new int[width1*height1];
        int[] pixel2=new int[width2*height2];
        bitmap1.getPixels(pixel1, 0, width1, 0, 0, width1, height1);
        bitmap2.getPixels(pixel2, 0, width2, 0, 0, width2, height2);
        
        for(int y=0;y<height2;y++){
            for(int x=0;x<width2;x++){
                //两个图片的像素进行融合
                if(pixel2[y*width2+x]!=Color.WHITE){
                    int a=(pixel2[y*width2+x]>>24&0xff);
                    int red=(int)(1*(pixel2[y*width2+x]>>16&0xff));
                    int green=(int)(1*(pixel2[y*width2+x]>>8&0xff));
                    int blue=(int)(1*(pixel2[y*width2+x]&0xff));
                    pixel1[(y+100)*width1+(x+20)]=a<<24|red<<16|green<<8|blue;//这里写成width1刚好换行的时候吻合
                }
            }
        }
        
        Bitmap newBitmap=Bitmap.createBitmap(width1, height1,Config.RGB_565);
        newBitmap.setPixels(pixel1, 0, width1,0 ,0, width1, height1);  
        destinationImage.setImageBitmap(newBitmap);
    }
   

最终达到的效果是:



0 0