Guest User

ovelayImage JAVA API

a guest
Jul 30th, 2013
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1.     void overlayImage(Mat background,Mat foreground,Mat output,Point location){
  2.         Log.i("call","Calling OVerlay Image");
  3.         background.copyTo(output);
  4.         // start at the row indicated by location, or at row 0 if location.y is negative.
  5.         for(int y = (int) Math.max(location.y , 0); y < background.rows(); ++y){
  6.             int fY = (int) (y - location.y); // because of the translation
  7.             // we are done of we have processed all rows of the foreground image.
  8.             if(fY >= foreground.rows())
  9.                 break;
  10.             // start at the column indicated by location,
  11.             // or at column 0 if location.x is negative.
  12.             for(int x = (int) Math.max(location.x, 0); x < background.cols(); ++x){
  13.                 int fX = (int) (x - location.x); // because of the translation.
  14.                 // we are done with this row if the column is outside of the foreground image.
  15.                 if(fX >= foreground.cols()){
  16.                     break;
  17.                 }
  18.                 //determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
  19.                 /// here tried other type of arrays like short , int and float but same problem is there
  20.                 double[] opacity = new double[(int) (foreground.total()*foreground.channels())];
  21.                 /// Mat type not compatible error over here
  22.                 foreground.get(fY , fX, opacity);
  23.  
  24.                                     ///OR
  25.                 //double[] opacity = foreground.get(fY , fX, opacity);
  26.                 Log.i("Length", String(opacity.length));
  27.  
  28.                 /// assuming that opacity will be array of 4 values and
  29.                 /// opacity[3] will contain alpha channel values
  30.                 if ( opacity[3] > 0){
  31.                     double[] foregroundPx = foreground.get(fY, fX);
  32.                     double[] backgroundPx = background.get(fY, fX);
  33.                     double[] finalPixelValue = new double[3] ;
  34.                     finalPixelValue[0] = backgroundPx[0] * ( 1.0 - opacity[3]) + foregroundPx[0] * opacity[3];
  35.                     finalPixelValue[1] = backgroundPx[1] * ( 1.0 - opacity[3]) + foregroundPx[1] * opacity[3];
  36.                     finalPixelValue[2] = backgroundPx[2] * ( 1.0 - opacity[3]) + foregroundPx[2] * opacity[3];
  37.                     output.put(fY, fX,finalPixelValue);
  38.                 }
  39.             }
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment