OpenCV 4.1.2-从网络摄像头获取帧并将其分割

问题描述 投票:0回答:1

我想从3D网络摄像头中抓取一个框架。网络摄像头可捕获左右图片,并在网络摄像头软件上并排显示。为了获得我的机器人的深度图片,我必须将图片分成两张。但是裁切图像时出现错误:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.1.2) /home/pi/opencv/opencv-4.1.2/modules/core/src/matrix.cpp:466: error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function 'Mat'

这里是我尝试的一些代码:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdint.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <dlfcn.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/time.h>

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;

typedef unsigned char           U1;     /* UBYTE   */
typedef int16_t                 S2;     /* INT     */
typedef uint16_t                U2;     /* UINT    */
typedef int32_t                 S4;     /* LONGINT */

typedef long long               S8;     /* 64 bit long */
typedef double                  F8;     /* DOUBLE */


int main ()
{
    S2 deviceId = 0;
    S2 retry = 0, drop;

    // Get a handle to the Video device:
    VideoCapture cap (deviceId);
    usleep (5000);

    open_cams:
    // Check if we can use this device at all:
    if(!cap.isOpened()) {
        cerr << "Capture Device ID " << deviceId << " cannot be opened." << endl;
        retry--;
    }

    if (retry < 6)
    {
        if (retry != 0)
        {
            usleep (4000);
            goto open_cams;
        }
    }



    Mat cam_left_out;
    Mat cam_right_out;
    Mat cam_out;

    for (;;)
    {
        // drop frames to get real time frames, was 10
        // for (drop = 5; drop > 1; drop--)
        for (;;)
        {
            for (drop = 5; drop > 1; drop--)
            {
                cap >> cam_out;
            }   

            cap >> cam_out;

            if (! cam_out.empty()) break;
        }

        Rect left_roi (0, 0, 640, 480);
        Rect right_roi (640, 0, 640, 480);

        Mat crop_left (cam_out, left_roi);
        Mat crop_right (cam_out, right_roi);

        crop_left.copyTo (cam_left_out);
        crop_right.copyTo (cam_right_out);

        imshow("cam", cam_out);
        imshow("cam-left", cam_left_out);
        imshow("cam-right", cam_right_out);
    }
}

这怎么了?从我的网络摄像头抓取的帧的尺寸应为1280x480!

c++ opencv webcam
1个回答
0
投票

似乎是顺序错误。我敢打赌,您的图片的形状为(480,1280,3),所以不要这样:

    Rect left_roi (0, 0, 640, 480);
    Rect right_roi (640, 0, 640, 480);

您应该使用此:

    Rect left_roi (0, 0, 480, 640);
    Rect right_roi (0, 640, 480, 640);

或者,更好的是,完全防弹的解决方案,而不是对数字进行明确的硬编码,而是使用cam_out.shape来计算图片的大小,并使用cam_out.shape[1]/2或类似的东西代替硬编码的数字。

© www.soinside.com 2019 - 2024. All rights reserved.