本文介绍了如何在opencv中通过实时相机馈送绘制透明图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在实时相机Feed上绘制透明图像。以下是要在相机Feed上叠加显示的png文件。

I need to draw a transparent image over the live camera feed. The below is the png file to be shown as overlay over the camera feed.

以下是从相机获取帧并在屏幕上显示的代码。我也试图画圆圈作为重叠,但圆圈不透明。

The below is the piece of code to fetch the frames from camera and show it on screen. I also tried to draw the circle as overlay, but the circle is not transparent. I think am wrong or missing out something in the below piece of code?

#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通道(假设图像大小相同),你可以这样做

If your overlay image has an alpha channel (and assuming the images are of the same size) you can do something like that

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));
        }
    }
}

这篇关于如何在opencv中通过实时相机馈送绘制透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:32
查看更多