3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kinectV2をopenFrameworksで動かす

Last updated at Posted at 2015-12-19

環境

  • of_v0.8.4_vs_release
  • Visual Studio Express 2012 for Windows
  • kinect v2
  • Windows Embedded 8

各種パスの設定

まずプロジェクトファイルのプロパティを開く

1.構成プロパティ > C/C++ > 追加のインクルードディレクトリに**$(KINECTSDK20_DIR)¥inc;**を追加

1.JPG

2.構成プロパティ > リンカー > 全般 > 追加のライブラリディレクトリに**$(KINECTSDK20_DIR)¥lib¥x86;**を追加

2.JPG

3.構成プロパティ > リンカー > 入力にKinect20.libを追加

5.JPG

4.構成プロパティ > VC++ディレクトリ > インクルードディレクトリに$(KINECTSDK20_DIR)\inc;を入力
5.構成プロパティ > VC++ディレクトリ > ライブラリディレクトリに$(KINECTSDK20_DIR)\lib\x86;を入力

4.JPG

##カラー画像の表示

ofApp.h

#pragma once

#include "ofMain.h"
#include <kinect.h>

template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
	if (pInterfaceToRelease != NULL) {
		pInterfaceToRelease->Release();
		pInterfaceToRelease = NULL;
	}
}

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
		bool initKinect();

		IKinectSensor *sensor;

		IColorFrameSource *colorSource;
		IColorFrameSource *colorFrameSource;
		IColorFrameReader *colorReader;
		IFrameDescription* colorDescription;
		int colorWidth, colorHeight;
		unsigned int colorBytesPerPixels;

		ofImage colorImage;
		
};


ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofSetWindowShape(1920, 1080);
	ofSetFrameRate(30);

	if (!initKinect()) exit();

	colorImage.allocate(colorWidth, colorHeight, OF_IMAGE_COLOR_ALPHA);
}

//--------------------------------------------------------------
void ofApp::update(){
	IColorFrame* colorFrame = nullptr;
	HRESULT hResult = colorReader->AcquireLatestFrame(&colorFrame);

	if (SUCCEEDED(hResult)) {
		hResult = colorFrame->CopyConvertedFrameDataToArray(colorHeight * colorWidth * colorBytesPerPixels, colorImage.getPixels(), ColorImageFormat_Rgba);
		colorImage.update();
	}

	SafeRelease(colorFrame);
}

//--------------------------------------------------------------
void ofApp::draw(){
	colorImage.draw(0,0);
}

bool ofApp:: initKinect() {
	HRESULT hResult = S_OK;
	hResult = GetDefaultKinectSensor(&sensor);
	if (FAILED(hResult)) {
		std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
		return -1;
	}

	hResult = sensor->Open();
	if (FAILED(hResult)) {
		std::cerr << "Error : IKinectSensor::Open()" << std::endl;
		return -1;
	}

	hResult = sensor->get_ColorFrameSource(&colorSource);
	if (FAILED(hResult)) {
		std::cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
		return -1;
	}

	hResult = colorSource->OpenReader(&colorReader);
	if (FAILED(hResult)) {
		std::cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
		return -1;
	}

	hResult = colorSource->CreateFrameDescription(ColorImageFormat::ColorImageFormat_Rgba, &colorDescription);
	if (FAILED(hResult)) {
		std::cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
		return -1;
	}
	colorDescription->get_Width(&colorWidth);
	colorDescription->get_Height(&colorHeight);
	colorDescription->get_BytesPerPixel(&colorBytesPerPixels);

	return true;
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}


参考

http://www.hirotakaster.com/weblog/openframeworks-0-8-3-and-kinect-v2/
http://www.buildinsider.net/small/kinectv2cpp/02

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?