0

我是 Computer Vison 的中级人员,并且对 opencv python 相当精通,但是在使用 c++ 时,我在仅从 Video feed 中选择 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、空格键或 esc 来获取视频源。奇怪吗?

4

1 回答 1

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)*;

} }

于 2017-06-14T14:52:13.807 回答