From be51a114bca4f3f43ed88001cdc510ccf246079f Mon Sep 17 00:00:00 2001 From: Stéphane Lenclud Date: Tue, 16 Apr 2019 18:01:57 +0200 Subject: Easy Tracker: Mostly code clean-up. --- tracker-kinect-face/camera_kinect_ir.cpp | 332 +++++++++++++++---------------- 1 file changed, 165 insertions(+), 167 deletions(-) (limited to 'tracker-kinect-face/camera_kinect_ir.cpp') diff --git a/tracker-kinect-face/camera_kinect_ir.cpp b/tracker-kinect-face/camera_kinect_ir.cpp index e170b15a..b881d51f 100644 --- a/tracker-kinect-face/camera_kinect_ir.cpp +++ b/tracker-kinect-face/camera_kinect_ir.cpp @@ -10,7 +10,7 @@ #ifdef OTR_HAVE_OPENCV -//#include "frame.hpp" + //#include "frame.hpp" #include "compat/sleep.hpp" #include "compat/math-imports.hpp" @@ -28,7 +28,7 @@ namespace Kinect { { if (name.compare(KKinectIRSensor) == 0) { - return std::make_unique(); + return std::make_unique(); } return nullptr; @@ -49,239 +49,237 @@ namespace Kinect { return false; } -// Register our camera provider thus making sure Point Tracker can use Kinect V2 IR Sensor -OTR_REGISTER_CAMERA(CamerasProvider) + // Register our camera provider thus making sure Point Tracker can use Kinect V2 IR Sensor + OTR_REGISTER_CAMERA(CamerasProvider) -CameraKinectIr::CameraKinectIr() -{ -} - + InfraredCamera::InfraredCamera() + { + } -CameraKinectIr::~CameraKinectIr() -{ - stop(); -} -bool CameraKinectIr::show_dialog() -{ - return false; -} + InfraredCamera::~InfraredCamera() + { + stop(); + } -bool CameraKinectIr::is_open() -{ - return iInfraredFrameReader!=nullptr; -} + bool InfraredCamera::show_dialog() + { + return false; + } -/// -/// Wait until we get a first frame -/// -void CameraKinectIr::WaitForFirstFrame() -{ - bool new_frame = false; - int attempts = 200; // Kinect cold start can take a while - while (!new_frame && attempts>0) + bool InfraredCamera::is_open() { - new_frame = get_frame_(iMatFrame); - portable::sleep(100); - --attempts; + return iInfraredFrameReader != nullptr; } -} + /// + /// Wait until we get a first frame + /// + void InfraredCamera::WaitForFirstFrame() + { + bool new_frame = false; + int attempts = 200; // Kinect cold start can take a while + while (!new_frame && attempts > 0) + { + new_frame = get_frame_(iMatFrame); + portable::sleep(100); + --attempts; + } + } -std::tuple CameraKinectIr::get_frame() -{ - bool new_frame = false; - new_frame = get_frame_(iMatFrame); - - iFrame.data = iMatFrame.ptr(); - iFrame.width = iWidth; - iFrame.height = iHeight; - iFrame.stride = cv::Mat::AUTO_STEP; - iFrame.channels = iMatFrame.channels(); - iFrame.channelSize = iMatFrame.elemSize1(); - return { iFrame, new_frame }; -} -// Safe release for interfaces -template -inline void SafeRelease(Interface *& pInterfaceToRelease) -{ - if (pInterfaceToRelease != NULL) + std::tuple InfraredCamera::get_frame() { - pInterfaceToRelease->Release(); - pInterfaceToRelease = NULL; - } -} - -/// -/// -/// -bool CameraKinectIr::start(info& aInfo) -{ - stop(); + bool new_frame = false; + new_frame = get_frame_(iMatFrame); - HRESULT hr; + iFrame.data = iMatFrame.ptr(); + iFrame.width = iWidth; + iFrame.height = iHeight; + iFrame.stride = cv::Mat::AUTO_STEP; + iFrame.channels = iMatFrame.channels(); + iFrame.channelSize = iMatFrame.elemSize1(); + return { iFrame, new_frame }; + } - // Get and open Kinect sensor - hr = GetDefaultKinectSensor(&iKinectSensor); - if (SUCCEEDED(hr)) + // Safe release for interfaces + template + inline void SafeRelease(Interface *& pInterfaceToRelease) { - hr = iKinectSensor->Open(); + if (pInterfaceToRelease != NULL) + { + pInterfaceToRelease->Release(); + pInterfaceToRelease = NULL; + } } - // Create infrared frame reader - if (SUCCEEDED(hr)) + /// + /// + /// + bool InfraredCamera::start(info& aInfo) { - // Initialize the Kinect and get the infrared reader - IInfraredFrameSource* pInfraredFrameSource = NULL; + stop(); - hr = iKinectSensor->Open(); + HRESULT hr; + // Get and open Kinect sensor + hr = GetDefaultKinectSensor(&iKinectSensor); if (SUCCEEDED(hr)) { - hr = iKinectSensor->get_InfraredFrameSource(&pInfraredFrameSource); + hr = iKinectSensor->Open(); } + // Create infrared frame reader if (SUCCEEDED(hr)) { - hr = pInfraredFrameSource->OpenReader(&iInfraredFrameReader); - } + // Initialize the Kinect and get the infrared reader + IInfraredFrameSource* pInfraredFrameSource = NULL; - SafeRelease(pInfraredFrameSource); + hr = iKinectSensor->Open(); - if (SUCCEEDED(hr)) - { - iKinectSensor->get_CoordinateMapper(&iCoordinateMapper); - } - } + if (SUCCEEDED(hr)) + { + hr = iKinectSensor->get_InfraredFrameSource(&pInfraredFrameSource); + } + if (SUCCEEDED(hr)) + { + hr = pInfraredFrameSource->OpenReader(&iInfraredFrameReader); + } - if (SUCCEEDED(hr)) - { - WaitForFirstFrame(); - bool success = iMatFrame.ptr() != nullptr; - if (success) - { - // Provide frame info - aInfo.width = iWidth; - aInfo.height = iHeight; + SafeRelease(pInfraredFrameSource); - CameraIntrinsics intrinsics; - hr = iCoordinateMapper->GetDepthCameraIntrinsics(&intrinsics); if (SUCCEEDED(hr)) { - aInfo.focalLengthX = intrinsics.FocalLengthX; - aInfo.focalLengthY = intrinsics.FocalLengthY; - aInfo.principalPointX = intrinsics.PrincipalPointX; - aInfo.principalPointY = intrinsics.PrincipalPointY; - aInfo.radialDistortionFourthOrder = intrinsics.RadialDistortionFourthOrder; - aInfo.radialDistortionSecondOrder = intrinsics.RadialDistortionSecondOrder; - aInfo.radialDistortionSixthOrder = intrinsics.RadialDistortionSixthOrder; + iKinectSensor->get_CoordinateMapper(&iCoordinateMapper); } - } - return success; - } - stop(); - return false; -} + if (SUCCEEDED(hr)) + { + WaitForFirstFrame(); + bool success = iMatFrame.ptr() != nullptr; + if (success) + { + // Provide frame info + aInfo.width = iWidth; + aInfo.height = iHeight; + + CameraIntrinsics intrinsics; + hr = iCoordinateMapper->GetDepthCameraIntrinsics(&intrinsics); + if (SUCCEEDED(hr)) + { + aInfo.focalLengthX = intrinsics.FocalLengthX; + aInfo.focalLengthY = intrinsics.FocalLengthY; + aInfo.principalPointX = intrinsics.PrincipalPointX; + aInfo.principalPointY = intrinsics.PrincipalPointY; + aInfo.radialDistortionFourthOrder = intrinsics.RadialDistortionFourthOrder; + aInfo.radialDistortionSecondOrder = intrinsics.RadialDistortionSecondOrder; + aInfo.radialDistortionSixthOrder = intrinsics.RadialDistortionSixthOrder; + } -void CameraKinectIr::stop() -{ - // done with infrared frame reader - SafeRelease(iInfraredFrame); - SafeRelease(iInfraredFrameReader); + } - // close the Kinect Sensor - if (iKinectSensor) - { - iKinectSensor->Close(); + return success; + } + + stop(); + return false; } - SafeRelease(iCoordinateMapper); - SafeRelease(iKinectSensor); + void InfraredCamera::stop() + { + // done with infrared frame reader + SafeRelease(iInfraredFrame); + SafeRelease(iInfraredFrameReader); - // Free up our memory buffer if any - iMatFrame = cv::Mat(); -} + // close the Kinect Sensor + if (iKinectSensor) + { + iKinectSensor->Close(); + } -bool CameraKinectIr::get_frame_(cv::Mat& aFrame) -{ + SafeRelease(iCoordinateMapper); + SafeRelease(iKinectSensor); - if (!iInfraredFrameReader) - { - return false; + // Free up our memory buffer if any + iMatFrame = cv::Mat(); } - bool success = false; + bool InfraredCamera::get_frame_(cv::Mat& aFrame) + { - // Release previous frame if any - SafeRelease(iInfraredFrame); + if (!iInfraredFrameReader) + { + return false; + } - HRESULT hr = iInfraredFrameReader->AcquireLatestFrame(&iInfraredFrame); + bool success = false; - if (SUCCEEDED(hr)) - { - if (iFirstFrame) - { - IFrameDescription* frameDescription = NULL; + // Release previous frame if any + SafeRelease(iInfraredFrame); - if (SUCCEEDED(hr)) - { - hr = iInfraredFrame->get_FrameDescription(&frameDescription); - } + HRESULT hr = iInfraredFrameReader->AcquireLatestFrame(&iInfraredFrame); - if (SUCCEEDED(hr)) + if (SUCCEEDED(hr)) + { + if (iFirstFrame) { - hr = frameDescription->get_Width(&iWidth); + IFrameDescription* frameDescription = NULL; + + if (SUCCEEDED(hr)) + { + hr = iInfraredFrame->get_FrameDescription(&frameDescription); + } + + if (SUCCEEDED(hr)) + { + hr = frameDescription->get_Width(&iWidth); + } + + if (SUCCEEDED(hr)) + { + hr = frameDescription->get_Height(&iHeight); + } + + if (SUCCEEDED(hr)) + { + hr = frameDescription->get_DiagonalFieldOfView(&iFov); + } + + if (SUCCEEDED(hr)) + { + iFirstFrame = false; + } + + SafeRelease(frameDescription); } - if (SUCCEEDED(hr)) - { - hr = frameDescription->get_Height(&iHeight); - } + + UINT nBufferSize = 0; + UINT16 *pBuffer = NULL; if (SUCCEEDED(hr)) { - hr = frameDescription->get_DiagonalFieldOfView(&iFov); + hr = iInfraredFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer); } if (SUCCEEDED(hr)) { - iFirstFrame = false; + // Create an OpenCV matrix with our 16-bits IR buffer + aFrame = cv::Mat(iHeight, iWidth, CV_16UC1, pBuffer, cv::Mat::AUTO_STEP); + // Any processing of the frame is left to the user + success = true; } - - SafeRelease(frameDescription); } - UINT nBufferSize = 0; - UINT16 *pBuffer = NULL; - - if (SUCCEEDED(hr)) - { - hr = iInfraredFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer); - } - - if (SUCCEEDED(hr)) - { - // Create an OpenCV matrix with our 16-bits IR buffer - aFrame = cv::Mat(iHeight, iWidth, CV_16UC1, pBuffer, cv::Mat::AUTO_STEP); - // Any processing of the frame is left to the user - success = true; - } + return success; } - - return success; -} - - - } #endif -- cgit v1.2.3