c++ - OpenCV 中的叠加图像

标签 c++ opencv image-processing

我正在尝试使用 OpenCV 在我的程序的视频捕获上覆盖图像,但我无法让它工作。我以矩形的形式在从网络摄像头拍摄的原始帧中设置了一个感兴趣区域。然后我将它复制到原始框架。但是,它从未出现在网络摄像头捕获的新帧中。我测试了它并且图像加载正确但由于某种原因它没有被复制到新框架。

C++ 代码如下:

#include<opencv2/core/core.hpp>
#include<opencv2/contrib/contrib.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<vector>

using namespace std;
using namespace cv;

int main(){
    VideoCapture cap;
    cap.open(0);

    if(!cap.isOpened()){
        cerr << "Error opening the webcam!" << endl;
        return -1;
    }


    for(;;){
        Mat frame;

        cap>>frame; 

        Mat newFrame;
        frame.copyTo(newFrame);

        Mat image = imread("C:\\User\\Desktop\\images\\image.png");


    int cx = (newFrame.cols - 70) / 2;
        if (image.data) {
            // Get a BGR version of the face, since the output is BGR color
            Mat srcBGR = Mat(face.size(), CV_8UC3);
            cvtColor(image, srcBGR, CV_GRAY2BGR);
            // Get the destination ROI (and make sure it is within the image)
            Rect dstRC = Rect(cx, newFrame.rows/2, 70, 70);
            Mat dstROI = newFrame(dstRC);
            // Copy the pixels from src to dst.
            srcBGR.copyTo(dstROI);
        }


        imshow("frame", newFrame);
        char key = (char) waitKey(30);
        // Exit this loop on escape:
        if(key == 27)
            break;
    }   

    return 0;
}

如有任何建议或帮助,我们将不胜感激。谢谢。

最佳答案

您使用未转换的图像将图像转换为 BGR: 改变这个:image.copyTo(dstROI);到这个 srcBGR.copyTo(dstROI);

#include<opencv2/core/core.hpp>
#include<opencv2/contrib/contrib.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<vector>

using namespace std;
using namespace cv;

int main(){
    VideoCapture cap;
    cap.open(0);

    if(!cap.isOpened()){
        cerr << "Error opening the webcam!" << endl;
        return -1;
    }
    Mat image = imread("D:\\ImagesForTest\\Lena.jpg",0);
    cv::resize(image,image,Size(70,70));  
    Mat frame;
    for(;;){
        cap>>frame; 
        Mat newFrame=frame.clone();
        int cx = (newFrame.cols - 70) / 2;
        if (!image.empty()) {
            // Get a BGR version of the face, since the output is BGR color
            Mat srcBGR = Mat(image.size(), CV_8UC3);
            cvtColor(image, srcBGR, CV_GRAY2BGR);
            // Get the destination ROI (and make sure it is within the image)
            Rect dstRC = Rect(cx, newFrame.rows/2, 70, 70);
            Mat dstROI = newFrame(dstRC);
            // Copy the pixels from src to dst.
            srcBGR.copyTo(dstROI);
        }
        imshow("frame", newFrame);
        char key = (char) waitKey(30);
        // Exit this loop on escape:
        if(key == 27)
            break;
    }   

    return 0;
}

关于c++ - OpenCV 中的叠加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175012/

相关文章:

java - j图 : General Traversal & Forest Traversal

python - 使用开放式CV python对两个图像进行边缘检测和直方图匹配。

c++ - 专注于 C++ 中的两个祖先类

c++ - 将正则表达式模式定义为无符号字符

c++ - QFormLayout 扩展 QGridLayout 的列

将图像矩阵大小复制到 OpenCV 中的另一个矩阵

opencv - 如何在 opencv python 中将 rgb 图像转换为 ycrcb 颜色空间

python - 使用opencv写入后,图像中的标题信息丢失

C++ 生成器 : Refresh FireMonkey Visual Component

c# - 背景减法 EMGU CV 后保留一些背景