【问题标题】:OpenCV 4.1.2 - get frame from webcam and split itOpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
【发布时间】:2020-02-17 04:37:00
【问题描述】:

我想从我的 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】:

    似乎顺序错误。我敢打赌你的图像的形状是 (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 或类似的东西而不是硬编码的数字.

    【讨论】:

    • 感谢您的回答。我试过了,仍然得到同样的错误信息。我认为从 3D 网络摄像头获取图像有问题。我试图抓取图片并让它通过 imshow() 显示,但我没有得到图片!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多