OpenCV - 在OSX中无法捕获摄像头

时间:2017-01-03 17:44:03

标签: c++ macos opencv camera

为什么在OSX中我的代码根本没有显示任何相机捕获?

enter image description here

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main() {
  VideoCapture stream1(0);   //0 or 1 or 2 same..

  if (!stream1.isOpened()) {
    cout << "cannot open camera";
  }


  while (true) {
    Mat cameraFrame;
    stream1.read(cameraFrame);
    imshow("cam", cameraFrame);
    if (waitKey(30) >= 0)
      break;
  }
  return 0;
}

2 个答案:

答案 0 :(得分:1)

首先捕获的图像是否为空?你可以添加一个支票:

if(!cameraFrame.empty())
    imshow("cam", cameraFrame);

此外,请检查此问题和可能的解决方案:Assertion failed (size.width>0 && size.height>0)

答案 1 :(得分:0)

[已解决] 通过替换值

解决了问题

VideoCapture stream1(2);