diff --git a/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.cpp b/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.cpp index e6bfe23ad2..8cb7f27ffb 100644 --- a/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.cpp +++ b/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.cpp @@ -1,143 +1,173 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Blueberry #include <berryISelectionService.h> #include <berryIWorkbenchWindow.h> // Qmitk #include "SimpleOpenCVExample.h" // mitk image -#include <mitkImage.h> #include <QTimer> #include <mitkIRenderingManager.h> +#include <mitkImage.h> -#include <opencv2/highgui/highgui.hpp> -#include <opencv2/core/core.hpp> - - +#include <opencv2/core/core.hpp> +#include <opencv2/highgui/highgui.hpp> const std::string SimpleOpenCVExample::VIEW_ID = "org.mitk.views.simpleopencvexample"; void SimpleOpenCVExample::SetFocus() {} void SimpleOpenCVExample::CreateQtPartControl(QWidget *parent) { // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi(parent); connect(m_Controls.m_StartGrabbing, SIGNAL(clicked()), this, SLOT(OnStartGrabbing())); connect(m_UpdateTimer, SIGNAL(timeout()), this, SLOT(OnUpdateImage())); } -SimpleOpenCVExample::SimpleOpenCVExample() : m_running(false), m_UpdateTimer(new QTimer()) -{ -} +SimpleOpenCVExample::SimpleOpenCVExample() : m_running(false), m_UpdateTimer(new QTimer()) {} SimpleOpenCVExample::~SimpleOpenCVExample() {} void SimpleOpenCVExample::OnStartGrabbing() { if (!m_running) { m_running = true; m_Controls.m_StartGrabbing->setText("Stop Video Grabbing"); m_VideoCapture = new cv::VideoCapture(); // open the default camera m_VideoCapture->open(m_Controls.m_InputID->value()); - - if (!m_VideoCapture->isOpened()) {return;} // check if we succeeded + + if (!m_VideoCapture->isOpened()) + { + return; + } // check if we succeeded if (m_Controls.m_enableResolution->isChecked()) { m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, m_Controls.m_resX->value()); m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, m_Controls.m_resY->value()); } - cv::VideoWriter outputVideo1; - cv::VideoWriter outputVideo2; + if (m_Controls.m_secondStream->isChecked()) + { + m_VideoCapture2 = cv::VideoCapture(); + m_VideoCapture2.open(m_Controls.m_InputID_2->value()); + } + + int millisPerFrame = 1000.0 / m_Controls.m_UpdateRate->value(); + int fps = m_Controls.m_UpdateRate->value(); + if (m_Controls.m_writeFile->isChecked()) { cv::Size S = cv::Size((int)m_VideoCapture->get(CV_CAP_PROP_FRAME_WIDTH), // Acquire input size (int)m_VideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT)); std::string name = m_Controls.m_firstFile->text().toStdString(); - int ex = static_cast<int>(m_VideoCapture->get(CV_CAP_PROP_FOURCC)); - //outputVideo1.open(name, ex = -1, m_VideoCapture->get(CV_CAP_PROP_FPS), S, true); - outputVideo1.open(name, cv::VideoWriter::fourcc('M','J','P','G'), 20, S, true); + m_outputVideo1 = cv::VideoWriter(); + m_outputVideo1.open(name, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, S, true); + + if (m_Controls.m_secondStream->isChecked()) + { + cv::Size S = cv::Size((int)m_VideoCapture->get(CV_CAP_PROP_FRAME_WIDTH), // Acquire input size + (int)m_VideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT)); + m_outputVideo2 = cv::VideoWriter(); + m_outputVideo2.open(m_Controls.m_secondFile->text().toStdString(), + cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, + S, + true); + } } if (m_Controls.m_SeparateWindow->isChecked()) { cv::namedWindow("Video", 1); - while (m_running) - { - cv::Mat frame; - *m_VideoCapture >> frame; // get a new frame from camera - imshow("Video", frame); - // Example to write a frame to a file: - // imwrite("C:/temp/output.jpg", frame); - // Press 'c' to stop the stream - if (cv::waitKey(30) == 'c') break; - if (m_Controls.m_writeFile->isChecked()) - { - outputVideo1.write(frame); - } - } + if (m_Controls.m_secondStream->isChecked()) + cv::namedWindow("Video2", 1); + } else if (m_Controls.m_MITKImage->isChecked()) { - mitk::DataNode::Pointer imageNode = mitk::DataNode::New(); imageNode->SetName("Open CV Example Image Stream"); imageNode->SetData(mitk::Image::New()); m_conversionFilter = mitk::OpenCVToMitkImageFilter::New(); this->GetDataStorage()->Add(imageNode); OnUpdateImage(); - + // Initialize view on Image mitk::IRenderWindowPart *renderWindow = this->GetRenderWindowPart(); - if (renderWindow != NULL) renderWindow->GetRenderingManager()->InitializeViews( + if (renderWindow != NULL) + renderWindow->GetRenderingManager()->InitializeViews( imageNode->GetData()->GetGeometry(), mitk::RenderingManager::REQUEST_UPDATE_2DWINDOWS, true); - - // Start timer for update loop - m_UpdateTimer->setInterval(20); - m_UpdateTimer->start(); - } + + // Start timer for update loop + m_UpdateTimer->setInterval(millisPerFrame); + m_UpdateTimer->start(); } else { m_UpdateTimer->stop(); + m_outputVideo1 = cv::VideoWriter(); + m_outputVideo2 = cv::VideoWriter(); m_running = false; m_Controls.m_StartGrabbing->setText("Start Video Grabbing"); cv::destroyWindow("Video"); + cv::destroyWindow("Video2"); mitk::DataNode::Pointer imageNode = this->GetDataStorage()->GetNamedNode("Open CV Example Image Stream"); this->GetDataStorage()->Remove(imageNode); m_VideoCapture->release(); delete m_VideoCapture; } } -void SimpleOpenCVExample::OnUpdateImage() +void SimpleOpenCVExample::OnUpdateImage() { + if (m_Controls.m_MITKImage->isChecked()) updateMITKImage(); + else updateOpenCVWindow(); +} + +void SimpleOpenCVExample::updateMITKImage() { cv::Mat image; *m_VideoCapture >> image; IplImage ipl_img = image; m_conversionFilter->SetOpenCVImage(&ipl_img); m_conversionFilter->Update(); this->GetDataStorage()->GetNamedNode("Open CV Example Image Stream")->SetData(m_conversionFilter->GetOutput()); this->RequestRenderWindowUpdate(mitk::RenderingManager::REQUEST_UPDATE_ALL); } + +void SimpleOpenCVExample::updateOpenCVWindow() { + cv::Mat frame; + *m_VideoCapture >> frame; // get a new frame from camera + imshow("Video", frame); + + if (m_Controls.m_secondStream->isChecked()) + { + cv::Mat frame2; + m_VideoCapture2 >> frame2; + imshow("Video2", frame2); + if (m_Controls.m_writeFile->isChecked()) + m_outputVideo2.write(frame2); + } + + if (m_Controls.m_writeFile->isChecked()) + m_outputVideo1.write(frame); +} diff --git a/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.h b/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.h index 7f15c12b40..5a4dae5229 100644 --- a/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.h +++ b/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExample.h @@ -1,63 +1,70 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef SimpleOpenCVExample_h #define SimpleOpenCVExample_h #include <berryISelectionListener.h> #include <QTime> #include <QmitkAbstractView.h> #include "ui_SimpleOpenCVExampleControls.h" #include "mitkOpenCVToMitkImageFilter.h" #include "opencv2/opencv.hpp" /** \brief TODO */ class SimpleOpenCVExample : public QmitkAbstractView { Q_OBJECT public: static const std::string VIEW_ID; SimpleOpenCVExample(); virtual ~SimpleOpenCVExample(); protected slots: /// \brief Called when the user clicks the GUI button void OnStartGrabbing(); void OnUpdateImage(); protected: virtual void CreateQtPartControl(QWidget *parent) override; virtual void SetFocus() override; bool m_running; cv::VideoCapture *m_VideoCapture; + cv::VideoCapture m_VideoCapture2; + cv::VideoWriter m_outputVideo1; + cv::VideoWriter m_outputVideo2; + mitk::OpenCVToMitkImageFilter::Pointer m_conversionFilter; Ui::SimpleOpenCVExampleControls m_Controls; QTimer *m_UpdateTimer; + + void updateMITKImage(); + void updateOpenCVWindow(); }; #endif // SimpleOpenCVExample_h diff --git a/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExampleControls.ui b/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExampleControls.ui index 0b04b67c2b..ebd938e1d1 100644 --- a/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExampleControls.ui +++ b/Examples/Plugins/org.mitk.example.gui.opencv/src/internal/SimpleOpenCVExampleControls.ui @@ -1,260 +1,291 @@ <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>SimpleOpenCVExampleControls</class> <widget class="QWidget" name="SimpleOpenCVExampleControls"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>262</width> <height>455</height> </rect> </property> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="windowTitle"> <string>QmitkTemplate</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QLabel" name="label"> <property name="text"> <string>Input ID</string> </property> </widget> </item> <item> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item> <widget class="QSpinBox" name="m_InputID"/> </item> </layout> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_2"> <item> <widget class="QCheckBox" name="m_enableResolution"> <property name="text"> <string>Manual Resolution:</string> </property> </widget> </item> <item> <spacer name="horizontalSpacer_2"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item> <widget class="QSpinBox" name="m_resX"> <property name="maximum"> <number>10000</number> </property> <property name="value"> <number>1280</number> </property> </widget> </item> <item> <widget class="QSpinBox" name="m_resY"> <property name="maximum"> <number>10000</number> </property> <property name="value"> <number>1024</number> </property> </widget> </item> </layout> </item> <item> <widget class="QRadioButton" name="m_SeparateWindow"> <property name="text"> <string>Render in Separate Window</string> </property> <property name="checked"> <bool>true</bool> </property> </widget> </item> <item> <widget class="QRadioButton" name="m_MITKImage"> <property name="text"> <string>Render as MITK Image</string> </property> </widget> </item> <item> <widget class="QPushButton" name="m_StartGrabbing"> <property name="text"> <string>Start Video Grabbing</string> </property> </widget> </item> <item> <widget class="Line" name="line"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> </widget> </item> <item> <widget class="QCheckBox" name="m_secondStream"> <property name="text"> <string>Second Stream</string> </property> </widget> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_3"> <item> <widget class="QLabel" name="label_2"> <property name="text"> <string>Second Input ID</string> </property> </widget> </item> <item> <spacer name="horizontalSpacer_3"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item> <widget class="QSpinBox" name="m_InputID_2"> <property name="value"> <number>1</number> </property> </widget> </item> </layout> </item> <item> <widget class="Line" name="line_2"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> </widget> </item> <item> <widget class="QCheckBox" name="m_writeFile"> <property name="text"> <string>Write Video File</string> </property> </widget> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_4"> <item> <widget class="QLabel" name="label_3"> <property name="text"> <string>First File:</string> </property> </widget> </item> <item> <spacer name="horizontalSpacer_4"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item> <widget class="QLineEdit" name="m_firstFile"> <property name="text"> <string>C:/temp/video1.avi</string> </property> </widget> </item> </layout> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_5"> <item> <widget class="QLabel" name="label_4"> <property name="text"> <string>Second File:</string> </property> </widget> </item> <item> <spacer name="horizontalSpacer_5"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item> <widget class="QLineEdit" name="m_secondFile"> <property name="text"> <string>C:/temp/video2.avi</string> </property> </widget> </item> </layout> </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_6"> + <item> + <widget class="QLabel" name="label_5"> + <property name="text"> + <string>Update Rate (FPS):</string> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_6"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QSpinBox" name="m_UpdateRate"> + <property name="value"> + <number>30</number> + </property> + </widget> + </item> + </layout> + </item> <item> <spacer name="spacer1"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeType"> <enum>QSizePolicy::Expanding</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>220</height> </size> </property> </spacer> </item> </layout> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>