歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

OpenCv在Qt中的使用

1.使用OpenCV進行打開攝像頭並進行錄像

  1.     CvCapture *capture;  
  2.     IplImage *frame;  
  3.     QImage *qImg;  
  4.     QTimer *timer;  
  5.     capture = cvCaptureFromCAM(0);  
  6.     cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,320);  
  7.     cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,240);  
  8.     if(capture)  
  9.     {  
  10.         QMessageBox::information(this,"Information","successful!");  
  11.         if (capture)  
  12.         {  
  13.             frame = cvQueryFrame(capture);  
  14.             if (frame)  
  15.                 this->resize(frame->width,frame->height);  
  16.             qImg = new QImage(QSize(frame->width,frame->height),QImage::Format_RGB888);  
  17.             iplImg = cvCreateImageHeader(cvSize(frame->width,frame->height),8,3);  
  18.             iplImg->imageData = (char*)qImg->bits();  
  19.             timer = new QTimer(this);  
  20.             timer->setInterval(30);  
  21.             connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));  
  22.             timer->start();  
  23.             writer = cvCreateVideoWriter("out.avi",CV_FOURCC('D''I''V''X'),10,  
  24.                                          cvSize(frame->width,frame->height),1);  
  25.             isCamera = true;  
  26.         }  
  27.     }  
  28.     else  
  29.     {  
  30.         QMessageBox::information(this,"Information","Sorry,fail!");  
  31.         isCamera = false;  
  32.     }  
  33. void openCV::paintEvent(QPaintEvent *e)  
  34. {  
  35.     QPainter painter(this);  
  36.     if(isCamera == true)  
  37.     {  
  38.         painter.drawImage(QPoint(0,0),*qImg);  
  39.     }  
  40.     else  
  41.     {  
  42.           
  43.     }  
  44. }  
  45. void openCV::nextFrame()  
  46. {  
  47.     frame = cvQueryFrame(capture);  
  48.   
  49.     if (frame)  
  50.     {  
  51.         if (frame->origin == IPL_ORIGIN_TL)  
  52.         {  
  53.             cvCopy(frame,iplImg,0);  
  54.         }  
  55.         else  
  56.         {  
  57.             cvFlip(frame,iplImg,0);  
  58.         }  
  59.         cvCvtColor(iplImg,iplImg,CV_BGR2RGB);  
  60.         cvWriteFrame(writer,frame);  
  61.         this->update();  
  62.     }  
  63. }  

在編譯的目錄下就有out.avi這個視頻文件,要打開視頻文件如下:

  1.     CvCapture *capture;  
  2.     IplImage *frame;  
  3.     QImage *qImg;  
  4.     QTimer *timer;  
  5.     capture = cvCaptureFromFile("out.avi");  
  6.     if (capture)  
  7.     {  
  8.         frame = cvQueryFrame(capture);  
  9.         if (frame)  
  10.             this->resize(frame->width,frame->height);  
  11.         qImg = new QImage(QSize(frame->width,frame->height),QImage::Format_RGB888);  
  12.         iplImg = cvCreateImageHeader(cvSize(frame->width,frame->height),8,3);  
  13.         iplImg->imageData = (char*)qImg->bits();  
  14.         timer = new QTimer(this);  
  15.         timer->setInterval(30);  
  16.         connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));  
  17.         timer->start();  
  18.               
  19.         isCamera = true;  
  20.     }  
  21.     else  
  22.     {  
  23.         QMessageBox::information(this,"Information","Sorry,fail!");  
  24.         isCamera = false;  
  25.     }  
  26. void openCV::paintEvent(QPaintEvent *e)  
  27. {  
  28.     QPainter painter(this);  
  29.     if(isCamera == true)  
  30.     {  
  31.         painter.drawImage(QPoint(0,0),*qImg);  
  32.     }  
  33.     else  
  34.     {  
  35.           
  36.     }  
  37. }  
  38. void openCV::nextFrame()  
  39. {  
  40.     frame = cvQueryFrame(capture);  
  41.   
  42.     if (frame)  
  43.     {  
  44.         if (frame->origin == IPL_ORIGIN_TL)  
  45.         {  
  46.             cvCopy(frame,iplImg,0);  
  47.         }  
  48.         else  
  49.         {  
  50.             cvFlip(frame,iplImg,0);  
  51.         }  
  52.         cvCvtColor(iplImg,iplImg,CV_BGR2RGB);  
  53.         this->update();  
  54.     }  
  55. }  
Copyright © Linux教程網 All Rights Reserved