1.使用OpenCV进行打开摄像头并进行录像
- CvCapture *capture;
- IplImage *frame;
- QImage *qImg;
- QTimer *timer;
- capture = cvCaptureFromCAM(0);
- cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,320);
- cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,240);
- if(capture)
- {
- QMessageBox::information(this,"Information","successful!");
- if (capture)
- {
- frame = cvQueryFrame(capture);
- if (frame)
- this->resize(frame->width,frame->height);
- qImg = new QImage(QSize(frame->width,frame->height),QImage::Format_RGB888);
- iplImg = cvCreateImageHeader(cvSize(frame->width,frame->height),8,3);
- iplImg->imageData = (char*)qImg->bits();
- timer = new QTimer(this);
- timer->setInterval(30);
- connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));
- timer->start();
- writer = cvCreateVideoWriter("out.avi",CV_FOURCC('D', 'I', 'V', 'X'),10,
- cvSize(frame->width,frame->height),1);
- isCamera = true;
- }
- }
- else
- {
- QMessageBox::information(this,"Information","Sorry,fail!");
- isCamera = false;
- }
- void openCV::paintEvent(QPaintEvent *e)
- {
- QPainter painter(this);
- if(isCamera == true)
- {
- painter.drawImage(QPoint(0,0),*qImg);
- }
- else
- {
- }
- }
- void openCV::nextFrame()
- {
- frame = cvQueryFrame(capture);
- if (frame)
- {
- if (frame->origin == IPL_ORIGIN_TL)
- {
- cvCopy(frame,iplImg,0);
- }
- else
- {
- cvFlip(frame,iplImg,0);
- }
- cvCvtColor(iplImg,iplImg,CV_BGR2RGB);
- cvWriteFrame(writer,frame);
- this->update();
- }
- }
在编译的目录下就有out.avi这个视频文件,要打开视频文件如下:
- CvCapture *capture;
- IplImage *frame;
- QImage *qImg;
- QTimer *timer;
- capture = cvCaptureFromFile("out.avi");
- if (capture)
- {
- frame = cvQueryFrame(capture);
- if (frame)
- this->resize(frame->width,frame->height);
- qImg = new QImage(QSize(frame->width,frame->height),QImage::Format_RGB888);
- iplImg = cvCreateImageHeader(cvSize(frame->width,frame->height),8,3);
- iplImg->imageData = (char*)qImg->bits();
- timer = new QTimer(this);
- timer->setInterval(30);
- connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));
- timer->start();
- isCamera = true;
- }
- else
- {
- QMessageBox::information(this,"Information","Sorry,fail!");
- isCamera = false;
- }
- void openCV::paintEvent(QPaintEvent *e)
- {
- QPainter painter(this);
- if(isCamera == true)
- {
- painter.drawImage(QPoint(0,0),*qImg);
- }
- else
- {
- }
- }
- void openCV::nextFrame()
- {
- frame = cvQueryFrame(capture);
- if (frame)
- {
- if (frame->origin == IPL_ORIGIN_TL)
- {
- cvCopy(frame,iplImg,0);
- }
- else
- {
- cvFlip(frame,iplImg,0);
- }
- cvCvtColor(iplImg,iplImg,CV_BGR2RGB);
- this->update();
- }
- }
- 这里面有个问题,第一,程序无法运行,经检查是
- cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,320);
- cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,240);
-
- 这两句话的问题。查了一圈资料,貌似不支持。将其屏蔽!注释掉,程序就能运行了!
- 第二,变量isCamera应该申明成为全局变量。按照原博客里的申明方法根本不可运行。将其申明为:static bool isCamera = false;这句话放在头文件的外面!!!
- 其他部分参照下面程序即可!
- 第一,有时候运行程序会卡住,直接出不来图像。在MyWidget的析构函数中增加cvReleaseImage(&frame)好了很多。另外定时器的间隔时间调40ms、50ms会好点。我的电脑内存是4G,当内存占用1300多M时,运行程序卡的几率很大。重启后卡的几率就变小了。 不知道为什么??
- 第二,每次运行程序时,在显示图片之前会有警告。mmap:无效的参数
- munmap:无效的参数
- munmap;无效的参数 。
我查了很多资料貌似是内存申请的问题。我在其他网站下载了另外一个读取摄像头视频的程序,也可以运行。程序构建和上述有点不同,但也是会出现这些警告。这些警告过后就能照常运行了。 我猜测这是第一帧图片没有正确申请内存的原因。解决办法正在思考zhong. 也希望高人过来指点!!!