Advertisement
Guest User

Camera Capture Qt/OpenCV

a guest
Apr 3rd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. CMainWindow::CMainWindow(QWidget *parent)
  2.     : QMainWindow(parent),
  3.     m_CameraHandler(0),
  4.     m_ImageWidget(0),
  5.     m_ProcessedImageWidget(0),
  6.     m_ImageProcessor(0)
  7.     {
  8.     //Setup GUI
  9.     ui.setupUi(this);
  10.     //Connect start menu to the CameraHandler startStop connection
  11.     connect(ui.actionFrom_webcam,SIGNAL(triggered()),this,SLOT(startStopCamera()));
  12.     //Setup Widgets and camera handler
  13.     this->setupImageWidget();
  14.     this->setupProcessedImageWidget();
  15.     this->setupCameraHandler();
  16.     m_ImageProcessor=new CImageDenoiserL2();
  17.     //Connections to send images from the camera handler to the gui
  18.     connect(m_CameraHandler,SIGNAL(sendFrame(QImage)),m_ImageWidget,SLOT(drawFrame(QImage)));
  19.     connect(m_CameraHandler,SIGNAL(sendProcessedFrame(QImage)),m_ProcessedImageWidget,SLOT(drawFrame(QImage)));
  20.     //Connection for L2 denoising parameters
  21.     connect(ui.sliderTheta,SIGNAL(valueChanged(int)),m_ImageProcessor,SLOT(setTheta(int)));
  22.     connect(ui.sliderMaxIter,SIGNAL(valueChanged(int)),m_ImageProcessor,SLOT(setMaxIter(int)));
  23.     //Set L2 image processor as default
  24.     m_CameraHandler->setImageProcessor(m_ImageProcessor);
  25.     //Quit app on close
  26.     this->setAttribute(Qt::WA_DeleteOnClose);
  27.     qDebug() << "Main window thread ID" << this->thread(); 
  28. }
  29.  
  30. CMainWindow::~CMainWindow(){
  31.     m_CameraHandler->deleteLater();
  32.     m_ImageWidget->deleteLater();
  33.     m_ProcessedImageWidget->deleteLater(); 
  34.     emit windowClosed();
  35.     qDebug() << "CMainWindow deleted";
  36. }
  37.  
  38. CCameraHandler::CCameraHandler(QObject *parent):
  39.     QObject(parent),
  40.     m_CameraCapture(),
  41.     m_ImageProcessor(),
  42.     m_CameraCaptureThread(),
  43.     m_ProcessingThread(),  
  44.     m_CurrentFrame(400,400,CV_8UC3){
  45.  
  46.     m_CameraCaptureThread= new QThread();
  47.     m_ProcessingThread= new QThread();
  48.     m_CameraCapture= new CCameraCapture();
  49.     //Move camera capture object to thread
  50.     m_CameraCapture->moveToThread(m_CameraCaptureThread);
  51.     //Connect error signal)
  52.     QObject::connect(m_CameraCapture, SIGNAL(error(QString,QString)), this, SLOT(reportError(QString,QString)));
  53.     //Connect the finished signal of the worker class to the thread for quitting the loop
  54.     QObject::connect(m_CameraCapture, SIGNAL(finished()), m_CameraCaptureThread, SLOT(quit()));
  55.     //This connections guarantees that the *m_CVideoCapture is automatically deleted if the event loop of the thread is terminated. Therefore, m_CVideoCapture does not need to be released manually if the capturing process is stopped.
  56.     QObject::connect(m_CameraCaptureThread, SIGNAL(finished()), m_CameraCaptureThread, SLOT(deleteLater()));
  57.     QObject::connect(m_CameraCapture, SIGNAL(finished()), m_CameraCapture, SLOT(deleteLater()));
  58.  
  59.     //Connect sendFrame to update frame for displaying the current frame
  60.     QObject::connect(m_CameraCapture, SIGNAL(sendFrame(cv::Mat)), this, SLOT(receiveFrame(cv::Mat)));
  61.     QObject::connect(this, SIGNAL(startGrabbing()), m_CameraCapture, SLOT(startGrabbing()));
  62.     QObject::connect(this, SIGNAL(stopGrabbing()), m_CameraCapture, SLOT(stopGrabbing()));
  63.     m_CameraCaptureThread->start();
  64.     m_CameraCapture->openCamera();
  65.     QObject::connect(this, SIGNAL(startGrabbing()), this, SLOT(getNewFrame()));
  66.     m_ProcessingThread->start();
  67. }
  68.  
  69. CCameraHandler::~CCameraHandler(void){
  70.     m_CameraCapture->exitThread();
  71.     m_ImageProcessor->exitThread();
  72.     qDebug() << "CCameraHandler deleted";
  73. }
  74.  
  75. CCameraCapture::~CCameraCapture(void){
  76.     m_Cap.release();
  77.     qDebug() << "CCameraCapture deleted";
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement