opencv代码无法打开电脑摄像头

电脑为win10 64位 VS2015

// Video.cpp: implementation of the CVideo class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Fatiguedetection.h"
#include "Video.h"

#ifdef DEBUG
#undef THIS_FILE
static char THIS_FILE[]=
_FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CVideo::CVideo()
{

}

CVideo::~CVideo()
{

}

int CVideo::OpenCamera(HDC hdc, CRect rect)
{
Sleep(3000);
CvCapture *capture;
capture=cvCaptureFromCAM(-1); //捕获摄像头
if(!capture)
{
AfxMessageBox("打开摄像头失败!");
return 1;
}
m_pcapture=capture; //视像头指针传递
m_rect=rect; //获得picture控件的RECT

m_hdc=hdc; //获得picture控件的画图句柄

return -1;

}

void CVideo::PlayCamera()
{
m_pframe = cvQueryFrame(m_pcapture);//获取一帧
if (m_pframe)
{
m_showimage.CopyOf(m_pframe, 1);
m_showimage.DrawToHDC(m_hdc, &m_rect); //显示到控件
}
}

// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include

int main()
{
cv::VideoCapture capture_watcher(0);
cv::Mat Image;
int flag = 1;
char inputC = 0;
while(flag)
{
if (!(capture_watcher.read(Image)))
{
}
cv::imshow("Watcher", Image);
inputC = cv::waitKey(100);
if (inputC == 112)
{
flag = 0;
}
}
return 0;
}

同样的配置,打开电脑自带摄像头,按"P"停止,你可以试一下

#include
#include

using namespace std;
using namespace cv;

int main()
{
VideoCapture capture(0);
int i = 0;
while (1)
{
Mat frame;
char key = waitKey(100);
capture >> frame;
imshow("frame", frame);
string filename = format("D:\pic\pic%d.jpg",i);

    switch (key)
    {
    case'p':
        i++;
        imwrite(filename, frame);
        imshow("photo", frame);
        waitKey(500);
        destroyWindow("photo");
        break;
    default:
        break;
    }
}

}
我自己运行过,成功的话帮忙悬赏喔

include那里
<
opencv2/opencv.hpp>
<
iostream>
连起来就可以了

switch (key)
{
case'p':
    i++;
    imwrite(filename, frame);
    imshow("photo", frame);
    waitKey(500);
    destroyWindow("photo");
    break;
default:
    break;
}

}

//--------------------------------------【程序说明】-------------------------------------------
// 程序说明:《OpenCV3编程入门》OpenCV2版书本配套示例程序07
// 程序描述:使用VideoCapture类调用摄像头读入视频并显示
// 开发测试所用操作系统: Windows 7 64bit
// 开发测试所用IDE版本:Visual Studio 2010
// 开发测试所用OpenCV版本: 2.4.9
// 2014年03月 Created by @浅墨_毛星云
// 2014年11月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------

//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include

using namespace cv;

//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main( )

{

//【1】从摄像头读入视频
VideoCapture capture(0);

//【2】循环显示每一帧
while(1)  
{  
    Mat frame;  //定义一个Mat变量,用于存储每一帧的图像
    capture>>frame;  //读取当前帧
    imshow("读取视频",frame);  //显示当前帧
    waitKey(30);  //延时30ms
}  
return 0;     

}