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 #include // Qmitk #include "SimpleOpenCVExample.h" // mitk image -#include #include #include +#include -#include -#include - - +#include +#include 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(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 #include #include #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 @@ SimpleOpenCVExampleControls 0 0 262 455 0 0 QmitkTemplate Input ID Qt::Horizontal 40 20 Manual Resolution: Qt::Horizontal 40 20 10000 1280 10000 1024 Render in Separate Window true Render as MITK Image Start Video Grabbing Qt::Horizontal Second Stream Second Input ID Qt::Horizontal 40 20 1 Qt::Horizontal Write Video File First File: Qt::Horizontal 40 20 C:/temp/video1.avi Second File: Qt::Horizontal 40 20 C:/temp/video2.avi + + + + + + Update Rate (FPS): + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 30 + + + + + Qt::Vertical QSizePolicy::Expanding 20 220