从相机opencv c ++获取输入

时间:2017-06-15 20:55:02

标签: c++ opencv video input webcam

使用opencv显示图像时,我总是看到灰色屏幕,图像是从相机捕获的。有什么帮助吗?

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\imgcodecs.hpp>

using namespace cv;

cv::Mat takePicture() {
cv::Mat pic;
VideoCapture cam(0);
while (!cam.isOpened()) {
    std::cout << "Failed to make connection to cam" << std::endl;
    VideoCapture cam(0);
}
cam >> pic;
return pic;
}
int main()
{

cv::Mat pic1;

pic1 = takePicture();

imshow("camera", pic1);

}

1 个答案:

答案 0 :(得分:0)

相机需要时间进行初始化。 也许你应该改变你的代码:

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\imgcodecs.hpp>    
VideoCapture cam(0);
cv::Mat pic;
cv::Mat takePicture(){
    while (!cam.isOpened()) {
        std::cout << "Failed to make connection to cam" << std::endl;
        cam.open(0);
    }
    cam>>pic;
    return pic;
}
int main(){
    cv::waitKey(1000);
    cv::Mat pic1;
    pic1 = takePicture();
    imshow("camera", pic1);
    cv::waitKey();
}