我需要在实时摄像头画面上绘制透明图像。下面是要显示为覆盖在相机源上的 png 文件。
下面是从相机获取帧并将其显示在屏幕上的代码。我也尝试将圆圈绘制为叠加层,但圆圈不透明。我认为下面的代码是错误的或遗漏了一些东西?
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main () {
Mat src;
Mat overlay = imread ( "circle.png", -1 );
VideoCapture cap ( 0 );
while ( 1 ) {
cap >> src;
cvtColor( src, src, CV_BGR2BGRA );
overlay.copyTo( src.colRange(0,400).rowRange(0,400));
imshow ( "src",src );
waitKey( 10 );
}
return 0;
}
如果你的叠加图像有一个 Alpha 通道(并且假设图像大小相同),你可以这样做
cv::Mat display_img( src.size(), src.type() );
for (int y = 0; y < src.rows; y++)
{
const cv::Vec3b* src_pixel = src.ptr<cv::Vec3b>(y);
const cv::Vec4b* ovl_pixel = overlay.ptr<cv::Vec4b>(y);
cv::Vec3b* dst_pixel = display_img.ptr<cv::Vec3b>(y);
for (int x = 0; x < src.cols; x++, ++src_pixel, ++ovl_pixel, ++dst_pixel)
{
double alpha = (*ovl_pixel).val[3] / 255.0;
for (int c = 0; c < 3; c++)
{
(*dst_pixel).val[c] = (uchar) ((*ovl_pixel).val[c] * alpha + (*src_pixel).val[c] * (1.0 -alpha));
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)