Created
June 14, 2015 12:39
-
-
Save MasWag/294382bd48666356368f to your computer and use it in GitHub Desktop.
depth's player
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
| #include <OpenNI.h> | |
| #include <opencv2/opencv.hpp> | |
| #include <vector> | |
| #include <stdexcept> | |
| int main(int argc,char* argv[]) | |
| { | |
| try { | |
| openni::Status ret = openni::OpenNI::initialize(); | |
| if ( ret != openni::STATUS_OK ) { | |
| throw std::runtime_error( "" ); | |
| } | |
| openni::Device device; | |
| if (argc == 1) | |
| ret = device.open( "./depth.oni" ); | |
| else | |
| ret = device.open( argv[1] ); | |
| if ( ret != openni::STATUS_OK ) { | |
| throw std::runtime_error( "" ); | |
| } | |
| openni::VideoStream depthStream; | |
| ret = depthStream.create( device, openni::SensorType::SENSOR_DEPTH ); | |
| if ( ret != openni::STATUS_OK ) { | |
| throw std::runtime_error( "" ); | |
| } | |
| depthStream.start(); | |
| std::vector<openni::VideoStream*> streams; | |
| streams.push_back( &depthStream ); | |
| cv::Mat depthImage; | |
| while ( 1 ) { | |
| int changedIndex; | |
| openni::OpenNI::waitForAnyStream( &streams[0], streams.size(), &changedIndex ); | |
| if ( changedIndex == 0 ) { | |
| openni::VideoFrameRef depthFrame; | |
| depthStream.readFrame( &depthFrame ); | |
| if ( depthFrame.isValid() ) { | |
| depthImage = cv::Mat( depthStream.getVideoMode().getResolutionY(), | |
| depthStream.getVideoMode().getResolutionX(), | |
| CV_16U, (char*)depthFrame.getData() ); | |
| // 中心点の距離を表示する | |
| auto videoMode = depthStream.getVideoMode(); | |
| // 0-10000mmまでのデータを0-255にする | |
| depthImage.convertTo( depthImage, CV_8U, 255.0 / 10000 ); | |
| cv::imshow( "Depth Camera", depthImage); | |
| } | |
| } | |
| int key = cv::waitKey( 10 ); | |
| if ( key == 'q' ) { | |
| break; | |
| } | |
| } | |
| } | |
| catch ( std::exception& ) { | |
| std::cout << openni::OpenNI::getExtendedError() << std::endl; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment