opencv3/C++视频中叠加透明图片的实现
时间:2021-06-18 08:46:29|栏目:C代码|点击: 次
如下所示:
大致思路:获取摄像头画面后,通过在图像上循环叠加一组背景透明的PNG图片,在图像上产生类似GIF的动图效果。
#include<opencv2/opencv.hpp> using namespace cv; //PNG透明图片叠加到视频 int main() { Mat frame; VideoCapture capture; capture.open(0); if(!capture.isOpened()) { printf("can not open the camear......\n"); return -1; } namedWindow("mask", CV_WINDOW_AUTOSIZE); namedWindow("output", CV_WINDOW_AUTOSIZE); while (capture.read(frame)) { //翻转摄像头图像 flip(frame, frame, 1); Mat image = frame; for (int i = 1; i < 11; i++) { char filename[100]; sprintf(filename,"E:/image/image/00/%d.PNG",i); //灰度图 Mat grayPng = imread(filename,0); threshold(grayPng,grayPng,180,255,CV_THRESH_BINARY); //掩模反色 Mat mask = 255 - grayPng; imshow("mask",mask); //读取背景透明的PNG图像 Mat png = imread(filename); Mat imROI; imROI = image(Rect(200,20,png.cols,png.rows)); png.copyTo(imROI,mask); imshow("output",image); //替换新图 image = frame; waitKey(10); } } waitKey(); return 0; }
导入的PNG图片为: