diff --git a/Modules/ToFHardware/Testing/mitkKinectControllerTest.cpp b/Modules/ToFHardware/Testing/mitkKinectControllerTest.cpp index 2d13ab73ae..a7cb121f6f 100644 --- a/Modules/ToFHardware/Testing/mitkKinectControllerTest.cpp +++ b/Modules/ToFHardware/Testing/mitkKinectControllerTest.cpp @@ -1,41 +1,40 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Language: C++ -Date: $Date: 2010-03-12 14:05:50 +0100 (Fr, 12 Mrz 2010) $ -Version: $Revision: 16010 $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 #include /**Documentation * test for the class "KinectController". */ int mitkKinectControllerTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("KinectController"); mitk::KinectController::Pointer testObject = mitk::KinectController::New(); MITK_TEST_CONDITION_REQUIRED(!testObject.GetPointer()==NULL,"Testing initialzation!"); MITK_TEST_CONDITION_REQUIRED(testObject->GetCaptureHeight()== 480 ,"Testing initialization of CaptureHeight"); MITK_TEST_CONDITION_REQUIRED(testObject->GetCaptureWidth()== 640 ,"Testing initialization of CaptureWidth"); MITK_TEST_CONDITION_REQUIRED(testObject->OpenCameraConnection(),"Testing opening of camera connection!"); MITK_TEST_CONDITION_REQUIRED(testObject->UpdateCamera(),"Testing UpdateCamera()"); MITK_TEST_CONDITION_REQUIRED(testObject->CloseCameraConnection(),"Testing closing of camera connection!"); MITK_TEST_END(); } diff --git a/Modules/ToFHardware/Testing/mitkKinectDeviceTest.cpp b/Modules/ToFHardware/Testing/mitkKinectDeviceTest.cpp index 28bcf40929..0e7be377bf 100644 --- a/Modules/ToFHardware/Testing/mitkKinectDeviceTest.cpp +++ b/Modules/ToFHardware/Testing/mitkKinectDeviceTest.cpp @@ -1,46 +1,45 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Language: C++ -Date: $Date: 2010-03-12 14:05:50 +0100 (Fr, 12 Mrz 2010) $ -Version: $Revision: 16010 $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 #include /**Documentation * test for the class "KinectDevice". */ int mitkKinectDeviceTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("KinectDevice"); mitk::KinectDevice::Pointer testObject = mitk::KinectDevice::New(); MITK_TEST_CONDITION_REQUIRED(!testObject.GetPointer()==NULL,"Testing initialzation!"); MITK_TEST_CONDITION_REQUIRED(testObject->ConnectCamera(),"Testing ConnectCamera()"); MITK_TEST_OUTPUT(<<"Testing StartCamera()"); testObject->StartCamera(); int captureHeight = testObject->GetCaptureHeight(); int captureWidth = testObject->GetCaptureWidth(); MITK_TEST_CONDITION_REQUIRED(captureHeight== 480 ,"Testing initialization of CaptureHeight"); MITK_TEST_CONDITION_REQUIRED(captureWidth== 640 ,"Testing initialization of CaptureWidth"); // MITK_TEST_OUTPUT(<<"Testing StopCamera()"); testObject->StopCamera(); MITK_TEST_CONDITION_REQUIRED(testObject->DisconnectCamera(),"Testing DisconnectCamera()"); MITK_TEST_END(); } diff --git a/Modules/ToFHardware/mitkKinectController.cpp b/Modules/ToFHardware/mitkKinectController.cpp index cf5bf9823a..d114c4f1e6 100644 --- a/Modules/ToFHardware/mitkKinectController.cpp +++ b/Modules/ToFHardware/mitkKinectController.cpp @@ -1,285 +1,283 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Module: $RCSfile$ -Language: C++ -Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ -Version: $Revision: $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 "mitkKinectController.h" #ifdef MITK_USE_TOF_KINECT #include #endif namespace mitk { class KinectController::KinectControllerPrivate { public: KinectControllerPrivate(); ~KinectControllerPrivate(); bool ErrorText(unsigned int error); #ifdef MITK_USE_TOF_KINECT xn::Context m_Context; ///< OpenNI context xn::DepthGenerator m_DepthGenerator; ///< Depth generator to access depth image of kinect xn::ImageGenerator m_ImageGenerator; ///< Image generator to access RGB image of kinect xn::IRGenerator m_IRGenerator; ///< IR generator to access IR image of kinect #endif bool m_ConnectionCheck; ///< check if camera is connected or not bool m_UseIR; ///< flag indicating whether IR image is used or not unsigned int m_CaptureWidth; ///< image width unsigned int m_CaptureHeight; ///< image height }; KinectController::KinectControllerPrivate::KinectControllerPrivate(): m_Context(NULL), m_DepthGenerator(NULL), m_ImageGenerator(NULL), m_IRGenerator(NULL), m_ConnectionCheck(false), m_UseIR(false), m_CaptureWidth(640), m_CaptureHeight(480) { } KinectController::KinectControllerPrivate::~KinectControllerPrivate() { } bool KinectController::KinectControllerPrivate::ErrorText(unsigned int error) { if(error != XN_STATUS_OK) { MITK_ERROR << "Camera Error " << xnGetStatusString(error); return false; } else return true; } KinectController::KinectController(): d(new KinectControllerPrivate) { } KinectController::~KinectController() { delete d; } bool KinectController::OpenCameraConnection() { if (!d->m_ConnectionCheck) { // Initialize the OpenNI status d->m_ConnectionCheck = !d->ErrorText(d->m_Context.Init()); // Create a depth generator and set its resolution XnMapOutputMode DepthMode; d->m_ConnectionCheck = !d->ErrorText(d->m_DepthGenerator.Create(d->m_Context)); d->m_DepthGenerator.GetMapOutputMode(DepthMode); DepthMode.nXRes = xn::Resolution((XnResolution)XN_RES_VGA).GetXResolution(); DepthMode.nYRes = xn::Resolution((XnResolution)XN_RES_VGA).GetYResolution(); d->m_ConnectionCheck = !d->ErrorText(d->m_DepthGenerator.SetMapOutputMode(DepthMode)); if (d->m_UseIR) { // Create the IR generator and set its resolution d->m_ConnectionCheck = !d->ErrorText(d->m_IRGenerator.Create(d->m_Context)); XnMapOutputMode IRMode; d->m_IRGenerator.GetMapOutputMode(IRMode); IRMode.nXRes = XN_VGA_X_RES; IRMode.nYRes = XN_VGA_Y_RES; IRMode.nFPS = 30; d->m_ConnectionCheck = !d->ErrorText(d->m_IRGenerator.SetMapOutputMode(IRMode)); } else { // Create an image generator and set its resolution XnMapOutputMode ImageMode; d->m_ConnectionCheck = !d->ErrorText(d->m_ImageGenerator.Create(d->m_Context)); d->m_ImageGenerator.GetMapOutputMode(ImageMode); ImageMode.nXRes = xn::Resolution((XnResolution)XN_RES_VGA).GetXResolution(); ImageMode.nYRes = xn::Resolution((XnResolution)XN_RES_VGA).GetYResolution(); d->m_ConnectionCheck = !d->ErrorText(d->m_ImageGenerator.SetMapOutputMode(ImageMode)); } // Camera registration if ( d->m_DepthGenerator.IsCapabilitySupported(XN_CAPABILITY_ALTERNATIVE_VIEW_POINT) ) { if (d->m_UseIR) { d->m_ConnectionCheck = !d->ErrorText(d->m_DepthGenerator.GetAlternativeViewPointCap().SetViewPoint(d->m_IRGenerator)); } else { d->m_ConnectionCheck = !d->ErrorText(d->m_DepthGenerator.GetAlternativeViewPointCap().SetViewPoint(d->m_ImageGenerator)); } } else { std::cout << "Alternative view point not supported by the depth generator..." << std::endl; } if (d->m_UseIR) { if ( d->m_IRGenerator.IsCapabilitySupported(XN_CAPABILITY_ALTERNATIVE_VIEW_POINT) ) { d->m_ConnectionCheck = !d->ErrorText(d->m_IRGenerator.GetAlternativeViewPointCap().SetViewPoint(d->m_DepthGenerator)); } else { std::cout << "Alternative view point not supported by the depth generator..." << std::endl; } } // Mirror data d->m_ConnectionCheck = d->ErrorText(d->m_Context.SetGlobalMirror(!d->m_Context.GetGlobalMirror())); // Start data generation d->m_ConnectionCheck = d->ErrorText(d->m_Context.StartGeneratingAll()); // // Update the connected flag // d->m_ConnectionCheck = true; } return d->m_ConnectionCheck; } bool KinectController::CloseCameraConnection() { d->m_ConnectionCheck = !d->ErrorText(d->m_Context.StopGeneratingAll()); return !d->m_ConnectionCheck; } bool KinectController::UpdateCamera() { bool updateSuccessful = d->ErrorText(d->m_Context.WaitAndUpdateAll()); xn::DepthMetaData DepthMD; d->m_DepthGenerator.GetMetaData(DepthMD); d->m_CaptureWidth = DepthMD.XRes(); d->m_CaptureHeight = DepthMD.YRes(); return updateSuccessful; } // TODO flag image void KinectController::GetDistances(float* distances) { xn::DepthMetaData DepthMD; d->m_DepthGenerator.GetMetaData(DepthMD); const XnDepthPixel* DepthData = DepthMD.Data(); for (unsigned int i=0; im_CaptureWidth*d->m_CaptureHeight; i++) { distances[i] = DepthData[i]; } } void KinectController::GetRgb(unsigned char* rgb) { if (!d->m_UseIR) { xn::ImageMetaData ImageMD; d->m_ImageGenerator.GetMetaData(ImageMD); const XnRGB24Pixel* rgbPixelArray = ImageMD.RGB24Data(); for (int i=0; im_CaptureWidth*d->m_CaptureHeight; i++) { rgb[i*3] = rgbPixelArray[i].nRed; rgb[i*3+1] = rgbPixelArray[i].nGreen; rgb[i*3+2] = rgbPixelArray[i].nBlue; } } } void KinectController::GetAllData(float* distances, float* amplitudes, unsigned char* rgb) { // get current distance data xn::DepthMetaData DepthMD; d->m_DepthGenerator.GetMetaData(DepthMD); const XnDepthPixel* DepthData = DepthMD.Data(); // IR data xn::IRMetaData IRData; const XnIRPixel* IRPixelData; // Image data xn::ImageMetaData ImageMD; const XnRGB24Pixel* rgbPixelArray; if (d->m_UseIR) { d->m_IRGenerator.GetMetaData(IRData); IRPixelData = IRData.Data(); } else { // get current rgb data d->m_ImageGenerator.GetMetaData(ImageMD); rgbPixelArray = ImageMD.RGB24Data(); } for (unsigned int i=0; im_CaptureWidth*d->m_CaptureHeight; i++) { distances[i] = DepthData[i]; if (d->m_UseIR) { amplitudes[i] = IRPixelData[i]; } else { rgb[i*3] = rgbPixelArray[i].nRed; rgb[i*3+1] = rgbPixelArray[i].nGreen; rgb[i*3+2] = rgbPixelArray[i].nBlue; } } } void KinectController::GetAmplitudes( float* amplitudes ) { if (d->m_UseIR) { xn::IRMetaData IRData; d->m_IRGenerator.GetMetaData(IRData); const XnIRPixel* IRPixelData = IRData.Data(); for (unsigned int i=0; im_CaptureWidth*d->m_CaptureHeight; i++) { amplitudes[i] = IRPixelData[i]; } } } void KinectController::GetIntensities( float* intensities ) { } unsigned int KinectController::GetCaptureWidth() const { return d->m_CaptureWidth; } unsigned int KinectController::GetCaptureHeight() const { return d->m_CaptureHeight; } bool KinectController::GetUseIR() const { return d->m_UseIR; } void KinectController::SetUseIR(bool useIR) { if (d->m_UseIR!=useIR) { d->m_UseIR = useIR; this->Modified(); } } } diff --git a/Modules/ToFHardware/mitkKinectController.h b/Modules/ToFHardware/mitkKinectController.h index 8f9745f98f..653912d291 100644 --- a/Modules/ToFHardware/mitkKinectController.h +++ b/Modules/ToFHardware/mitkKinectController.h @@ -1,93 +1,91 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Module: $RCSfile$ -Language: C++ -Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ -Version: $Revision: $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 __mitkKinectController_h #define __mitkKinectController_h #include "mitkToFHardwareExports.h" #include "mitkCommon.h" #include "mitkToFConfig.h" #include "itkObject.h" #include "itkObjectFactory.h" namespace mitk { /** * @brief Interface to the Kinect camera * * * @ingroup ToFHardware */ class MITK_TOFHARDWARE_EXPORT KinectController : public itk::Object { public: mitkClassMacro( KinectController , itk::Object ); itkNewMacro( Self ); unsigned int GetCaptureWidth() const; unsigned int GetCaptureHeight() const; bool GetUseIR() const; void SetUseIR(bool useIR); /*! \brief opens a connection to the Kinect camera. */ virtual bool OpenCameraConnection(); /*! \brief closes the connection to the camera */ virtual bool CloseCameraConnection(); /*! \brief updates the camera. The update function of the hardware interface is called only when new data is available */ virtual bool UpdateCamera(); /*! \brief acquire new distance data from the Kinect camera \param distances pointer to memory location where distances should be stored */ void GetDistances(float* distances); void GetAmplitudes(float* amplitudes); void GetIntensities(float* intensities); /*! \brief acquire new rgb data from the Kinect camera \parama rgb pointer to memory location where rgb information should be stored */ void GetRgb(unsigned char* rgb); /*! \brief convenience method for faster access to distance and rgb data \param distances pointer to memory location where distances should be stored \param rgb pointer to memory location where rgb information should be stored */ void GetAllData(float* distances, float* amplitudes, unsigned char* rgb); protected: KinectController(); ~KinectController(); private: class KinectControllerPrivate; KinectControllerPrivate *d; }; } //END mitk namespace #endif diff --git a/Modules/ToFHardware/mitkKinectControllerStub.cpp b/Modules/ToFHardware/mitkKinectControllerStub.cpp index 9e6bff68d3..b10a4d69e9 100644 --- a/Modules/ToFHardware/mitkKinectControllerStub.cpp +++ b/Modules/ToFHardware/mitkKinectControllerStub.cpp @@ -1,98 +1,96 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Module: $RCSfile$ -Language: C++ -Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ -Version: $Revision: $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 "mitkKinectController.h" namespace mitk { KinectController::KinectController() { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } KinectController::~KinectController() { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } bool KinectController::OpenCameraConnection() { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; return true; } bool KinectController::CloseCameraConnection() { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; return true; } bool KinectController::UpdateCamera() { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; return true; } // TODO flag image void KinectController::GetDistances(float* distances) { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } void KinectController::GetRgb(unsigned char* rgb) { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } void KinectController::GetAllData(float* distances, float* amplitudes, unsigned char* rgb) { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } void KinectController::GetAmplitudes( float* amplitudes ) { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } void KinectController::GetIntensities( float* intensities ) { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } unsigned int KinectController::GetCaptureWidth() const { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; return 0; } unsigned int KinectController::GetCaptureHeight() const { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; return 0; } bool KinectController::GetUseIR() const { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; return true; } void KinectController::SetUseIR(bool useIR) { MITK_WARN("ToF") << "Error: Kinect camera currently not available"; } } diff --git a/Modules/ToFHardware/mitkKinectDevice.cpp b/Modules/ToFHardware/mitkKinectDevice.cpp index fc19087c8c..c8f88e2326 100644 --- a/Modules/ToFHardware/mitkKinectDevice.cpp +++ b/Modules/ToFHardware/mitkKinectDevice.cpp @@ -1,426 +1,424 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Module: $RCSfile$ -Language: C++ -Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ -Version: $Revision: $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 "mitkKinectDevice.h" #include "mitkRealTimeClock.h" #include "itkMultiThreader.h" #include namespace mitk { KinectDevice::KinectDevice() { m_Controller = mitk::KinectController::New(); } KinectDevice::~KinectDevice() { } bool KinectDevice::ConnectCamera() { bool ok = false; if (m_Controller) { ok = m_Controller->OpenCameraConnection(); if (ok) { this->m_CaptureWidth = m_Controller->GetCaptureWidth(); this->m_CaptureHeight = m_Controller->GetCaptureHeight(); this->m_PixelNumber = this->m_CaptureWidth * this->m_CaptureHeight; // allocate buffer this->m_IntensityArray = new float[this->m_PixelNumber]; for(int i=0; im_PixelNumber; i++) {this->m_IntensityArray[i]=0.0;} this->m_DistanceArray = new float[this->m_PixelNumber]; for(int i=0; im_PixelNumber; i++) {this->m_DistanceArray[i]=0.0;} this->m_AmplitudeArray = new float[this->m_PixelNumber]; for(int i=0; im_PixelNumber; i++) {this->m_AmplitudeArray[i]=0.0;} this->m_DistanceDataBuffer = new float*[this->m_MaxBufferSize]; for(int i=0; im_MaxBufferSize; i++) { this->m_DistanceDataBuffer[i] = new float[this->m_PixelNumber]; } this->m_AmplitudeDataBuffer = new float*[this->m_MaxBufferSize]; for(int i=0; im_MaxBufferSize; i++) { this->m_AmplitudeDataBuffer[i] = new float[this->m_PixelNumber]; } this->m_IntensityDataBuffer = new float*[this->m_MaxBufferSize]; for(int i=0; im_MaxBufferSize; i++) { this->m_IntensityDataBuffer[i] = new float[this->m_PixelNumber]; } this->m_RGBDataBuffer = new unsigned char*[this->m_MaxBufferSize]; for (int i=0; im_MaxBufferSize; i++) { this->m_RGBDataBuffer[i] = new unsigned char[this->m_PixelNumber*3]; } m_CameraConnected = true; } } return ok; } bool KinectDevice::DisconnectCamera() { bool ok = false; if (m_Controller) { ok = m_Controller->CloseCameraConnection(); // clean-up only if camera was connected if (m_CameraConnected) { delete [] m_IntensityArray; delete [] m_DistanceArray; delete [] m_AmplitudeArray; for(int i=0; im_MaxBufferSize; i++) { delete[] this->m_DistanceDataBuffer[i]; delete[] this->m_AmplitudeDataBuffer[i]; delete[] this->m_IntensityDataBuffer[i]; delete[] this->m_RGBDataBuffer[i]; } delete[] this->m_DistanceDataBuffer; delete[] this->m_AmplitudeDataBuffer; delete[] this->m_IntensityDataBuffer; delete[] this->m_RGBDataBuffer; m_CameraConnected = false; } } return ok; } void KinectDevice::StartCamera() { if (m_CameraConnected) { // get the first image this->m_Controller->UpdateCamera(); this->m_ImageMutex->Lock(); this->m_Controller->GetAllData(this->m_DistanceDataBuffer[this->m_FreePos],this->m_AmplitudeDataBuffer[this->m_FreePos],this->m_RGBDataBuffer[this->m_FreePos]); this->m_FreePos = (this->m_FreePos+1) % this->m_BufferSize; this->m_CurrentPos = (this->m_CurrentPos+1) % this->m_BufferSize; this->m_ImageSequence++; this->m_ImageMutex->Unlock(); this->m_CameraActiveMutex->Lock(); this->m_CameraActive = true; this->m_CameraActiveMutex->Unlock(); this->m_ThreadID = this->m_MultiThreader->SpawnThread(this->Acquire, this); // wait a little to make sure that the thread is started itksys::SystemTools::Delay(10); } else { MITK_INFO<<"Camera not connected"; } } void KinectDevice::StopCamera() { m_CameraActiveMutex->Lock(); m_CameraActive = false; m_CameraActiveMutex->Unlock(); itksys::SystemTools::Delay(100); if (m_MultiThreader.IsNotNull()) { m_MultiThreader->TerminateThread(m_ThreadID); } // wait a little to make sure that the thread is terminated itksys::SystemTools::Delay(10); } bool KinectDevice::IsCameraActive() { m_CameraActiveMutex->Lock(); bool ok = m_CameraActive; m_CameraActiveMutex->Unlock(); return ok; } void KinectDevice::UpdateCamera() { if (m_Controller) { m_Controller->UpdateCamera(); } } ITK_THREAD_RETURN_TYPE KinectDevice::Acquire(void* pInfoStruct) { /* extract this pointer from Thread Info structure */ struct itk::MultiThreader::ThreadInfoStruct * pInfo = (struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct; if (pInfo == NULL) { return ITK_THREAD_RETURN_VALUE; } if (pInfo->UserData == NULL) { return ITK_THREAD_RETURN_VALUE; } KinectDevice* toFCameraDevice = (KinectDevice*)pInfo->UserData; if (toFCameraDevice!=NULL) { mitk::RealTimeClock::Pointer realTimeClock; realTimeClock = mitk::RealTimeClock::New(); double t1, t2; t1 = realTimeClock->GetCurrentStamp(); int n = 100; bool overflow = false; bool printStatus = false; while (toFCameraDevice->IsCameraActive()) { // update the ToF camera toFCameraDevice->UpdateCamera(); // get the image data from the camera and write it at the next free position in the buffer toFCameraDevice->m_ImageMutex->Lock(); toFCameraDevice->m_Controller->GetAllData(toFCameraDevice->m_DistanceDataBuffer[toFCameraDevice->m_FreePos],toFCameraDevice->m_AmplitudeDataBuffer[toFCameraDevice->m_FreePos],toFCameraDevice->m_RGBDataBuffer[toFCameraDevice->m_FreePos]); toFCameraDevice->m_ImageMutex->Unlock(); // call modified to indicate that cameraDevice was modified toFCameraDevice->Modified(); /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TODO Buffer Handling currently only works for buffer size 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ //toFCameraDevice->m_ImageSequence++; toFCameraDevice->m_FreePos = (toFCameraDevice->m_FreePos+1) % toFCameraDevice->m_BufferSize; toFCameraDevice->m_CurrentPos = (toFCameraDevice->m_CurrentPos+1) % toFCameraDevice->m_BufferSize; toFCameraDevice->m_ImageSequence++; if (toFCameraDevice->m_FreePos == toFCameraDevice->m_CurrentPos) { // buffer overflow //MITK_INFO << "Buffer overflow!! "; //toFCameraDevice->m_CurrentPos = (toFCameraDevice->m_CurrentPos+1) % toFCameraDevice->m_BufferSize; //toFCameraDevice->m_ImageSequence++; overflow = true; } if (toFCameraDevice->m_ImageSequence % n == 0) { printStatus = true; } if (overflow) { //itksys::SystemTools::Delay(10); overflow = false; } /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! END TODO Buffer Handling currently only works for buffer size 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ // print current framerate if (printStatus) { t2 = realTimeClock->GetCurrentStamp() - t1; //MITK_INFO << "t2: " << t2 <<" Time (s) for 1 image: " << (t2/1000) / n << " Framerate (fps): " << n / (t2/1000) << " Sequence: " << toFCameraDevice->m_ImageSequence; MITK_INFO << " Framerate (fps): " << n / (t2/1000) << " Sequence: " << toFCameraDevice->m_ImageSequence; t1 = realTimeClock->GetCurrentStamp(); printStatus = false; } } // end of while loop } return ITK_THREAD_RETURN_VALUE; } // TODO: Buffer size currently set to 1. Once Buffer handling is working correctly, method may be reactivated // void KinectDevice::ResetBuffer(int bufferSize) // { // this->m_BufferSize = bufferSize; // this->m_CurrentPos = -1; // this->m_FreePos = 0; // } void KinectDevice::GetAmplitudes(float* amplitudeArray, int& imageSequence) { m_ImageMutex->Lock(); if (m_CameraActive) { // 1) copy the image buffer // 2) Flip around y- axis (vertical axis) /* this->m_Controller->GetAmplitudes(this->m_SourceDataBuffer[this->m_CurrentPos], this->m_AmplitudeArray); for (int i=0; im_CaptureHeight; i++) { for (int j=0; jm_CaptureWidth; j++) { amplitudeArray[i*this->m_CaptureWidth+j] = this->m_AmplitudeArray[(i+1)*this->m_CaptureWidth-1-j]; } } */ for (int i=0; im_PixelNumber; i++) { amplitudeArray[i] = this->m_AmplitudeDataBuffer[this->m_CurrentPos][i]; } imageSequence = this->m_ImageSequence; } else { MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; } m_ImageMutex->Unlock(); } void KinectDevice::GetIntensities(float* intensityArray, int& imageSequence) { m_ImageMutex->Lock(); if (m_CameraActive) { // 1) copy the image buffer // 2) Flip around y- axis (vertical axis) /* this->m_Controller->GetIntensities(this->m_SourceDataBuffer[this->m_CurrentPos], this->m_IntensityArray); for (int i=0; im_CaptureHeight; i++) { for (int j=0; jm_CaptureWidth; j++) { intensityArray[i*this->m_CaptureWidth+j] = this->m_IntensityArray[(i+1)*this->m_CaptureWidth-1-j]; } } */ for (int i=0; im_PixelNumber; i++) { intensityArray[i] = this->m_IntensityDataBuffer[this->m_CurrentPos][i]; } imageSequence = this->m_ImageSequence; } else { MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; } m_ImageMutex->Unlock(); } void KinectDevice::GetDistances(float* distanceArray, int& imageSequence) { m_ImageMutex->Lock(); if (m_CameraActive) { // 1) copy the image buffer // 2) convert the distance values from m to mm // 3) Flip around y- axis (vertical axis) /* this->m_Controller->GetDistances(this->m_SourceDataBuffer[this->m_CurrentPos], this->m_DistanceArray); for (int i=0; im_CaptureHeight; i++) { for (int j=0; jm_CaptureWidth; j++) { distanceArray[i*this->m_CaptureWidth+j] = 1000 * this->m_DistanceArray[(i+1)*this->m_CaptureWidth-1-j]; } } */ for (int i=0; im_PixelNumber; i++) { distanceArray[i] = this->m_DistanceDataBuffer[this->m_CurrentPos][i]; // * 1000 } imageSequence = this->m_ImageSequence; } else { MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; } m_ImageMutex->Unlock(); } void KinectDevice::GetAllImages(float* distanceArray, float* amplitudeArray, float* intensityArray, char* sourceDataArray, int requiredImageSequence, int& capturedImageSequence, unsigned char* rgbDataArray) { if (m_CameraActive) { // 1) copy the image buffer // 2) convert the distance values from m to mm // 3) Flip around y- axis (vertical axis) // check for empty buffer if (this->m_ImageSequence < 0) { // buffer empty MITK_INFO << "Buffer empty!! "; capturedImageSequence = this->m_ImageSequence; return; } // determine position of image in buffer int pos = 0; if ((requiredImageSequence < 0) || (requiredImageSequence > this->m_ImageSequence)) { capturedImageSequence = this->m_ImageSequence; pos = this->m_CurrentPos; //MITK_INFO << "Required image not found! Required: " << requiredImageSequence << " delivered/current: " << this->m_ImageSequence; } else if (requiredImageSequence <= this->m_ImageSequence - this->m_BufferSize) { capturedImageSequence = (this->m_ImageSequence - this->m_BufferSize) + 1; pos = (this->m_CurrentPos + 1) % this->m_BufferSize; //MITK_INFO << "Out of buffer! Required: " << requiredImageSequence << " delivered: " << capturedImageSequence << " current: " << this->m_ImageSequence; } else // (requiredImageSequence > this->m_ImageSequence - this->m_BufferSize) && (requiredImageSequence <= this->m_ImageSequence) { capturedImageSequence = requiredImageSequence; pos = (this->m_CurrentPos + (10-(this->m_ImageSequence - requiredImageSequence))) % this->m_BufferSize; } // write image data to float arrays for (int i=0; im_PixelNumber; i++) { distanceArray[i] = this->m_DistanceDataBuffer[pos][i]; amplitudeArray[i] = this->m_AmplitudeDataBuffer[pos][i]; intensityArray[i] = this->m_IntensityDataBuffer[pos][i]; rgbDataArray[i*3] = this->m_RGBDataBuffer[pos][i*3]; rgbDataArray[i*3+1] = this->m_RGBDataBuffer[pos][i*3+1]; rgbDataArray[i*3+2] = this->m_RGBDataBuffer[pos][i*3+2]; } } else { MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; } } KinectController::Pointer KinectDevice::GetController() { return this->m_Controller; } void KinectDevice::SetProperty( const char *propertyKey, BaseProperty* propertyValue ) { ToFCameraDevice::SetProperty(propertyKey,propertyValue); this->m_PropertyList->SetProperty(propertyKey, propertyValue); if (strcmp(propertyKey, "RGB") == 0) { bool rgb = false; GetBoolProperty(propertyKey, rgb); m_Controller->SetUseIR(!rgb); } else if (strcmp(propertyKey, "IR") == 0) { bool ir = false; GetBoolProperty(propertyKey, ir); m_Controller->SetUseIR(ir); } } } diff --git a/Modules/ToFHardware/mitkKinectDevice.h b/Modules/ToFHardware/mitkKinectDevice.h index 3a3276ab09..31a5960f3d 100644 --- a/Modules/ToFHardware/mitkKinectDevice.h +++ b/Modules/ToFHardware/mitkKinectDevice.h @@ -1,149 +1,147 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Module: $RCSfile$ -Language: C++ -Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ -Version: $Revision: $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 __mitkKinectDevice_h #define __mitkKinectDevice_h #include "mitkToFHardwareExports.h" #include "mitkCommon.h" #include "mitkToFCameraDevice.h" #include "mitkKinectController.h" #include "itkObject.h" #include "itkObjectFactory.h" #include "itkMultiThreader.h" #include "itkFastMutexLock.h" namespace mitk { /** * @brief Interface for all representations of MESA ToF devices. * KinectDevice internally holds an instance of KinectController and starts a thread * that continuously grabs images from the controller. A buffer structure buffers the last acquired images * to provide the image data loss-less. * * @ingroup ToFHardware */ class MITK_TOFHARDWARE_EXPORT KinectDevice : public ToFCameraDevice { public: mitkClassMacro( KinectDevice , ToFCameraDevice ); itkNewMacro( Self ); /*! \brief opens a connection to the ToF camera */ virtual bool ConnectCamera(); /*! \brief closes the connection to the 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 updates the camera for image acquisition */ virtual void UpdateCamera(); /*! \brief returns whether the camera is currently active or not */ virtual bool IsCameraActive(); /*! \brief gets the amplitude data from the ToF camera as the strength of the active illumination of every pixel. Caution! The user is responsible for allocating and deleting the images. These values can be used to determine the quality of the distance values. The higher the amplitude value, the higher the accuracy of the according distance value \param imageSequence the actually captured image sequence number \param amplitudeArray contains the returned amplitude data as an array. */ virtual void GetAmplitudes(float* amplitudeArray, int& imageSequence); /*! \brief gets the intensity data from the ToF camera as a greyscale image. Caution! The user is responsible for allocating and deleting the images. \param intensityArray contains the returned intensities data as an array. \param imageSequence the actually captured image sequence number */ virtual void GetIntensities(float* intensityArray, int& imageSequence); /*! \brief gets the distance data from the ToF camera measuring the distance between the camera and the different object points in millimeters. Caution! The user is responsible for allocating and deleting the images. \param distanceArray contains the returned distances data as an array. \param imageSequence the actually captured image sequence number */ virtual void GetDistances(float* distanceArray, int& imageSequence); /*! \brief gets the 3 images (distance, amplitude, intensity) from the ToF camera. Caution! The user is responsible for allocating and deleting the images. \param distanceArray contains the returned distance data as an array. \param amplitudeArray contains the returned amplitude data as an array. \param intensityArray contains the returned intensity data as an array. \param sourceDataArray contains the complete source data from the camera device. \param requiredImageSequence the required image sequence number \param capturedImageSequence the actually captured image sequence number */ virtual void GetAllImages(float* distanceArray, float* amplitudeArray, float* intensityArray, char* sourceDataArray, int requiredImageSequence, int& capturedImageSequence, unsigned char* rgbDataArray=NULL); // TODO: Buffer size currently set to 1. Once Buffer handling is working correctly, method may be reactivated // /*! // \brief pure virtual method resetting the buffer using the specified bufferSize. Has to be implemented by sub-classes // \param bufferSize buffer size the buffer should be reset to // */ // virtual void ResetBuffer(int bufferSize) = 0; //TODO add/correct documentation for requiredImageSequence and capturedImageSequence in the GetAllImages, GetDistances, GetIntensities and GetAmplitudes methods. /*! \brief returns the corresponding camera controller */ KinectController::Pointer GetController(); /*! \brief set a BaseProperty */ virtual void SetProperty( const char *propertyKey, BaseProperty* propertyValue ); protected: KinectDevice(); ~KinectDevice(); /*! \brief Thread method continuously acquiring images from the ToF hardware */ static ITK_THREAD_RETURN_TYPE Acquire(void* pInfoStruct); /*! \brief moves the position pointer m_CurrentPos to the next position in the buffer if that's not the next free position to prevent reading from an empty buffer */ void GetNextPos(); KinectController::Pointer m_Controller; ///< corresponding CameraController float** m_DistanceDataBuffer; ///< buffer holding the last distance images float** m_AmplitudeDataBuffer; ///< buffer holding the last amplitude images float** m_IntensityDataBuffer; ///< buffer holding the last intensity images unsigned char** m_RGBDataBuffer; ///< buffer holding the last RGB image private: }; } //END mitk namespace #endif diff --git a/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.cpp b/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.cpp index f36ea96cad..0f6c5314fd 100644 --- a/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.cpp +++ b/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.cpp @@ -1,90 +1,88 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Module: $RCSfile$ -Language: C++ -Date: $Date: 2009-05-20 13:35:09 +0200 (Mi, 20 Mai 2009) $ -Version: $Revision: 17332 $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 const std::string QmitkKinectParameterWidget::VIEW_ID = "org.mitk.views.qmitkkinectparameterwidget"; QmitkKinectParameterWidget::QmitkKinectParameterWidget(QWidget* p, Qt::WindowFlags f): QWidget(p, f) { m_ToFImageGrabber = NULL; m_Controls = NULL; CreateQtPartControl(this); } QmitkKinectParameterWidget::~QmitkKinectParameterWidget() { } void QmitkKinectParameterWidget::CreateQtPartControl(QWidget *parent) { if (!m_Controls) { // create GUI widgets m_Controls = new Ui::QmitkKinectParameterWidgetControls; m_Controls->setupUi(parent); this->CreateConnections(); } } void QmitkKinectParameterWidget::CreateConnections() { if ( m_Controls ) { connect( m_Controls->m_RGB, SIGNAL(toggled(bool)), this, SLOT(OnAcquisitionModeChanged()) ); connect( m_Controls->m_IR, SIGNAL(toggled(bool)), this, SLOT(OnAcquisitionModeChanged()) ); } } mitk::ToFImageGrabber* QmitkKinectParameterWidget::GetToFImageGrabber() { return this->m_ToFImageGrabber; } void QmitkKinectParameterWidget::SetToFImageGrabber(mitk::ToFImageGrabber* aToFImageGrabber) { this->m_ToFImageGrabber = aToFImageGrabber; } void QmitkKinectParameterWidget::ActivateAllParameters() { this->OnAcquisitionModeChanged(); } void QmitkKinectParameterWidget::OnAcquisitionModeChanged() { if (m_ToFImageGrabber.IsNotNull()) { // stop camera if active bool active = m_ToFImageGrabber->IsCameraActive(); if (active) { m_ToFImageGrabber->StopCamera(); m_ToFImageGrabber->DisconnectCamera(); } this->m_ToFImageGrabber->SetBoolProperty("RGB", m_Controls->m_RGB->isChecked()); this->m_ToFImageGrabber->SetBoolProperty("IR", m_Controls->m_IR->isChecked()); if (active) { m_ToFImageGrabber->ConnectCamera(); m_ToFImageGrabber->StartCamera(); } } } diff --git a/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.h b/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.h index d46b43b63b..789fc989ae 100644 --- a/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.h +++ b/Modules/ToFUI/Qmitk/QmitkKinectParameterWidget.h @@ -1,80 +1,78 @@ -/*========================================================================= +/*=================================================================== -Program: Medical Imaging & Interaction Toolkit -Module: $RCSfile$ -Language: C++ -Date: $Date: 2009-05-20 13:35:09 +0200 (Mi, 20 Mai 2009) $ -Version: $Revision: 17332 $ +The Medical Imaging Interaction Toolkit (MITK) -Copyright (c) German Cancer Research Center, Division of Medical and -Biological Informatics. All rights reserved. -See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. +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 the above copyright notices for more information. +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 _QMITKKINECTPARAMETERWIDGET_H_INCLUDED #define _QMITKKINECTPARAMETERWIDGET_H_INCLUDED #include "mitkTOFUIExports.h" #include "ui_QmitkKinectParameterWidgetControls.h" //mitk headers #include #include /** * @brief Widget for configuring the Kinect device * * @ingroup ToFUI */ class mitkTOFUI_EXPORT QmitkKinectParameterWidget :public QWidget { //this is needed for all Qt objects that should have a MOC object (everything that derives from QObject) Q_OBJECT public: static const std::string VIEW_ID; QmitkKinectParameterWidget(QWidget* p = 0, Qt::WindowFlags f = 0); virtual ~QmitkKinectParameterWidget(); /* @brief This method is part of the widget an needs not to be called seperately. */ virtual void CreateQtPartControl(QWidget *parent); /* @brief This method is part of the widget an needs not to be called seperately. (Creation of the connections of main and control widget.)*/ virtual void CreateConnections(); /*! \brief returns the ToFImageGrabber \return ToFImageGrabber currently used by the widget */ mitk::ToFImageGrabber* GetToFImageGrabber(); /*! \brief sets the ToFImageGrabber */ void SetToFImageGrabber(mitk::ToFImageGrabber* aToFImageGrabber); /*! \brief activate camera settings according to the parameters from GUI */ void ActivateAllParameters(); protected slots: void OnAcquisitionModeChanged(); protected: Ui::QmitkKinectParameterWidgetControls* m_Controls; ///< member holding the UI elements of this widget mitk::ToFImageGrabber::Pointer m_ToFImageGrabber; ///< image grabber object to be configured by the widget private: }; #endif // _QmitkKinectParameterWIDGET_H_INCLUDED