OpenCV Camera 黑屏 与 录像avi实现

OpenCV1.0

VC6.0

win7 64bit


opencv 可以成功打开camera,但是预览总是黑屏(原因是camera 使用了DirectShow,而opencv 只支持vfw接口

http://hi.baidu.com/sail2011/item/9d0d736d60b9540fa1cf0f96

http://wiki.opencv.org.cn/index.php/%e4%bd%bf%e7%94%a8DirectShow%e9%87%87%e9%9b%86%e5%9b%be%e5%83%8f

录制avi格式视频,测试代码如下:

#include "camerads.h"
#include <highgui.h>
#include <stdio.h>
 
int main()
{
 
	CCameraDS camera;
	CvVideoWriter* video=NULL;

 

	//if(! camera.OpenCamera(0, true)) //弹出属性选择窗口
	if(! camera.OpenCamera(0, false, 640,480)) //不弹出属性选择窗口,用代码制定图像宽和高
	{
		fprintf(stderr, "Can not open camera.\n");
		return -1;
	}

	IplImage *frame = camera.QueryFrame();
    video=cvCreateVideoWriter("camera.avi", CV_FOURCC('X', 'V', 'I', 'D'), 25,cvSize(frame->width,frame->height)); 
    if(video) 
	{
        printf("VideoWriter has created.\n");
	}
	else
	{
		printf("VideoWriter has create fail.\n");
		return -1;
	
	}
 	cvNamedWindow("camera");
	int i = 0;
	int n = 0;
	while(i <= 200)
	{
		frame=camera.QueryFrame(); 
		if(!frame)
        {
             printf("Can not get frame from the capture.\n");
             break;
        }
        n=cvWriteFrame(video,frame); 
        if(n != 1)
		printf("write video fail\n");
		cvShowImage("camera", frame);
        i++;
		if (cvWaitKey(20) == 'q')
			break; 
	}
	camera.CloseCamera(); 
    cvReleaseVideoWriter(&video);
	cvDestroyWindow("camera");
	return 0;
}



播放avi视频 

#include "cxcore.h"
#include "cvcam.h"
#include "windows.h"
#include <string>
#include "highgui.h"



int main( int argc ,char ** argv)
{
	
	CvCapture * capture = cvCreateFileCapture ("camera.avi"); 
    if(capture==NULL) 
	{
		printf("NO capture");   
		return 1;
	};
    
	double fps=cvGetCaptureProperty(capture, CV_CAP_PROP_FPS );   
	
	int vfps = 1000 / fps;                                        
    printf("%5.1f\t%5d\n",fps,vfps);

	cvNamedWindow("example",CV_WINDOW_AUTOSIZE);                 
	IplImage * frame;

	while(1)
	{ 
		frame = cvQueryFrame( capture );                        
		if(!frame)
			break;
		cvShowImage("example",frame);  
		char c = cvWaitKey(vfps);
		if(c == 27 )break;
	}

	 cvReleaseCapture(&capture);
	 cvDestroyWindow("example");
	 return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值