Advertisement
Guest User

overlayImage ( png over jpg ) Completed

a guest
Jul 31st, 2013
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1.  void overlayImage(Mat background,Mat foreground,Mat output,Point location){
  2.         background.copyTo(output);
  3.         for(int y = (int) Math.max(location.y , 0); y < background.rows(); ++y){
  4.             int fY = (int) (y + location.y);
  5.             if(fY >= foreground.rows())
  6.                 break;
  7.             for(int x = (int) Math.max(location.x, 0); x < background.cols(); ++x){
  8.                 int fX = (int) (x + location.x);
  9.             if(fX >= foreground.cols()){
  10.                     break;
  11.                 }
  12.  
  13.                 double opacity;
  14.                 double[] finalPixelValue = new double[4];
  15.                 opacity = foreground.get(fY , fX)[3];
  16.  
  17.                 finalPixelValue[0] = background.get(fY, fX)[0];
  18.                 finalPixelValue[1] = background.get(fY, fX)[1];
  19.                 finalPixelValue[2] = background.get(fY, fX)[2];
  20.                 finalPixelValue[3] = background.get(fY, fX)[3];
  21.  
  22.                 for(int c = 0;  c < output.channels(); ++c){
  23.                     if(opacity > 0){
  24.                         double foregroundPx =  foreground.get(fY, fX)[c];
  25.                         double backgroundPx =  background.get(fY, fX)[c];
  26.  
  27.                         float fOpacity = (float) (opacity / 255);
  28.                         finalPixelValue[c] = ((backgroundPx * ( 1.0 - fOpacity)) + (foregroundPx * fOpacity));
  29.                         if(c==3){
  30.                             finalPixelValue[c] = foreground.get(fY,fX)[3];
  31.                         }
  32.                     }
  33.                 }
  34.                 output.put(fY, fX,finalPixelValue);
  35.             }
  36.         }
  37.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement