diff --git a/Modules/ToFHardware/mitkToFImageGrabber.cpp b/Modules/ToFHardware/mitkToFImageGrabber.cpp index 5692364cbc..6d06f0afa6 100644 --- a/Modules/ToFHardware/mitkToFImageGrabber.cpp +++ b/Modules/ToFHardware/mitkToFImageGrabber.cpp @@ -1,377 +1,367 @@ /*=================================================================== 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 "mitkToFImageGrabber.h" #include "itkCommand.h" namespace mitk { ToFImageGrabber::ToFImageGrabber(): m_ToFCameraDevice(NULL), - m_CaptureWidth(204), - m_CaptureHeight(204), - m_PixelNumber(41616), + m_CaptureWidth(-1), + m_CaptureHeight(-1), + m_PixelNumber(-1), m_RGBImageWidth(0), m_RGBImageHeight(0), m_RGBPixelNumber(0), m_ImageSequence(0), m_SourceDataSize(0), m_IntensityArray(NULL), m_DistanceArray(NULL), m_AmplitudeArray(NULL), m_SourceDataArray(NULL), m_RgbDataArray(NULL), - m_DeviceObserverTag(), - m_DistanceImageInitialized(false), - m_IntensityImageInitialized(false), - m_AmplitudeImageInitialized(false), - m_RGBImageInitialized(false) + m_DeviceObserverTag() { // Create the output. We use static_cast<> here because we know the default // output must be of type TOutputImage OutputImageType::Pointer output0 = static_cast(this->MakeOutput(0).GetPointer()); OutputImageType::Pointer output1 = static_cast(this->MakeOutput(1).GetPointer()); OutputImageType::Pointer output2 = static_cast(this->MakeOutput(2).GetPointer()); OutputImageType::Pointer output3 = static_cast(this->MakeOutput(3).GetPointer()); mitk::ImageSource::SetNumberOfRequiredOutputs(3); mitk::ImageSource::SetNthOutput(0, output0.GetPointer()); mitk::ImageSource::SetNthOutput(1, output1.GetPointer()); mitk::ImageSource::SetNthOutput(2, output2.GetPointer()); mitk::ImageSource::SetNthOutput(3, output3.GetPointer()); } ToFImageGrabber::~ToFImageGrabber() { if (m_IntensityArray||m_AmplitudeArray||m_DistanceArray||m_RgbDataArray) { if (m_ToFCameraDevice) { m_ToFCameraDevice->RemoveObserver(m_DeviceObserverTag); } this->DisconnectCamera(); this->CleanUpImageArrays(); } } void ToFImageGrabber::GenerateData() { int requiredImageSequence = 0; // acquire new image data this->m_ToFCameraDevice->GetAllImages(this->m_DistanceArray, this->m_AmplitudeArray, this->m_IntensityArray, this->m_SourceDataArray, requiredImageSequence, this->m_ImageSequence, this->m_RgbDataArray ); mitk::Image::Pointer distanceImage = this->GetOutput(0); if (m_DistanceArray) { distanceImage->SetSlice(this->m_DistanceArray, 0, 0, 0); } bool hasAmplitudeImage = false; m_ToFCameraDevice->GetBoolProperty("HasAmplitudeImage", hasAmplitudeImage); if((hasAmplitudeImage) && (m_AmplitudeArray)) { mitk::Image::Pointer amplitudeImage = this->GetOutput(1); amplitudeImage->SetSlice(this->m_AmplitudeArray, 0, 0, 0); } bool hasIntensityImage = false; m_ToFCameraDevice->GetBoolProperty("HasIntensityImage", hasIntensityImage); if((hasIntensityImage) && (m_IntensityArray)) { mitk::Image::Pointer intensityImage = this->GetOutput(2); intensityImage->SetSlice(this->m_IntensityArray, 0, 0, 0); } bool hasRGBImage = false; m_ToFCameraDevice->GetBoolProperty("HasRGBImage", hasRGBImage); if( hasRGBImage ) { mitk::Image::Pointer rgbImage = this->GetOutput(3); if (m_RgbDataArray) { rgbImage->SetSlice(this->m_RgbDataArray, 0, 0, 0); } } } bool ToFImageGrabber::ConnectCamera() { bool ok = m_ToFCameraDevice->ConnectCamera(); if (ok) { this->m_CaptureWidth = this->m_ToFCameraDevice->GetCaptureWidth(); this->m_CaptureHeight = this->m_ToFCameraDevice->GetCaptureHeight(); this->m_PixelNumber = this->m_CaptureWidth * this->m_CaptureHeight; this->m_RGBImageWidth = this->m_ToFCameraDevice->GetRGBCaptureWidth(); this->m_RGBImageHeight = this->m_ToFCameraDevice->GetRGBCaptureHeight(); this->m_RGBPixelNumber = this->m_RGBImageWidth * this->m_RGBImageHeight; this->m_SourceDataSize = m_ToFCameraDevice->GetSourceDataSize(); this->AllocateImageArrays(); this->InitializeImages(); } return ok; } bool ToFImageGrabber::DisconnectCamera() { - bool success = m_ToFCameraDevice->DisconnectCamera(); - // reset initialized flag of outputs to allow reinitializing when using new device - m_DistanceImageInitialized = false; - m_IntensityImageInitialized = false; - m_AmplitudeImageInitialized = false; - m_RGBImageInitialized = false; - return success; + return m_ToFCameraDevice->DisconnectCamera(); } void ToFImageGrabber::StartCamera() { m_ToFCameraDevice->StartCamera(); } void ToFImageGrabber::StopCamera() { m_ToFCameraDevice->StopCamera(); } bool ToFImageGrabber::IsCameraActive() { return m_ToFCameraDevice->IsCameraActive(); } bool ToFImageGrabber::IsCameraConnected() { return m_ToFCameraDevice->IsCameraConnected(); } void ToFImageGrabber::SetCameraDevice(ToFCameraDevice* aToFCameraDevice) { m_ToFCameraDevice = aToFCameraDevice; itk::SimpleMemberCommand::Pointer modifiedCommand = itk::SimpleMemberCommand::New(); modifiedCommand->SetCallbackFunction(this, &ToFImageGrabber::OnToFCameraDeviceModified); m_DeviceObserverTag = m_ToFCameraDevice->AddObserver(itk::ModifiedEvent(), modifiedCommand); this->Modified(); } ToFCameraDevice* ToFImageGrabber::GetCameraDevice() { return m_ToFCameraDevice; } int ToFImageGrabber::GetCaptureWidth() { return m_CaptureWidth; } int ToFImageGrabber::GetCaptureHeight() { return m_CaptureHeight; } int ToFImageGrabber::GetPixelNumber() { return m_PixelNumber; } int ToFImageGrabber::GetRGBImageWidth() { return m_RGBImageWidth; } int ToFImageGrabber::GetRGBImageHeight() { return m_RGBImageHeight; } int ToFImageGrabber::GetRGBPixelNumber() { return m_RGBPixelNumber; } int ToFImageGrabber::SetModulationFrequency(int modulationFrequency) { this->m_ToFCameraDevice->SetProperty("ModulationFrequency",mitk::IntProperty::New(modulationFrequency)); this->Modified(); modulationFrequency = this->GetModulationFrequency(); // return the new valid modulation frequency from the camera return modulationFrequency; } int ToFImageGrabber::SetIntegrationTime(int integrationTime) { this->m_ToFCameraDevice->SetProperty("IntegrationTime",mitk::IntProperty::New(integrationTime)); this->Modified(); integrationTime = this->GetIntegrationTime(); // return the new valid integration time from the camera return integrationTime; } int ToFImageGrabber::GetIntegrationTime() { int integrationTime = 0; this->m_ToFCameraDevice->GetIntProperty("IntegrationTime",integrationTime); return integrationTime; } int ToFImageGrabber::GetModulationFrequency() { int modulationFrequency = 0; this->m_ToFCameraDevice->GetIntProperty("ModulationFrequency",modulationFrequency); return modulationFrequency; } void ToFImageGrabber::SetBoolProperty( const char* propertyKey, bool boolValue ) { SetProperty(propertyKey, mitk::BoolProperty::New(boolValue)); } void ToFImageGrabber::SetIntProperty( const char* propertyKey, int intValue ) { SetProperty(propertyKey, mitk::IntProperty::New(intValue)); } void ToFImageGrabber::SetFloatProperty( const char* propertyKey, float floatValue ) { SetProperty(propertyKey, mitk::FloatProperty::New(floatValue)); } void ToFImageGrabber::SetStringProperty( const char* propertyKey, const char* stringValue ) { SetProperty(propertyKey, mitk::StringProperty::New(stringValue)); } void ToFImageGrabber::SetProperty( const char *propertyKey, BaseProperty* propertyValue ) { this->m_ToFCameraDevice->SetProperty(propertyKey, propertyValue); } bool ToFImageGrabber::GetBoolProperty( const char* propertyKey) { mitk::BoolProperty::Pointer boolProp = dynamic_cast(GetProperty(propertyKey)); if(!boolProp) return false; return boolProp->GetValue(); } int ToFImageGrabber::GetIntProperty( const char* propertyKey) { mitk::IntProperty::Pointer intProp = dynamic_cast(GetProperty(propertyKey)); return intProp->GetValue(); } float ToFImageGrabber::GetFloatProperty( const char* propertyKey) { mitk::FloatProperty::Pointer floatProp = dynamic_cast(GetProperty(propertyKey)); return floatProp->GetValue(); } const char* ToFImageGrabber::GetStringProperty( const char* propertyKey) { mitk::StringProperty::Pointer stringProp = dynamic_cast(GetProperty(propertyKey)); return stringProp->GetValue(); } BaseProperty* ToFImageGrabber::GetProperty( const char *propertyKey) { return this->m_ToFCameraDevice->GetProperty(propertyKey); } void ToFImageGrabber::OnToFCameraDeviceModified() { this->Modified(); } void ToFImageGrabber::CleanUpImageArrays() { // free buffer if (m_IntensityArray) { delete [] m_IntensityArray; m_IntensityArray = NULL; } if (m_DistanceArray) { delete [] m_DistanceArray; m_DistanceArray = NULL; } if (m_AmplitudeArray) { delete [] m_AmplitudeArray; m_AmplitudeArray = NULL; } if (m_SourceDataArray) { delete [] m_SourceDataArray; m_SourceDataArray = NULL; } if (m_RgbDataArray) { delete [] m_RgbDataArray; m_RgbDataArray = NULL; } } void ToFImageGrabber::AllocateImageArrays() { // cleanup memory if necessary this->CleanUpImageArrays(); // allocate buffer m_IntensityArray = new float[m_PixelNumber]; m_DistanceArray = new float[m_PixelNumber]; m_AmplitudeArray = new float[m_PixelNumber]; m_SourceDataArray = new char[m_SourceDataSize]; m_RgbDataArray = new unsigned char[m_RGBPixelNumber*3]; } void ToFImageGrabber::InitializeImages() { unsigned int dimensions[3]; dimensions[0] = this->m_ToFCameraDevice->GetCaptureWidth(); dimensions[1] = this->m_ToFCameraDevice->GetCaptureHeight(); dimensions[2] = 1; mitk::PixelType FloatType = MakeScalarPixelType(); mitk::Image::Pointer distanceImage = this->GetOutput(); distanceImage->ReleaseData(); distanceImage->Initialize(FloatType, 3, dimensions, 1); bool hasAmplitudeImage = false; m_ToFCameraDevice->GetBoolProperty("HasAmplitudeImage", hasAmplitudeImage); if(hasAmplitudeImage) { mitk::Image::Pointer amplitudeImage = this->GetOutput(1); amplitudeImage->ReleaseData(); amplitudeImage->Initialize(FloatType, 3, dimensions, 1); } bool hasIntensityImage = false; m_ToFCameraDevice->GetBoolProperty("HasIntensityImage", hasIntensityImage); if(hasIntensityImage) { mitk::Image::Pointer intensityImage = this->GetOutput(2); intensityImage->ReleaseData(); intensityImage->Initialize(FloatType, 3, dimensions, 1); } bool hasRGBImage = false; m_ToFCameraDevice->GetBoolProperty("HasRGBImage", hasRGBImage); if(hasRGBImage) { unsigned int rgbDimension[3]; rgbDimension[0] = this->m_ToFCameraDevice->GetRGBCaptureWidth(); rgbDimension[1] = this->m_ToFCameraDevice->GetRGBCaptureHeight(); rgbDimension[2] = 1 ; mitk::Image::Pointer rgbImage = this->GetOutput(3); rgbImage->ReleaseData(); rgbImage->Initialize(mitk::PixelType(MakePixelType, 3>()), 3, rgbDimension,1); } } } diff --git a/Modules/ToFHardware/mitkToFImageGrabber.h b/Modules/ToFHardware/mitkToFImageGrabber.h index 8c1984e05f..8862703646 100644 --- a/Modules/ToFHardware/mitkToFImageGrabber.h +++ b/Modules/ToFHardware/mitkToFImageGrabber.h @@ -1,213 +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. ===================================================================*/ #ifndef __mitkToFImageGrabber_h #define __mitkToFImageGrabber_h #include #include "mitkCommon.h" #include "mitkImageSource.h" #include "mitkToFCameraDevice.h" #include "itkObject.h" #include "itkObjectFactory.h" namespace mitk { /**Documentation * \brief Image source providing ToF images. Interface for filters provided in ToFProcessing module * * This class internally holds a ToFCameraDevice to access the images acquired by a ToF camera. * * Provided images include: distance image (output 0), amplitude image (output 1), intensity image (output 2) * * \ingroup ToFHardware */ class MITK_TOFHARDWARE_EXPORT ToFImageGrabber : public mitk::ImageSource { public: mitkClassMacro( ToFImageGrabber , ImageSource ); itkFactorylessNewMacro(Self) itkCloneMacro(Self) void ShowDebugImage(float* distances); /*! \brief Establish a connection to the ToF camera */ virtual bool ConnectCamera(); /*! \brief Disconnects the ToF camera */ virtual bool DisconnectCamera(); /*! \brief Starts the continuous updating of the camera. A separate thread updates the source data, the main thread processes the source data and creates images and coordinates */ virtual void StartCamera(); /*! \brief Stops the continuous updating of the camera */ virtual void StopCamera(); /*! \brief Returns true if the camera is connected and started */ virtual bool IsCameraActive(); /*! \brief Returns true if the camera is connected */ virtual bool IsCameraConnected(); /*! \brief Sets the ToF device, the image grabber is grabbing the images from \param aToFCameraDevice device internally used for grabbing the images from the camera */ void SetCameraDevice(ToFCameraDevice* aToFCameraDevice); /*! \brief Get the currently set ToF camera device \return device currently used for grabbing images from the camera */ ToFCameraDevice* GetCameraDevice(); /*! \brief Set the modulation frequency used by the ToF camera. For default values see the corresponding device classes \param modulationFrequency modulation frequency in Hz */ int SetModulationFrequency(int modulationFrequency); /*! \brief Get the modulation frequency used by the ToF camera. \return modulation frequency in MHz */ int GetModulationFrequency(); /*! \brief Set the integration time used by the ToF camera. For default values see the corresponding device classes \param integrationTime integration time in ms */ int SetIntegrationTime(int integrationTime); /*! \brief Get the integration time in used by the ToF camera. \return integration time in ms */ int GetIntegrationTime(); /*! \brief Get the dimension in x direction of the ToF image \return width of the image */ int GetCaptureWidth(); /*! \brief Get the dimension in y direction of the ToF image \return height of the image */ int GetCaptureHeight(); /*! \brief Get the number of pixel in the ToF image \return number of pixel */ int GetPixelNumber(); /*! \brief Get the dimension in x direction of the ToF image \return width of the image */ int GetRGBImageWidth(); /*! \brief Get the dimension in y direction of the ToF image \return height of the image */ int GetRGBImageHeight(); /*! \brief Get the number of pixel in the ToF image \return number of pixel */ int GetRGBPixelNumber(); // properties void SetBoolProperty( const char* propertyKey, bool boolValue ); void SetIntProperty( const char* propertyKey, int intValue ); void SetFloatProperty( const char* propertyKey, float floatValue ); void SetStringProperty( const char* propertyKey, const char* stringValue ); void SetProperty( const char *propertyKey, BaseProperty* propertyValue ); bool GetBoolProperty( const char* propertyKey); int GetIntProperty( const char* propertyKey); float GetFloatProperty( const char* propertyKey); const char* GetStringProperty( const char* propertyKey); BaseProperty* GetProperty( const char *propertyKey); protected: /// /// called when the ToFCameraDevice was modified /// void OnToFCameraDeviceModified(); /*! \brief clean up memory allocated for the image arrays m_IntensityArray, m_DistanceArray, m_AmplitudeArray and m_SourceDataArray */ virtual void CleanUpImageArrays(); /*! \brief Allocate memory for the image arrays m_IntensityArray, m_DistanceArray, m_AmplitudeArray and m_SourceDataArray */ virtual void AllocateImageArrays(); - + /** + * @brief InitializeImages Initialze the geometries of the images according to the device properties. + */ void InitializeImages(); ToFCameraDevice::Pointer m_ToFCameraDevice; ///< Device allowing access to ToF image data int m_CaptureWidth; ///< Width of the captured ToF image int m_CaptureHeight; ///< Height of the captured ToF image int m_PixelNumber; ///< Number of pixels in the image int m_RGBImageWidth; ///< Width of the captured RGB image int m_RGBImageHeight; ///< Height of the captured RGB image int m_RGBPixelNumber; ///< Number of pixels in the RGB image int m_ImageSequence; ///< counter for currently acquired images int m_SourceDataSize; ///< size of the source data in bytes float* m_IntensityArray; ///< member holding the current intensity array float* m_DistanceArray; ///< member holding the current distance array float* m_AmplitudeArray; ///< member holding the current amplitude array char* m_SourceDataArray;///< member holding the current source data array unsigned char* m_RgbDataArray; ///< member holding the current rgb data array unsigned long m_DeviceObserverTag; ///< tag of the observer for the ToFCameraDevice - bool m_DistanceImageInitialized; ///< flag indicating whether the distance image is initialized or not - bool m_IntensityImageInitialized; ///< flag indicating whether the intensity image is initialized or not - bool m_AmplitudeImageInitialized; ///< flag indicating whether the amplitude image is initialized or not - bool m_RGBImageInitialized; ///< flag indicating whether the RGB image is initialized or not ToFImageGrabber(); ~ToFImageGrabber(); /*! \brief Method generating the outputs of this filter. Called in the updated process of the pipeline. 0: distance image 1: amplitude image 2: intensity image 3: RGB image */ void GenerateData(); private: }; } //END mitk namespace #endif