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;
}




你可能感兴趣的:(OpenCV Camera 黑屏 与 录像avi实现)