Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef _NITE_SAMPLE_UTILITIES_H_
00009 #define _NITE_SAMPLE_UTILITIES_H_
00010
00011 #include <stdio.h>
00012 #include <OpenNI.h>
00013
00014 #ifdef WIN32
00015 #include <conio.h>
00016 int wasKeyboardHit()
00017 {
00018 return (int)_kbhit();
00019 }
00020
00021 #else // linux
00022
00023 #include <unistd.h>
00024 #include <stdlib.h>
00025 #include <termios.h>
00026 #include <fcntl.h>
00027 int wasKeyboardHit()
00028 {
00029 struct termios oldt, newt;
00030 int ch;
00031 int oldf;
00032
00033
00034 tcgetattr(STDIN_FILENO, &oldt);
00035 newt = oldt;
00036 newt.c_lflag &= ~(ICANON | ECHO);
00037 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
00038 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
00039
00040
00041 if (0 != fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK))
00042 {
00043 return 0;
00044 }
00045
00046 ch = getchar();
00047
00048 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
00049 if (0 != fcntl(STDIN_FILENO, F_SETFL, oldf))
00050 {
00051 return 0;
00052 }
00053
00054 if(ch != EOF)
00055 {
00056 ungetc(ch, stdin);
00057 return 1;
00058 }
00059
00060 return 0;
00061 }
00062 #endif // WIN32
00063
00064 void calculateHistogram(float* pHistogram, int histogramSize, const openni::VideoFrameRef& depthFrame)
00065 {
00066 const openni::DepthPixel* pDepth = (const openni::DepthPixel*)depthFrame.getData();
00067 int width = depthFrame.getWidth();
00068 int height = depthFrame.getHeight();
00069
00070 memset(pHistogram, 0, histogramSize*sizeof(float));
00071 int restOfRow = depthFrame.getStrideInBytes() / sizeof(openni::DepthPixel) - width;
00072
00073 unsigned int nNumberOfPoints = 0;
00074 for (int y = 0; y < height; ++y)
00075 {
00076 for (int x = 0; x < width; ++x, ++pDepth)
00077 {
00078 if (*pDepth != 0)
00079 {
00080 pHistogram[*pDepth]++;
00081 nNumberOfPoints++;
00082 }
00083 }
00084 pDepth += restOfRow;
00085 }
00086 for (int nIndex=1; nIndex<histogramSize; nIndex++)
00087 {
00088 pHistogram[nIndex] += pHistogram[nIndex-1];
00089 }
00090 if (nNumberOfPoints)
00091 {
00092 for (int nIndex=1; nIndex<histogramSize; nIndex++)
00093 {
00094 pHistogram[nIndex] = (256 * (1.0f - (pHistogram[nIndex] / nNumberOfPoints)));
00095 }
00096 }
00097 }
00098
00099
00100 #endif // _NITE_SAMPLE_UTILITIES_H_