Qt5.3里使用OpenCV库采集摄像头图像保存为图片和视频

pro文件:
INCLUDEPATH += D:\Work\Video\OpenCV_Qt5.3\include\opencv \
               D:\Work\Video\OpenCV_Qt5.3\include\opencv2 \
               D:\Work\Video\OpenCV_Qt5.3\include
LIBS += D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_calib3d249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_contrib249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_core249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_features2d249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_flann249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_gpu249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_highgui249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_imgproc249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_legacy249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_ml249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_nonfree249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_objdetect249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_ocl249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_photo249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_stitching249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_superres249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_video249.dll.a \
    D:\Work\Video\OpenCV_Qt5.3\lib\libopencv_videostab249.dll.a
main.cpp

#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
#include <QDebug>
 
 
int main()
{
    CvCapture *capture=cvCaptureFromCAM(0);
    CvVideoWriter *video=NULL;
    IplImage *frame=NULL;
    IplImage *pSaveFrame=NULL;
 
 
    int i=0;
    int p[3];
    p[0]=CV_IMWRITE_JPEG_QUALITY;
    p[1]=255;
    p[2]=255;
 
 
    int n;
    if(!capture)
    {
        qDebug()<<"Cannot open the camera.";
        return -1;
    }
    else
    {
        frame=cvQueryFrame(capture);
        video=cvCreateVideoWriter("camera.avi",
                                  CV_FOURCC('X','V','I','D'),
                                  25,
                                  cvSize(frame->width,frame->height));
        if(video)
        {
            qDebug()<<"VideoWriter has been created.";
        }
 
 
        cvNamedWindow("Camera Video",1);
        while(i<400)
        {
            frame=cvQueryFrame(capture);
            if(!frame)
            {
                qDebug()<<"Cannot get frame from the capture.";
                break;
            }
            n=cvWriteFrame(video,frame);
            qDebug()<<n;
 
 
            cvShowImage("Camera Video",frame);
 
 
            pSaveFrame=cvCreateImage(cvSize(frame->width,frame->height),
                                     frame->depth,
                                     frame->nChannels);
            QString strFilename=QString("%1.jpg").arg(i);
            char *chFilename;
            QByteArray ba=strFilename.toLatin1();
            chFilename=ba.data();
            i++;
            cvResize(frame,pSaveFrame,CV_INTER_LINEAR);
            cvSaveImage(chFilename,
                        pSaveFrame,
                        p);
            cvReleaseImage(&pSaveFrame);
 
 
            if(cvWaitKey(1)>0)
                break;
        }
        cvReleaseVideoWriter(&video);
        cvReleaseCapture(&capture);
        cvDestroyWindow("Camera Video");
    }
 
 
    return 0;
}
 

你可能感兴趣的:(opencv,2.4.9,qt5.3.0)