本篇博客介绍使用两种方式来实现将透明PNG图片填充为白色背景。
第一种方法:
代码:
#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/highgui/highgui.hpp>using namespace cv;int i, j;Mat overlayImage(const Mat &background, const Mat &foreground){Mat output;background.copyTo(output);for (int y = std::max(0, 0); y < background.rows; ++y){int fY = y - 0; if (fY >= foreground.rows)break;for (int x = std::max(0, 0); x < background.cols; ++x){int fX = x - 0; if (fX >= foreground.cols)break;double opacity =((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3])/ 255.;for (int c = 0; opacity > 0 && c < output.channels(); ++c){uns