【问题标题】:Overlay Image in OpenCVOpenCV中的叠加图像
【发布时间】:2013-08-13 01:22:49
【问题描述】:

我正在尝试使用 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;
}

任何建议或帮助将不胜感激。谢谢。

【问题讨论】:

    标签: c++ opencv image-processing


    【解决方案1】:

    您使用未转换的图像将图像转换为 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;
    }
    

    【讨论】:

    • 其实我之前也是这样的,但是没用。我正在以另一种方式对其进行测试,却忘记将其改回。我只是将示例代码中的它改回 srcBGR.copyTo(dstROI)
    • 好的,我已经编辑了我的答案,我已经测试了这段代码并且它可以工作。
    • 谢谢。问题是我没有根据目标大小重新调整图像大小。再次感谢。
    • 优雅的解决方案。带有 alpha 通道的图像呢? png?
    • 分割图像,提取 alpha,转换为浮点和 0-1 范围元素,然后使用这些系数混合图像: Vec3f r = v1f*blending_coeff + (1.0 - blending_coeff)*v2f;其中 r - 结果像素,v1f - 要混合的一个图像的像素,v2f - 要混合的第二个图像的像素。可以逐个像素或使用矩阵 mul 和 + 运算符来完成。
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多