【opencv】vc6.0中opencv打开摄像头失败解决方法

来源:互联网 发布:qq三国辅助软件 编辑:程序博客网 时间:2024/06/06 07:04

因为要修改的工程是VC6.0下的,只能用opencv1.0读取摄像头,但是在读取的过程中出现了各种问题

问题描述:opencv1.0版本的摄像头驱动不兼容win7

http://www.xuebuyuan.com/2124092.html

http://blog.csdn.net/williamvalentine/article/details/6599313  (这篇是DirectShow解决方案

但是也发生了很奇怪的问题,笔记本自带的摄像头可以通过这种方式读取,然后从图像采集卡的却不能(可以通过opencv2正常调用)

因此为了避免各种麻烦,直接用opencv2.4.9写出了dll供VC6.0调用来得到iplimage


dll代码如下:

#include <opencv2/opencv.hpp>#include <iostream>  using namespace std;  using namespace cv;  CvCapture *capture; IplImage *frame;extern "C" _declspec(dllexport)  int _stdcall openCam(int num){      if ((capture = cvCreateCameraCapture(num)) != 0)    {        return 1;    }    else    {        return 0;    }}  extern "C" _declspec(dllexport)  IplImage* _stdcall getIplimageFromCam(){    frame = cvQueryFrame(capture);     if (!frame || frame->height == 0 || frame->width == 0)    {        return 0;    }    else    {        return frame;    }}

有两个函数, openCam用来打开摄像头,getIplimageFromCam用来读取图片

编译后,将生成的dll与lib拷贝到VC6.0工程下


工程下添加代码:

#pragma comment(lib, "camdll.lib")extern  _declspec(dllexport)  int _stdcall openCam(int num);extern  _declspec(dllexport)  IplImage* _stdcall getIplimageFromCam();

后面的调用:

IplImage *image;if (!openCam(0))//打开摄像头{    printf("Can not open cam !\r\n");    return;}image = getIplimageFromCam();if (!image){    printf("Can not load image !\r\n");    return;}