在OpenCV中将二维码投影到摄像头时,出现错误

Posted

技术标签:

【中文标题】在OpenCV中将二维码投影到摄像头时,出现错误【英文标题】:When I project the QR code to the camera in OpenCV, an error occurs 【发布时间】:2021-10-04 15:41:57 【问题描述】:

我想生成一个代码,以便在 QR 码指向相机时播放视频。 但我在 warpPerspective 中遇到错误。 错误是:OpenCV(4.5.3) 错误:断言失败 (_src.total() > 0) in cv::warpPerspective, 文件 C:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc \src \imgwarp.cpp,第 3144 行 找了很多地方都找不到答案,所以来问一下。

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void main() 

    Mat cameraFrame, videoFrame;
    vector<Point2f> bbox;
    double width, height;
    VideoCapture camera(0), video;
    QRCodeDetector qrDecoder;
    string data;
    
    while (camera.read(cameraFrame))
    
        data = qrDecoder.detectAndDecode(cameraFrame, bbox);

        if (data.length() > 0)
        
            if (!video.isOpened()) 
                video.open("../data/"+data+".mp4");
                width = video.get(CAP_PROP_FRAME_WIDTH);
                height = video.get(CAP_PROP_FRAME_HEIGHT);
            
            
            vector<Point2f> v_bbox Point2f(0, 0), Point2f(width,0), Point2f(width,height), Point2f(0, height) ;
            Mat H = findHomography(v_bbox, bbox);
            video >> videoFrame;
            Mat warpedFrame, warpedMask;

            warpedFrame.copyTo(cameraFrame, warpedMask);

            warpPerspective(videoFrame, warpedFrame, H, cameraFrame.size());
            Mat mask(videoFrame.size(), CV_8UC1, cv::Scalar(255));
            
            warpPerspective(mask, warpedMask, H, cameraFrame.size());
            imshow("warepedFrame", warpedFrame);
            imshow("WarpedMask", warpedMask);
        
        imshow("cam", cameraFrame);
        waitKey(1);
    

【问题讨论】:

【参考方案1】:

问题是您在 warpedFrame 被重置 cameraFrame 填充 warpPerspective 之前调用了 copyTo 方法mat,然后当您将 cameraFrame.size() 传递给 warpPerspective 并使用 cameraFrame.size() 构造掩码垫,您基本上传递的大小为 (0,0)

您所要做的就是在应用透视变换后复制包装的视频叠加层:

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void main() 

    Mat cameraFrame, videoFrame;
    vector<Point2f> bbox;
    double width, height;
    VideoCapture camera(0), video;
    QRCodeDetector qrDecoder;
    string data;
    
    while (camera.read(cameraFrame))
    
        data = qrDecoder.detectAndDecode(cameraFrame, bbox);

        if (data.length() > 0)
        
            if (!video.isOpened()) 
                video.open("../data/"+data+".mp4");
                width = video.get(CAP_PROP_FRAME_WIDTH);
                height = video.get(CAP_PROP_FRAME_HEIGHT);
            
            
            vector<Point2f> v_bbox Point2f(0, 0), Point2f(width,0), Point2f(width,height), Point2f(0, height) ;
            Mat H = findHomography(v_bbox, bbox);
            video >> videoFrame;
            Mat warpedFrame, warpedMask;
    
            warpPerspective(videoFrame, warpedFrame, H, cameraFrame.size());
            Mat mask(videoFrame.size(), CV_8UC1, cv::Scalar(255));
            
            warpPerspective(mask, warpedMask, H, cameraFrame.size());

            imshow("warepedFrame", warpedFrame);
            imshow("WarpedMask", warpedMask);

            warpedFrame.copyTo(cameraFrame, warpedMask);

        
        imshow("cam", cameraFrame);
        waitKey(1);
    

【讨论】:

以上是关于在OpenCV中将二维码投影到摄像头时,出现错误的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV---直方图反向投影

摄像机模型

结合opencv,在arm开发板上运行打开摄像头test文件,出现错误。。如下图:

OpenCV相机标定及距离估计(单目)

透视投影详解转

用于 OpenCV 的 Iphone 6 相机校准