简体   繁体   English

无法在网络摄像头视频 OpenCV C++ 上绘制矩形

[英]Unable to draw rectangle over webcam video OpenCV C++

Im trying to create a rectangle ROI over webcam video.我试图在网络摄像头视频上创建一个矩形 ROI。 But this code is crashing但是这段代码崩溃了

#include "opencv2/opencv.hpp"
#include <iostream>
#include <bits/stdc++.h> 
using namespace std;
using namespace cv;

int main(){
  VideoCapture cap(0); 
  if(!cap.isOpened()){
    cout << "Error opening video stream or file" << endl;
    return -1;
  }
Rect Roi(1,1,100,200);

 while(1){

    Mat frame;
    cap >> frame;
    frame.copyTo(frame(Roi));
    // If the frame is empty, break immediately
    if (frame.empty())
        break;
    // Display the resulting frame
    imshow( "Frame", frame );
    moveWindow("Frame",500,100);

    // Press  ESC on keyboard to exit
    char c=(char)waitKey(25);
    if(c==27)
      break;
  }
  cap.release();
  destroyAllWindows();
  return 0;
}

// g++ cam_roi.cpp pkg-config --cflags --libs opencv4 ; // g++ cam_roi.cpp pkg-config --cflags --libs opencv4 ; ./a.out ./a.out

ERROR ==> [ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (961) open OpenCV |错误 ==> [警告:0] 全局 ../modules/videoio/src/cap_gstreamer.cpp (961) 打开 OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1 terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(4.5.1) ../modules/core/src/matrix_wrap.cpp:1188: error: (-215:Assertion failed) !fixedSize() || GStreamer 警告:无法查询视频位置:状态=0,值=-1,持续时间=-1 在抛出“cv::Exception”what() 实例后调用终止:OpenCV(4.5.1) ../modules/core /src/matrix_wrap.cpp:1188: 错误: (-215:Assertion failed) !fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows) in function 'create' ((Mat*)obj)->size.operator()() == Size(_cols, _rows) 在函数“create”中

Aborted中止

the core issue is核心问题是

(-215:Assertion failed) !fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows) in function 'create'

(the gstreamer line is just a warning) (gstreamer 行只是一个警告)

this error comes from that line (it's not obvious but copyto involves a Mat::create call):此错误来自该行(这并不明显,但copyto涉及Mat::create调用):

    frame.copyTo(frame(Roi));

copyTo gets the destination Mat as the argument. copyTo获取目标 Mat 作为参数。 it can resize the destination in some circumstances.在某些情况下,它可以调整目的地的大小。

frame(Roi) is a "view" into frame , and views can't be resized, ie they are "fixed size". frame(Roi)是一个“视图”到frame ,和视图不能调整大小,即,它们是“固定的大小”。

you seem to want to take that subregion and put it back into frame.您似乎想要将该次区域带回框架中。

if that line of code worked, it would do the opposite, ie put the data from the whole frame into a subregion of itself...如果那行代码有效,它会做相反的事情,即将整个frame的数据放入其自身的一个子区域中......

simply do this:只需这样做:

frame = frame(Roi);

this will keep the view.这将保持视图。 you can use it as usual.你可以照常使用它。 the whole data of frame will live as long as the view. frame的整个数据将与视图一样长。

the view's data will not be contiguous, if that's a concern.如果担心的话,视图的数据将不会是连续的。

to "fix" that, you can create a copy that'll be contiguous:要“修复”该问题,您可以创建一个连续的副本:

frame = frame(Roi).clone();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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