Last active
December 19, 2016 04:05
-
-
Save nariakiiwatani/8ca533849c61a493fca51fd54c4ed392 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofxMultiKinectV2.h" | |
#include "ofxCv.h" | |
class Kinect : public ofxMultiKinectV2 | |
{ | |
public: | |
template<typename PixelType> | |
ofPixels_<PixelType> getClippedPixels(const ofRectangle &crop_rect, unsigned short min_distance_in_mm, unsigned short max_distance_in_mm) { | |
ofPixels_<PixelType> ret; | |
const cv::Mat &src = ofxCv::toCv(getDepthPixelsRef()); | |
if(src.empty()) return ret; | |
cv::Rect rect = ofxCv::toCv(crop_rect); | |
cv::Mat result(rect.size(), cvType<PixelType>(), cv::Scalar(0)); | |
cv::Mat crop(src, rect); | |
cv::Mat mask; | |
cv::inRange(crop, min_distance_in_mm, max_distance_in_mm, mask); | |
double nearest, farest; | |
cv::minMaxLoc(crop, &nearest, &farest, nullptr, nullptr, mask); | |
double ratio = std::numeric_limits<PixelType>::max()/(double)(max_distance_in_mm-min_distance_in_mm); | |
nearest = (nearest-min_distance_in_mm)*ratio; | |
farest = (farest-min_distance_in_mm)*ratio; | |
cv::normalize(crop, result, farest, nearest, cv::NORM_MINMAX, cvType<PixelType>(), mask); | |
ofxCv::toOf(result, ret); | |
return std::move(ret); | |
} | |
private: | |
template<typename PixelType> int cvType() const; | |
}; | |
template<> inline int Kinect::cvType<unsigned char>() const { return CV_8UC1; } | |
template<> inline int Kinect::cvType<unsigned short>() const { return CV_16UC1; } | |
template<> inline int Kinect::cvType<float>() const { return CV_32FC1; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment