简体   繁体   中英

OpenCV 4.1.2 - get frame from webcam and split it

I want to grab a frame from my 3D webcam. The webcam grabs the left and right picture and on webcam software they are displayed side by side. In order to get a depth picture for my robot I have to split the picture up in two pictures. But I get a error in croping the image:

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'

Here is some code I tried:

#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);
    }
}

What is wrong with this? The frame grabbed from my webcam should be in 1280x480 in size!

Seems like the wrong order. I bet your image has the shape of (480, 1280, 3), so instead of this:

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

you should have used this:

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

or, better yet, a totally bulletproof solution, instead of explicitly hard-coding your numbers, use cam_out.shape to figure out the size of your picture, and use cam_out.shape[1]/2 or something like that instead of hard-coded numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM