Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在opencv中的实时摄影机提要上绘制透明图像_Opencv_Image Processing_Computer Vision - Fatal编程技术网

如何在opencv中的实时摄影机提要上绘制透明图像

如何在opencv中的实时摄影机提要上绘制透明图像,opencv,image-processing,computer-vision,Opencv,Image Processing,Computer Vision,我需要在实时摄像机上画一个透明的图像。下面是png文件,将显示为覆盖在摄影机提要上 下面是一段代码,用于从相机中获取帧并在屏幕上显示。我还尝试将圆绘制为覆盖,但圆不是透明的。我认为我错了,或者在下面的代码中遗漏了什么 #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> #include &l

我需要在实时摄像机上画一个透明的图像。下面是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;
 }
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(){
Mat-src;
Mat overlay=imread(“circle.png”,-1);
视频捕获上限(0);
而(1){
cap>>src;
CVT颜色(src、src、CV_BGR2BGRA);
overlay.copyTo(src.colRange(0400).rowRange(0400));
imshow(“src”,src);
等候室(10);
}
返回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));
        }
    }
}
cv::Mat display_img(src.size(),src.type());
对于(int y=0;y
我刚刚解决了完全相同的问题,我的解决方案是找出覆盖图中的哪些像素中有什么,然后将图像中的这些像素归零。现在您知道两幅图像中的一幅中的每个像素都是零,然后您可以将两幅图像相加

在python中:

import numpy as np
import cv2

# load the overlay file
overlay = cv2.imread('overlay.png')

# detect which pixels in the overlay have something in them
# and make a binary mask out of it
overlayMask = cv2.cvtColor( overlay, cv2.COLOR_BGR2GRAY )
res, overlayMask = cv2.threshold( overlayMask, 10, 1, cv2.THRESH_BINARY_INV)

# expand the mask from 1-channel to 3-channel
h,w = overlayMask.shape
overlayMask = np.repeat( overlayMask, 3).reshape( (h,w,3) )


# here's where the work gets done :

# mask out the pixels that you want to overlay
img *= overlayMask

# put the overlay on
img += overlay

# Show the image.
cv2.imshow(WINDOW_NAME, img)

嘿,那可不容易。为什么不直接画一个呢?确保圆的白色区域有一个合适的alpha通道(值255)。。另外,在
waitKey
@berak周围添加if&break语句,这只是一个例子。。我需要在cmara提要上绘制另一个图像,而不是椭圆。您当前的代码只是将一个图像替换为另一个图像,无论是否为alpha。opencv中没有任何操作,它尊重alpha,所以您必须即兴创作,比如将alpha通道的倒数作为add()操作的掩码。再说一次,这是一个计算机视觉库,不是为了在ppls脸上贴有趣的小胡子。@berak终于成功了