OpenCV启动摄像头的问题 摄像头捕获 opencv 有现成的例子。你写的代码和例子差在哪?也不上代码,让大家如何分析 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是我疏忽了,代码如下:#include "cxcore.h"#include "cvcam.h"#include "windows.h"#include "highgui.h"int main(){ int camsnums = cvcamGetCamerasCount(); HWND MyWin; cvcamSetProperty(0,CVCAM_PROP_ENABLE,CVCAMTRUE); MyWin = (HWND)cvGetWindowHandle("CameraUSB window"); cvcamInit(); cvcamStart(); cvWaitKey(); cvcamStop(); cvcamExit(); return 0;} 接帖子问一下,我为什么WIN7连摄像头都打不开,同样的代码在XP上运行完全没问题 没有刷新刷新函数写在cvcamInit();或者cvcamStart(); 你的怕不是是win7吧,还是联想品牌的吧,我在做这个时候也遇到了这个问题,解决方法是下一个驱动程序嵌在你的程序中,很简单的。 //////////////////////////////////////////////////////////////////////// Video Capture using DirectShow// Author: Shiqi Yu ([email protected])// Thanks to:// HardyAI@OpenCV China// flymanbox@OpenCV China (for his contribution to function CameraName, and frame width/height setting)// Last modification: April 9, 2009////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 使用说明:// 1. 将CameraDS.h CameraDS.cpp以及目录DirectShow复制到你的项目中// 2. 菜单 Project->Settings->Settings for:(All configurations)->C/C++->Category(Preprocessor)->Additional include directories// 设置为 DirectShow/Include// 3. 菜单 Project->Settings->Settings for:(All configurations)->Link->Category(Input)->Additional library directories// 设置为 DirectShow/Lib//////////////////////////////////////////////////////////////////////#ifndef CCAMERA_H#define CCAMERA_H#define WIN32_LEAN_AND_MEAN#include <atlbase.h>#include "DirectShow/Include/qedit.h"#include "DirectShow/Include/dshow.h"#include <windows.h>#include <cxcore.h>#define MYFREEMEDIATYPE(mt) {if ((mt).cbFormat != 0) \ {CoTaskMemFree((PVOID)(mt).pbFormat); \ (mt).cbFormat = 0; \ (mt).pbFormat = NULL; \ } \ if ((mt).pUnk != NULL) \ { \ (mt).pUnk->Release(); \ (mt).pUnk = NULL; \ }} class CCameraDS {private: IplImage * m_pFrame; bool m_bConnected; int m_nWidth; int m_nHeight; bool m_bLock; bool m_bChanged; long m_nBufferSize; CComPtr<IGraphBuilder> m_pGraph; CComPtr<IBaseFilter> m_pDeviceFilter; CComPtr<IMediaControl> m_pMediaControl; CComPtr<IBaseFilter> m_pSampleGrabberFilter; CComPtr<ISampleGrabber> m_pSampleGrabber; CComPtr<IPin> m_pGrabberInput; CComPtr<IPin> m_pGrabberOutput; CComPtr<IPin> m_pCameraOutput; CComPtr<IMediaEvent> m_pMediaEvent; CComPtr<IBaseFilter> m_pNullFilter; CComPtr<IPin> m_pNullInputPin;private: bool BindFilter(int nCamIDX, IBaseFilter **pFilter); void SetCrossBar();public: CCameraDS(); virtual ~CCameraDS(); //打开摄像头,nCamID指定打开哪个摄像头,取值可以为0,1,2,... //bDisplayProperties指示是否自动弹出摄像头属性页 //nWidth和nHeight设置的摄像头的宽和高,如果摄像头不支持所设定的宽度和高度,则返回false bool CCameraDS::OpenCamera(int nCamID, bool bDisplayProperties=true, int nWidth=320, int nHeight=240); //关闭摄像头,析构函数会自动调用这个函数 void CloseCamera(); //返回摄像头的数目 //可以不用创建CCameraDS实例,采用int c=CCameraDS::CameraCount();得到结果。 static int CameraCount(); //根据摄像头的编号返回摄像头的名字 //nCamID: 摄像头编号 //sName: 用于存放摄像头名字的数组 //nBufferSize: sName的大小 //可以不用创建CCameraDS实例,采用CCameraDS::CameraName();得到结果。 static int CCameraDS::CameraName(int nCamID, char* sName, int nBufferSize); //返回图像宽度 int GetWidth(){return m_nWidth;} //返回图像高度 int GetHeight(){return m_nHeight;} //抓取一帧,返回的IplImage不可手动释放! //返回图像数据的为RGB模式的Top-down(第一个字节为左上角像素),即IplImage::origin=0(IPL_ORIGIN_TL) IplImage * QueryFrame();};#endif 谢谢了,我下过一个DirectShow的,但是没有成功 谢谢14楼。给cvWaitKey()加一个不为0的参数以后,问题就解决了。 遇到过,你看你的WIN 7 是否可以禁用DX。以前遇到过,不能禁用的时候果断换XP了。 大声求你写的源码文件 给我发一份吧 谢谢了 [email protected] 留邮箱了 谢谢啊 谢谢啊 谢谢啊 我用的是opencv2.4.8为什么无法打开#include "cvcam.h" 试试这个void CDirectshowTestDlg::OnBnClickedButton1(){ // TODO: 在此添加控件通知处理程序代码 ICaptureGraphBuilder2 * pBuilder = NULL; pGraph = NULL; pMediaControl = NULL; //枚举视频设备 ICreateDevEnum *pDevEnum = NULL; CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **)&pDevEnum); IEnumMoniker *pClassEnum = NULL; pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pClassEnum, 0); ULONG cFetched; if (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK) { pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc); pMoniker->Release(); } pClassEnum->Release(); CoCreateInstance(CLSID_CaptureGraphBuilder2,0,CLSCTX_INPROC_SERVER,IID_ICaptureGraphBuilder2,(void**)&pBuilder); CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); pBuilder->SetFiltergraph(pGraph);//设置滤波图表 pGraph->QueryInterface(IID_IMediaControl,(void**)&pMediaControl); pGraph->AddFilter(pSrc,L"avi");//连接视频源,第一个filter pPreview = NULL; IBaseFilter * decompression; IBaseFilter * colorspace; IPin * pSourceOut;//定义输出引脚 IPin* pPreIn;//定义输入引脚 CoCreateInstance(CLSID_MjpegDec,0,CLSCTX_ALL,IID_IBaseFilter,(void**)&decompression);//声明第二个filter CoCreateInstance(CLSID_Colour,0,CLSCTX_ALL,IID_IBaseFilter,(void**)&colorspace);//声明第三个filter CoCreateInstance(CLSID_VideoRenderer,0,CLSCTX_ALL,IID_IBaseFilter,(void**)&pPreview);//声明第四个filter if (pPreview != NULL) { //按顺序连接三个滤波图表,如果有多个,顺序连接即可 pGraph->AddFilter(decompression,L"decompression"); pSourceOut = FindPin(pSrc,PINDIR_OUTPUT);//按顺序找到输入、输出引脚,并连接 pPreIn = FindPin(decompression,PINDIR_INPUT); pGraph->ConnectDirect(pSourceOut,pPreIn,NULL); pGraph->AddFilter(colorspace,L"decompression"); pSourceOut = FindPin(decompression,PINDIR_OUTPUT); pPreIn = FindPin(colorspace,PINDIR_INPUT); pGraph->ConnectDirect(pSourceOut,pPreIn,NULL); pGraph->AddFilter(pPreview,L"preview"); //连接引脚 pSourceOut = FindPin(colorspace,PINDIR_OUTPUT); pPreIn = FindPin(pPreview,PINDIR_INPUT); pGraph->ConnectDirect(pSourceOut,pPreIn,NULL); //获取预览窗口 IVideoWindow * pViewWnd= NULL; pPreview->QueryInterface(IID_IVideoWindow,(void**)&pViewWnd); if (pViewWnd) { //设置预览窗口的拥有者 pViewWnd->put_Owner((long)m_hWnd); pViewWnd->put_Left(1); pViewWnd->put_Top(1); //获取预览窗口风格 long style; pViewWnd->get_WindowStyle(&style); style = style & ~WS_CAPTION; style = style & ~WS_DLGFRAME; style = style & WS_CHILD; pViewWnd->put_WindowStyle(style); //设置预览窗口宽度和高度 CRect rc; GetClientRect(rc); pViewWnd->put_Height(rc.Height()-60); pViewWnd->put_Width(rc.Width()-2); } pMediaControl->Run(); } return; // return TRUE unless you set the focus to a control} 分层窗口与Win7系统"显示桌面"冲突 子类化问题 结构体中包含一个可变长度字符串时,该怎么定义结构体? 变量赋值问题. 调用类方法的问题 怎样修改ccombo box的编辑框的高度? 关于在一个程序中使用多个COM的情况 请问如何定义i个CString 的数组?谢谢!! 怎样设置Grid的长度和宽度????? 如何用VC打开并且读取EXCEL数据 请问这个键盘监听程序,怎么样才能让它把其监听到的内容存储到txt文本当中呢? 'ATTRIB' 不是内部或外部命令,也不是可运行的程序,Error executing c:\windows\system32\cmd.exe.
#include "cvcam.h"
#include "windows.h"
#include "highgui.h"int main()
{
int camsnums = cvcamGetCamerasCount();
HWND MyWin;
cvcamSetProperty(0,CVCAM_PROP_ENABLE,CVCAMTRUE);
MyWin = (HWND)cvGetWindowHandle("CameraUSB window"); cvcamInit();
cvcamStart();
cvWaitKey();
cvcamStop();
cvcamExit(); return 0;
}
刷新函数写在cvcamInit();
或者cvcamStart();
// Video Capture using DirectShow
// Author: Shiqi Yu ([email protected])
// Thanks to:
// HardyAI@OpenCV China
// flymanbox@OpenCV China (for his contribution to function CameraName, and frame width/height setting)
// Last modification: April 9, 2009
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// 使用说明:
// 1. 将CameraDS.h CameraDS.cpp以及目录DirectShow复制到你的项目中
// 2. 菜单 Project->Settings->Settings for:(All configurations)->C/C++->Category(Preprocessor)->Additional include directories
// 设置为 DirectShow/Include
// 3. 菜单 Project->Settings->Settings for:(All configurations)->Link->Category(Input)->Additional library directories
// 设置为 DirectShow/Lib
//////////////////////////////////////////////////////////////////////#ifndef CCAMERA_H
#define CCAMERA_H#define WIN32_LEAN_AND_MEAN#include <atlbase.h>
#include "DirectShow/Include/qedit.h"
#include "DirectShow/Include/dshow.h"
#include <windows.h>
#include <cxcore.h>#define MYFREEMEDIATYPE(mt) {if ((mt).cbFormat != 0) \
{CoTaskMemFree((PVOID)(mt).pbFormat); \
(mt).cbFormat = 0; \
(mt).pbFormat = NULL; \
} \
if ((mt).pUnk != NULL) \
{ \
(mt).pUnk->Release(); \
(mt).pUnk = NULL; \
}}
class CCameraDS
{
private:
IplImage * m_pFrame;
bool m_bConnected;
int m_nWidth;
int m_nHeight;
bool m_bLock;
bool m_bChanged;
long m_nBufferSize; CComPtr<IGraphBuilder> m_pGraph;
CComPtr<IBaseFilter> m_pDeviceFilter;
CComPtr<IMediaControl> m_pMediaControl;
CComPtr<IBaseFilter> m_pSampleGrabberFilter;
CComPtr<ISampleGrabber> m_pSampleGrabber;
CComPtr<IPin> m_pGrabberInput;
CComPtr<IPin> m_pGrabberOutput;
CComPtr<IPin> m_pCameraOutput;
CComPtr<IMediaEvent> m_pMediaEvent;
CComPtr<IBaseFilter> m_pNullFilter;
CComPtr<IPin> m_pNullInputPin;private:
bool BindFilter(int nCamIDX, IBaseFilter **pFilter);
void SetCrossBar();public:
CCameraDS();
virtual ~CCameraDS(); //打开摄像头,nCamID指定打开哪个摄像头,取值可以为0,1,2,...
//bDisplayProperties指示是否自动弹出摄像头属性页
//nWidth和nHeight设置的摄像头的宽和高,如果摄像头不支持所设定的宽度和高度,则返回false
bool CCameraDS::OpenCamera(int nCamID, bool bDisplayProperties=true, int nWidth=320, int nHeight=240); //关闭摄像头,析构函数会自动调用这个函数
void CloseCamera(); //返回摄像头的数目
//可以不用创建CCameraDS实例,采用int c=CCameraDS::CameraCount();得到结果。
static int CameraCount(); //根据摄像头的编号返回摄像头的名字
//nCamID: 摄像头编号
//sName: 用于存放摄像头名字的数组
//nBufferSize: sName的大小
//可以不用创建CCameraDS实例,采用CCameraDS::CameraName();得到结果。
static int CCameraDS::CameraName(int nCamID, char* sName, int nBufferSize); //返回图像宽度
int GetWidth(){return m_nWidth;} //返回图像高度
int GetHeight(){return m_nHeight;} //抓取一帧,返回的IplImage不可手动释放!
//返回图像数据的为RGB模式的Top-down(第一个字节为左上角像素),即IplImage::origin=0(IPL_ORIGIN_TL)
IplImage * QueryFrame();
};#endif
void CDirectshowTestDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码 ICaptureGraphBuilder2 * pBuilder = NULL; pGraph = NULL; pMediaControl = NULL; //枚举视频设备 ICreateDevEnum *pDevEnum = NULL; CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **)&pDevEnum); IEnumMoniker *pClassEnum = NULL; pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pClassEnum, 0); ULONG cFetched; if (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK) { pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc); pMoniker->Release(); } pClassEnum->Release(); CoCreateInstance(CLSID_CaptureGraphBuilder2,0,CLSCTX_INPROC_SERVER,IID_ICaptureGraphBuilder2,(void**)&pBuilder); CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); pBuilder->SetFiltergraph(pGraph);//设置滤波图表 pGraph->QueryInterface(IID_IMediaControl,(void**)&pMediaControl); pGraph->AddFilter(pSrc,L"avi");//连接视频源,第一个filter pPreview = NULL; IBaseFilter * decompression; IBaseFilter * colorspace; IPin * pSourceOut;//定义输出引脚 IPin* pPreIn;//定义输入引脚 CoCreateInstance(CLSID_MjpegDec,0,CLSCTX_ALL,IID_IBaseFilter,(void**)&decompression);//声明第二个filter CoCreateInstance(CLSID_Colour,0,CLSCTX_ALL,IID_IBaseFilter,(void**)&colorspace);//声明第三个filter CoCreateInstance(CLSID_VideoRenderer,0,CLSCTX_ALL,IID_IBaseFilter,(void**)&pPreview);//声明第四个filter if (pPreview != NULL) { //按顺序连接三个滤波图表,如果有多个,顺序连接即可 pGraph->AddFilter(decompression,L"decompression"); pSourceOut = FindPin(pSrc,PINDIR_OUTPUT);//按顺序找到输入、输出引脚,并连接 pPreIn = FindPin(decompression,PINDIR_INPUT); pGraph->ConnectDirect(pSourceOut,pPreIn,NULL); pGraph->AddFilter(colorspace,L"decompression"); pSourceOut = FindPin(decompression,PINDIR_OUTPUT); pPreIn = FindPin(colorspace,PINDIR_INPUT); pGraph->ConnectDirect(pSourceOut,pPreIn,NULL); pGraph->AddFilter(pPreview,L"preview"); //连接引脚 pSourceOut = FindPin(colorspace,PINDIR_OUTPUT); pPreIn = FindPin(pPreview,PINDIR_INPUT); pGraph->ConnectDirect(pSourceOut,pPreIn,NULL); //获取预览窗口 IVideoWindow * pViewWnd= NULL; pPreview->QueryInterface(IID_IVideoWindow,(void**)&pViewWnd); if (pViewWnd) { //设置预览窗口的拥有者 pViewWnd->put_Owner((long)m_hWnd); pViewWnd->put_Left(1); pViewWnd->put_Top(1); //获取预览窗口风格 long style; pViewWnd->get_WindowStyle(&style); style = style & ~WS_CAPTION; style = style & ~WS_DLGFRAME; style = style & WS_CHILD; pViewWnd->put_WindowStyle(style); //设置预览窗口宽度和高度 CRect rc; GetClientRect(rc); pViewWnd->put_Height(rc.Height()-60); pViewWnd->put_Width(rc.Width()-2); } pMediaControl->Run(); } return; // return TRUE unless you set the focus to a control}