【发布时间】:2014-03-07 18:31:22
【问题描述】:
在 OpenCV 中——使用 USB 网络摄像头初始化 VideoCapture 对象时——每运行 2 或 3 次,摄像头将无法初始化。我有一段非常难看的代码来解决这个问题,但也许有人知道什么会导致相机初始化失败以及如何防止它?
// Initialize video capture
camera_ = cv::VideoCapture(1);
camera_.set(CV_CAP_PROP_CONVERT_RGB , false);
camera_ >> frame_full_;
while (frame_full_.empty()){ // Could be !cap.isOpened
cerr << "Camera failure." << endl;
camera_.release();
camera_ = cv::VideoCapture(1);
camera_.set(CV_CAP_PROP_CONVERT_RGB , false);
camera_ >> frame_full_;
}
/// Do something with the camera feed.
当我关闭我的程序时,我确实在控制台中收到了Cleaned up camera.,所以我认为它已正确发布。
添加信息:在相机无法初始化的运行中,对>>/.read() 的调用需要很长时间才能返回。
编辑:正如@4nonymou5 所建议的那样,使用>> 运算符和.read() 之间没有区别。
【问题讨论】: