tg_522129 2022-12-01 16:02 采纳率: 0%
浏览 10

WIN7系统使用C++opencv4.5.5调用USB摄像头无法开启问题

WIN7系统使用C++opencv4.5.5调用USB摄像头,USB摄像头灯亮一下,但无法开启摄像头,程序在其它电脑上能正常运行,使用QQ、微信、amcap能正常打开摄像头。请各位指点,是什么原因不能开启摄像头,解决方法?

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

int main(int argc, char* argv[])
{
    //读取摄像头
    VideoCapture capture(0,CAP_DSHOW);

    // 设置参数
    capture.set(CAP_PROP_FRAME_WIDTH, 680);      // 宽度
    capture.set(CAP_PROP_FRAME_HEIGHT, 480);    // 高度
    capture.set(CAP_PROP_FPS, 30);
    capture.set(CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
    cout << "width:" << capture.get(CAP_PROP_FRAME_WIDTH) << endl;
    cout << "height:" << capture.get(CAP_PROP_FRAME_HEIGHT) << endl;

    //是否读取成功进行判断
    if(!capture.isOpened())
    {
        cout << "未打开摄像头!" << endl;
        return -1;
    }

    while(true)
    {
        Mat frame;
        capture>>frame;
        if(frame.empty())break;
        imshow("frame", frame);
        int key = waitKey(10);
        //cout << "key:" << key <<endl;
        if(key == 27)break;
    }
    return 0;
}

运行结果:
width:0
height:0
未打开摄像头!

Process returned -1 (0xFFFFFFFF) execution time : 0.457 s
Press any key to continue.

  • 写回答

3条回答 默认 最新

  • 爱晚乏客游 2022-12-01 16:33
    关注

    直接写capture(0)呢?
    还有,opencv默认0是笔记本之类的自带的id,你如果是台式机的话,摄像头一般是从1开始计算的,你看下是不是这个问题

    评论

报告相同问题?

问题事件

  • 创建了问题 12月1日