VideoFrames仅在按下esc,空格键或输入键时才会出现OpenCV c ++

时间:2017-06-14 14:21:31

标签: c++ opencv3.2

我是计算机视觉中的中间人,并且相当精通opencv python但是来到c ++我只是在从视频源中选择ROI并显示裁剪的Feed时遇到问题。我的代码看起来像这样。

#include "opencv2/highgui.hpp"
 #include "opencv2/imgproc.hpp"
 #include "opencv2/objdetect/objdetect.hpp"
  #include "opencv2/tracking.hpp"
#include "iostream"
using namespace cv;
using namespace std;
 int main() {

Mat frame1;
VideoCapture cap;
cap.open(0);
cap.read(frame1);
Rect2d roi = selectROI(frame1, true);
Mat Crop = frame1(roi);

while (1) {

    cap.read(frame1);
    Crop = frame1(roi);
    if (Crop.empty()) {
        cerr << "ERROR! blank frame grabbed\n";
        break;

    }
    imshow("roi", Crop);
    int key=waitkey(0);

}
}

代码正在编译,看到裁剪的窗口但是我总是需要点击enter,spacebar或esc来获取视频。很奇怪?

1 个答案:

答案 0 :(得分:1)

因此,更正后的代码的正确版本看起来会像这样。感谢您的帮助。

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/tracking.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main() {

Mat frame1;
VideoCapture cap;
cap.open(0);
cap.read(frame1);
 Rect2d roi = selectROI(frame1, true);
 Mat Crop = frame1(roi);

 while (1) {

cap.read(frame1);
Crop = frame1(roi);
if (Crop.empty()) {
    cerr << "ERROR! blank frame grabbed\n";
    break;

}
imshow("roi", Crop);
*int key=waitkey(1)*;

} }