diff --git a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp index ee58efa78c..eba3656c55 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp +++ b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp @@ -1,90 +1,100 @@ /*=================================================================== 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. ===================================================================*/ #include "mitkCropOpenCVImageFilter.h" #include "cv.h" namespace mitk { +CropOpenCVImageFilter::CropOpenCVImageFilter( ) + : m_NewCropRegionSet(false) +{ + +} + bool CropOpenCVImageFilter::FilterImage( cv::Mat& image ) { if (m_CropRegion.width == 0) { MITK_ERROR("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Cropping cannot be done without setting a non-empty crop region first."; return false; } - cv::Rect cropRegion = m_CropRegion; - - // We can try and correct too large boundaries - if ( cropRegion.x + cropRegion.width > image.size().width) + // We can try and correct too large boundaries (do this only once + // after a new crop region was set. + if (m_NewCropRegionSet) { - cropRegion.width = image.size().width - cropRegion.x; - MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") - << "Changed too large roi in x direction to fit the image size."; - } - if ( cropRegion.y + cropRegion.height > image.size().height) - { - cropRegion.height = image.size().height - cropRegion.y; - MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") - << "Changed too large roi in y direction to fit the image size."; + m_NewCropRegionSet = false; + + if ( m_CropRegion.x + m_CropRegion.width > image.size().width) + { + m_CropRegion.width = image.size().width - m_CropRegion.x; + MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") + << "Changed too large roi in x direction to fit the image size."; + } + if ( m_CropRegion.y + m_CropRegion.height > image.size().height) + { + m_CropRegion.height = image.size().height - m_CropRegion.y; + MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") + << "Changed too large roi in y direction to fit the image size."; + } } - cv::Mat buffer = image(cropRegion); + cv::Mat buffer = image(m_CropRegion); image.release(); image = buffer; return true; } void CropOpenCVImageFilter::SetCropRegion( cv::Rect cropRegion ) { // First, let's do some basic checks to make sure rectangle is inside of actual image if (cropRegion.x < 0) { MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Changed negative x value in roi to 0."; cropRegion.x = 0; } if (cropRegion.y < 0) { cropRegion.y = 0; MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Changed negative y value in roi to 0."; } // Nothing to save, throw an exception if ( cropRegion.height < 0 || cropRegion.width < 0 ) { mitkThrow() << "Invalid boundaries supplied to USImageVideoSource::SetRegionOfInterest()"; } m_CropRegion = cropRegion; } void CropOpenCVImageFilter::SetCropRegion( int topLeftX, int topLeftY, int bottomRightX, int bottomRightY ) { this->SetCropRegion( cv::Rect(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY) ); } cv::Rect CropOpenCVImageFilter::GetCropRegion( ) { return m_CropRegion; } } // namespace mitk diff --git a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h index 2f8b9d32ed..8f96965c40 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h +++ b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h @@ -1,66 +1,73 @@ /*=================================================================== 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 mitkAbstractOpenCVImageFilter_h #define mitkAbstractOpenCVImageFilter_h #include "mitkAbstractOpenCVImageFilter.h" #include "cv.h" //itk headers #include namespace mitk { class MITK_OPENCVVIDEOSUPPORT_EXPORT CropOpenCVImageFilter : public AbstractOpenCVImageFilter { public: mitkClassMacro(CropOpenCVImageFilter, AbstractOpenCVImageFilter); itkNewMacro(Self); + CropOpenCVImageFilter( ); + /** * \brief Crops image to rectangle given by mitk::CropOpenCVImageFilter::SetCropRegion. * \return false if no crop region was set or the crop region width is zero, true otherwise. */ bool FilterImage( cv::Mat& image ); /** * \brief Set region of interest for cropping. */ void SetCropRegion( cv::Rect cropRegion ); /** * \brief Set region of interest for cropping. */ void SetCropRegion( int topLeftX, int topLeftY, int bottomRightX, int bottomRightY ); /** * \brief Returns region, which was set by mitk::CropOpenCVImageFilter::SetCropRegion(). */ cv::Rect GetCropRegion( ); protected: /** * \brief Defines the region which will be cropped from the image. */ cv::Rect m_CropRegion; + + /** + * \brief True if no image was filtered since last set of a crop region. + */ + bool m_NewCropRegionSet; }; } // namespace mitk #endif // mitkAbstractOpenCVImageFilter_h diff --git a/Plugins/org.mitk.gui.qt.ultrasound/src/internal/UltrasoundSupport.cpp b/Plugins/org.mitk.gui.qt.ultrasound/src/internal/UltrasoundSupport.cpp index 48bcc422eb..62cd504b9f 100644 --- a/Plugins/org.mitk.gui.qt.ultrasound/src/internal/UltrasoundSupport.cpp +++ b/Plugins/org.mitk.gui.qt.ultrasound/src/internal/UltrasoundSupport.cpp @@ -1,211 +1,211 @@ /*=================================================================== 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 //Mitk #include #include #include // Qmitk #include "UltrasoundSupport.h" #include // Qt #include // Ultrasound #include "mitkUSDevice.h" const std::string UltrasoundSupport::VIEW_ID = "org.mitk.views.ultrasoundsupport"; void UltrasoundSupport::SetFocus() { m_Controls.m_AddDevice->setFocus(); } void UltrasoundSupport::CreateQtPartControl( QWidget *parent ) { m_Timer = new QTimer(this); // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi( parent ); connect( m_Controls.m_AddDevice, SIGNAL(clicked()), this, SLOT(OnClickedAddNewDevice()) ); // Change Widget Visibilities connect( m_Controls.m_AddDevice, SIGNAL(clicked()), this->m_Controls.m_NewVideoDeviceWidget, SLOT(CreateNewDevice()) ); // Init NewDeviceWidget connect( m_Controls.m_NewVideoDeviceWidget, SIGNAL(Finished()), this, SLOT(OnNewDeviceWidgetDone()) ); // After NewDeviceWidget finished editing connect( m_Controls.m_BtnView, SIGNAL(clicked()), this, SLOT(OnClickedViewDevice()) ); connect( m_Timer, SIGNAL(timeout()), this, SLOT(DisplayImage())); connect( m_Controls.crop_left, SIGNAL(valueChanged(int)), this, SLOT(OnCropAreaChanged()) ); connect( m_Controls.crop_right, SIGNAL(valueChanged(int)), this, SLOT(OnCropAreaChanged()) ); connect( m_Controls.crop_top, SIGNAL(valueChanged(int)), this, SLOT(OnCropAreaChanged()) ); connect( m_Controls.crop_bot, SIGNAL(valueChanged(int)), this, SLOT(OnCropAreaChanged()) ); //connect (m_Controls.m_ActiveVideoDevices, SIGNAL()) // Initializations m_Controls.m_NewVideoDeviceWidget->setVisible(false); std::string filter = "(&(" + mitk::ServiceConstants::OBJECTCLASS() + "=" + "org.mitk.services.UltrasoundDevice)(" + mitk::USDevice::US_PROPKEY_ISACTIVE + "=true))"; m_Controls.m_ActiveVideoDevices->Initialize(mitk::USDevice::US_PROPKEY_LABEL ,filter); //UI initializations m_Controls.crop_left->setEnabled(false); m_Controls.crop_right->setEnabled(false); m_Controls.crop_bot->setEnabled(false); m_Controls.crop_top->setEnabled(false); m_Node = mitk::DataNode::New(); m_Node->SetName("US Image Stream"); this->GetDataStorage()->Add(m_Node); } void UltrasoundSupport::OnClickedAddNewDevice() { m_Controls.m_NewVideoDeviceWidget->setVisible(true); m_Controls.m_DeviceManagerWidget->setVisible(false); m_Controls.m_AddDevice->setVisible(false); m_Controls.m_Headline->setText("Add New Device:"); } void UltrasoundSupport::DisplayImage() { m_Device->UpdateOutputData(0); m_Node->SetData(m_Device->GetOutput()); this->RequestRenderWindowUpdate(); m_FrameCounter ++; if (m_FrameCounter == 10) { int nMilliseconds = m_Clock.restart(); int fps = 10000.0f / (nMilliseconds ); m_Controls.m_FramerateLabel->setText("Current Framerate: "+ QString::number(fps) +" FPS"); m_FrameCounter = 0; } } void UltrasoundSupport::OnCropAreaChanged() { if (m_Device->GetDeviceClass()=="org.mitk.modules.us.USVideoDevice") { mitk::USVideoDevice::Pointer currentVideoDevice = dynamic_cast(m_Device.GetPointer()); mitk::USDevice::USImageCropArea newArea; newArea.cropLeft = m_Controls.crop_left->value(); newArea.cropTop = m_Controls.crop_top->value(); newArea.cropRight = m_Controls.crop_right->value(); newArea.cropBottom = m_Controls.crop_bot->value(); //check enabled: if not we are in the initializing step and don't need to do anything //otherwise: update crop area if (m_Controls.crop_right->isEnabled()) currentVideoDevice->SetCropArea(newArea); GlobalReinit(); } else { MITK_WARN << "No USVideoDevice: Cannot Crop!"; } } void UltrasoundSupport::OnClickedViewDevice() { m_FrameCounter = 0; // We use the activity state of the timer to determine whether we are currently viewing images if ( ! m_Timer->isActive() ) // Activate Imaging { //get device & set data node m_Device = m_Controls.m_ActiveVideoDevices->GetSelectedService(); if (m_Device.IsNull()){ m_Timer->stop(); return; } m_Device->Update(); m_Node->SetData(m_Device->GetOutput()); //start timer int interval = (1000 / m_Controls.m_FrameRate->value()); m_Timer->setInterval(interval); m_Timer->start(); //reinit view GlobalReinit(); //change UI elements m_Controls.m_BtnView->setText("Stop Viewing"); m_Controls.m_FrameRate->setEnabled(false); m_Controls.crop_left->setValue(m_Device->GetCropArea().cropLeft); m_Controls.crop_right->setValue(m_Device->GetCropArea().cropRight); m_Controls.crop_bot->setValue(m_Device->GetCropArea().cropBottom); m_Controls.crop_top->setValue(m_Device->GetCropArea().cropTop); m_Controls.crop_left->setEnabled(true); m_Controls.crop_right->setEnabled(true); m_Controls.crop_bot->setEnabled(true); m_Controls.crop_top->setEnabled(true); } else //deactivate imaging { //stop timer & release data m_Timer->stop(); m_Node->ReleaseData(); this->RequestRenderWindowUpdate(); //change UI elements m_Controls.m_BtnView->setText("Start Viewing"); m_Controls.m_FrameRate->setEnabled(true); m_Controls.crop_left->setEnabled(false); m_Controls.crop_right->setEnabled(false); m_Controls.crop_bot->setEnabled(false); m_Controls.crop_top->setEnabled(false); } } void UltrasoundSupport::OnNewDeviceWidgetDone() { m_Controls.m_NewVideoDeviceWidget->setVisible(false); m_Controls.m_DeviceManagerWidget->setVisible(true); m_Controls.m_AddDevice->setVisible(true); m_Controls.m_Headline->setText("Connected Devices:"); } void UltrasoundSupport::GlobalReinit() { // get all nodes that have not set "includeInBoundingBox" to false mitk::NodePredicateNot::Pointer pred = mitk::NodePredicateNot::New(mitk::NodePredicateProperty::New("includeInBoundingBox", mitk::BoolProperty::New(false))); mitk::DataStorage::SetOfObjects::ConstPointer rs = this->GetDataStorage()->GetSubset(pred); // calculate bounding geometry of these nodes mitk::TimeSlicedGeometry::Pointer bounds = this->GetDataStorage()->ComputeBoundingGeometry3D(rs, "visible"); // initialize the views to the bounding geometry mitk::RenderingManager::GetInstance()->InitializeViews(bounds); } UltrasoundSupport::UltrasoundSupport() { m_DevicePersistence = mitk::USDevicePersistence::New(); m_DevicePersistence->RestoreLastDevices(); } UltrasoundSupport::~UltrasoundSupport() { m_DevicePersistence->StoreCurrentDevices(); m_Controls.m_DeviceManagerWidget->DisconnectAllDevices(); -} \ No newline at end of file +}