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 0938f1d1e4..61d756ded3 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,90 +1,114 @@ /*=================================================================== 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 "opencv2/opencv.hpp" const std::string SimpleOpenCVExample::VIEW_ID = "org.mitk.views.simpleopencvexample"; -void SimpleOpenCVExample::SetFocus() -{ -} +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) { - -} - -SimpleOpenCVExample::~SimpleOpenCVExample() +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_Controls.m_SeparateWindow->isChecked()) + { + cv::namedWindow("Video", 1); + while (m_running) { - cv::VideoCapture cap = cv::VideoCapture(); // open the default camera - cap.open(0); - if (!cap.isOpened()) // check if we succeeded - return; - - cv::namedWindow("Video", 1); - - while (m_running) - { - cv::Mat frame; - cap >> frame; // get a new frame from camera - imshow("Video", frame); - - // imwrite("C:/temp/output.jpg", frame); - - // Press 'c' to escape - if (cv::waitKey(30) == 'c') - break; - } + 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; } - - } - else - { - m_running = false; - m_Controls.m_StartGrabbing->setText("Start Video Grabbing"); - cv::destroyWindow("Video"); } + 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( + imageNode->GetData()->GetGeometry(), mitk::RenderingManager::REQUEST_UPDATE_ALL, true); + + m_UpdateTimer->setInterval(20); + m_UpdateTimer->start(); + } + } + else + { + m_UpdateTimer->stop(); + m_running = false; + m_Controls.m_StartGrabbing->setText("Start Video Grabbing"); + cv::destroyWindow("Video"); + 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() +{ + 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); } 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 8e67f64b48..7f15c12b40 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,56 +1,63 @@ /*=================================================================== 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; + mitk::OpenCVToMitkImageFilter::Pointer m_conversionFilter; + Ui::SimpleOpenCVExampleControls m_Controls; + + QTimer *m_UpdateTimer; }; #endif // SimpleOpenCVExample_h