https://openkinect.org/w/api.php?action=feedcontributions&user=Christian+wittner&feedformat=atom
OpenKinect - User contributions [en]
2024-03-28T17:38:19Z
User contributions
MediaWiki 1.30.0
https://openkinect.org/w/index.php?title=C%2B%2BOpenCvExample&diff=1498
C++OpenCvExample
2012-07-03T15:34:28Z
<p>Christian wittner: Fix with solution from this post https://groups.google.com/group/openkinect/browse_thread/thread/dede0769c6f05456/699bebae3698b11a?lnk=gst&q=maarige#</p>
<hr />
<div><pre><br />
#include "libfreenect.hpp"<br />
#include <iostream><br />
#include <vector><br />
#include <cmath><br />
#include <pthread.h><br />
#include <cv.h><br />
#include <cxcore.h><br />
#include <highgui.h><br />
<br />
using namespace cv;<br />
using namespace std;<br />
<br />
class Mutex {<br />
public:<br />
Mutex() {<br />
pthread_mutex_init( &m_mutex, NULL );<br />
}<br />
void lock() {<br />
pthread_mutex_lock( &m_mutex );<br />
}<br />
void unlock() {<br />
pthread_mutex_unlock( &m_mutex );<br />
}<br />
private:<br />
pthread_mutex_t m_mutex;<br />
};<br />
<br />
class MyFreenectDevice : public Freenect::FreenectDevice {<br />
public:<br />
MyFreenectDevice(freenect_context *_ctx, int _index)<br />
: Freenect::FreenectDevice(_ctx, _index), m_buffer_depth(FREENECT_DEPTH_11BIT),m_buffer_rgb(FREENECT_VIDEO_RGB), m_gamma(2048), m_new_rgb_frame(false), m_new_depth_frame(false),<br />
depthMat(Size(640,480),CV_16UC1), rgbMat(Size(640,480),CV_8UC3,Scalar(0)), ownMat(Size(640,480),CV_8UC3,Scalar(0))<br />
{<br />
for( unsigned int i = 0 ; i < 2048 ; i++) {<br />
float v = i/2048.0;<br />
v = std::pow(v, 3)* 6;<br />
m_gamma[i] = v*6*256;<br />
}<br />
}<br />
// Do not call directly even in child<br />
void VideoCallback(void* _rgb, uint32_t timestamp) {<br />
std::cout << "RGB callback" << std::endl;<br />
m_rgb_mutex.lock();<br />
uint8_t* rgb = static_cast<uint8_t*>(_rgb);<br />
rgbMat.data = rgb;<br />
m_new_rgb_frame = true;<br />
m_rgb_mutex.unlock();<br />
};<br />
// Do not call directly even in child<br />
void DepthCallback(void* _depth, uint32_t timestamp) {<br />
std::cout << "Depth callback" << std::endl;<br />
m_depth_mutex.lock();<br />
uint16_t* depth = static_cast<uint16_t*>(_depth);<br />
depthMat.data = (uchar*) depth;<br />
m_new_depth_frame = true;<br />
m_depth_mutex.unlock();<br />
}<br />
<br />
bool getVideo(Mat& output) {<br />
m_rgb_mutex.lock();<br />
if(m_new_rgb_frame) {<br />
cv::cvtColor(rgbMat, output, CV_RGB2BGR);<br />
m_new_rgb_frame = false;<br />
m_rgb_mutex.unlock();<br />
return true;<br />
} else {<br />
m_rgb_mutex.unlock();<br />
return false;<br />
}<br />
}<br />
<br />
bool getDepth(Mat& output) {<br />
m_depth_mutex.lock();<br />
if(m_new_depth_frame) {<br />
depthMat.copyTo(output);<br />
m_new_depth_frame = false;<br />
m_depth_mutex.unlock();<br />
return true;<br />
} else {<br />
m_depth_mutex.unlock();<br />
return false;<br />
}<br />
}<br />
<br />
private:<br />
std::vector<uint8_t> m_buffer_depth;<br />
std::vector<uint8_t> m_buffer_rgb;<br />
std::vector<uint16_t> m_gamma;<br />
Mat depthMat;<br />
Mat rgbMat;<br />
Mat ownMat;<br />
Mutex m_rgb_mutex;<br />
Mutex m_depth_mutex;<br />
bool m_new_rgb_frame;<br />
bool m_new_depth_frame;<br />
};<br />
<br />
<br />
<br />
int main(int argc, char **argv) {<br />
bool die(false);<br />
string filename("snapshot");<br />
string suffix(".png");<br />
int i_snap(0),iter(0);<br />
<br />
Mat depthMat(Size(640,480),CV_16UC1);<br />
Mat depthf (Size(640,480),CV_8UC1);<br />
Mat rgbMat(Size(640,480),CV_8UC3,Scalar(0));<br />
Mat ownMat(Size(640,480),CV_8UC3,Scalar(0));<br />
<br />
//The next two lines must be changed as Freenect::Freenect isn't a template but the method createDevice:<br />
//Freenect::Freenect<MyFreenectDevice> freenect;<br />
//MyFreenectDevice& device = freenect.createDevice(0);<br />
//by these two lines:<br />
Freenect::Freenect freenect;<br />
MyFreenectDevice& device = freenect.createDevice<MyFreenectDevice>(0);<br />
<br />
namedWindow("rgb",CV_WINDOW_AUTOSIZE);<br />
namedWindow("depth",CV_WINDOW_AUTOSIZE);<br />
device.startVideo();<br />
device.startDepth();<br />
while (!die) {<br />
device.getVideo(rgbMat);<br />
device.getDepth(depthMat);<br />
cv::imshow("rgb", rgbMat);<br />
depthMat.convertTo(depthf, CV_8UC1, 255.0/2048.0);<br />
cv::imshow("depth",depthf);<br />
char k = cvWaitKey(5);<br />
if( k == 27 ){<br />
cvDestroyWindow("rgb");<br />
cvDestroyWindow("depth");<br />
break;<br />
}<br />
if( k == 8 ) {<br />
std::ostringstream file;<br />
file << filename << i_snap << suffix;<br />
cv::imwrite(file.str(),rgbMat);<br />
i_snap++;<br />
}<br />
if(iter >= 1000) break;<br />
iter++;<br />
}<br />
<br />
device.stopVideo();<br />
device.stopDepth();<br />
return 0;<br />
}<br />
<br />
</pre><br />
<br />
== Makefile ==<br />
<br />
<pre><br />
all: test<br />
<br />
CFLAGS=-fPIC -g -Wall `pkg-config --cflags opencv`<br />
LIBS = `pkg-config --libs opencv`<br />
INCLUDE = -I/usr/local/include/libfreenect<br />
FREE_LIBS = -L/usr/local/lib -lfreenect<br />
<br />
test: test.cpp<br />
$(CXX) $(INCLUDE) $(CFLAGS) $? -o $@ $(LIBS) $(FREE_LIBS)<br />
<br />
%.o: %.cpp<br />
$(CXX) -c $(CFLAGS) $< -o $@<br />
<br />
clean:<br />
rm -rf *.o test<br />
</pre></div>
Christian wittner