Skip to content

Instantly share code, notes, and snippets.

@nariakiiwatani
Last active December 19, 2016 04:05
Show Gist options
  • Save nariakiiwatani/8ca533849c61a493fca51fd54c4ed392 to your computer and use it in GitHub Desktop.
Save nariakiiwatani/8ca533849c61a493fca51fd54c4ed392 to your computer and use it in GitHub Desktop.
#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