0

我正在尝试使用该OpenCV Stitcher课程来拼接来自两个相机的帧。我只想以最简单的方式开始,然后进入 Stitcher 类的细节。

代码真的很简单,没有那么多代码行。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>

#include<iostream>
#include <fstream>
#include<conio.h>           // may have to modify this line if not using Windows

using namespace std;
using namespace cv;
//using namespace cv::detail;

bool try_use_gpu = true;
Stitcher::Mode mode = Stitcher::PANORAMA; //Stitcher::SCANS; 
vector<Mat> imgs;

int main()
{

    //initialize and allocate memory to load the video stream from camera 
    cv::VideoCapture camera0(2);
    cv::VideoCapture camera1(3);

    if (!camera0.isOpened()) return 1;
    if (!camera1.isOpened()) return 1;

    //Mat output_frame;
    cv::Mat3b output_frame;
    cv::Stitcher stitcher = cv::Stitcher::createDefault(true);
    //Ptr<Stitcher> stitcher = Stitcher::create(mode, try_use_gpu);
    Mat camera0_frame_resized;
    Mat camera1_frame_resized;

    while (true) {
        //grab and retrieve each frames of the video sequentially 
        cv::Mat3b camera0_frame;
        cv::Mat3b camera1_frame;
        camera0 >> camera0_frame;
        camera1 >> camera1_frame;

        resize(camera0_frame, camera0_frame_resized, Size(640, 480));
        resize(camera1_frame, camera1_frame_resized, Size(640, 480));

        imgs.push_back(camera0_frame_resized);
        imgs.push_back(camera1_frame_resized);
        Stitcher::Status status = stitcher.stitch(imgs, output_frame);
        output_frame.empty();
        if (status != Stitcher::OK) {
            cout << "Can't stitch images, error code = " << int(status) << endl;
            return -1;
        }

        cv::imshow("Camera0", camera0_frame);
        cv::imshow("Camera1", camera1_frame);

        cv::imshow("Stitch", output_frame);

        //wait for 40 milliseconds
        int c = cvWaitKey(5);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if (27 == char(c)) break;
    }

    return 0;
}

我正在使用OpenCV 3.2Visual Studio Community Edition 2017运行windows 10.

问题是它非常慢,它似乎缝合了第一帧,然后它有点卡住了,没有其他事情发生,几秒钟/分钟后可能会出现下一帧。

我在极快的 CPU 和顶级 GPU 上运行。我没想到快速缝合,但这只是放慢速度的方式,让我觉得我做错了什么。关于为什么它只缝合第一帧然后卡住的任何想法?

4

0 回答 0