Advertisement
jack06215

[OpenCV] overlay image

Jul 8th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <opencv2\core\core.hpp>
  2. #include <opencv2\highgui\highgui.hpp>
  3. #include <opencv2\imgproc\imgproc.hpp>
  4.  
  5. void overlayImage(const cv::Mat &background, const cv::Mat &foreground, cv::Mat &output, cv::Point2i location);
  6.  
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     // add the second parameter "-1" as flag, to make sure the transparancy channel is read!
  11.     cv::Mat foreground = cv::imread("bomb256.jpg");
  12.     cv::Mat background = cv::imread("fig1.jpg");
  13.     cv::Mat result;
  14.     cv::Mat output;
  15.  
  16.     // Input Quadilateral or Image plane coordinates
  17.     cv::Point2f inputQuad[4];
  18.     // Output Quadilateral or World plane coordinates
  19.     cv::Point2f outputQuad[4];
  20.  
  21.     cv::Mat lambda = cv::Mat::zeros(foreground.rows, foreground.cols, foreground.type());
  22.  
  23.     // The 4 points that select quadilateral on the input , from top-left in clockwise order
  24.     // These four pts are the sides of the rect box used as input
  25.     inputQuad[0] = cv::Point2f(-30, -60);
  26.     inputQuad[1] = cv::Point2f(foreground.cols + 50, -50);
  27.     inputQuad[2] = cv::Point2f(foreground.cols + 100, foreground.rows + 50);
  28.     inputQuad[3] = cv::Point2f(-50, foreground.rows + 50);
  29.     // The 4 points where the mapping is to be done , from top-left in clockwise order
  30.     outputQuad[0] = cv::Point2f(0, 0);
  31.     outputQuad[1] = cv::Point2f(foreground.cols - 1, 0);
  32.     outputQuad[2] = cv::Point2f(foreground.cols - 1, foreground.rows - 1);
  33.     outputQuad[3] = cv::Point2f(0, foreground.rows - 1);
  34.  
  35.     // Get the Perspective Transform Matrix i.e. lambda
  36.     lambda = getPerspectiveTransform(inputQuad, outputQuad);
  37.     // Apply the Perspective Transform just found to the src image
  38.     warpPerspective(foreground, output, lambda, output.size());
  39.  
  40.     overlayImage(background, output, result, cv::Point(50, 0));
  41.     cv::imshow("result", result);
  42.     cv::waitKey();
  43.  
  44.  
  45.     return 0;
  46. }
  47.  
  48. void overlayImage(const cv::Mat &background, const cv::Mat &foreground, cv::Mat &output, cv::Point2i location)
  49. {
  50.     background.copyTo(output);
  51.     // start at the row indicated by location, or at row 0 if location.y is negative.
  52.     for (int y = std::max(location.y, 0); y < background.rows; ++y)
  53.     {
  54.         int fY = y - location.y; // because of the translation
  55.  
  56.         // we are done of we have processed all rows of the foreground image.
  57.         if (fY >= foreground.rows) break;
  58.  
  59.         // start at the column indicated by location,
  60.         // or at column 0 if location.x is negative.
  61.         for (int x = std::max(location.x, 0); x < background.cols; ++x)
  62.         {
  63.             int fX = x - location.x; // because of the translation.
  64.  
  65.             // we are done with this row if the column is outside of the foreground image.
  66.             if (fX >= foreground.cols)
  67.                 break;
  68.  
  69.             // determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
  70.             double opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;
  71.             // and now combine the background and foreground pixel, using the opacity,
  72.             // but only if opacity > 0.
  73.             for (int c = 0; opacity > 0 && c < output.channels(); ++c)
  74.             {
  75.                 unsigned char foregroundPx = foreground.data[fY * foreground.step + fX * foreground.channels() + c];
  76.                 unsigned char backgroundPx = background.data[y * background.step + x * background.channels() + c];
  77.                 output.data[y * output.step + output.channels() * x + c] = backgroundPx * (1. - opacity) + foregroundPx * opacity;
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement