2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OpenCV(C++)で透過PNGを重ねる

Last updated at Posted at 2022-03-03

void copyTo (OutputArray m, InputArray mask) const を使って、透過PNGを重ねる方法

void putTranspPng(cv::Mat &_frame, cv::Mat &_png, int _x, int _y)
{
    vector<cv::Mat> layers;
    cv::split(_png, layers);
    // 下記の処理は_pngがRGBAの4チャンネルMatである必要がある
    if (4 != layers.size())
        cerr << "putTranspPng() invalid input" << endl;
    // 貼り付ける画像(3チャンネル)
    cv::Mat rgb;
    cv::merge(layers.data(), 3, rgb);
    // copyToに使うmask(1チャンネル)
    cv::Mat mask = layers[3];
    // copyToに使うroi
    cv::Mat roi = _frame(cv::Rect(_x, _y, rgb.cols, rgb.rows));
    rgb.copyTo(roi, mask);
}

// frameの真ん中に小さい画像imgを重ねる場合
cv::Mat img = cv::imread("test.png", -1);
int x = frame.cols/2 - img.cols/2;
int y = frame.rows/2 - img.rows/2;
putTranspPng(frame, img, x, y);
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?