1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
// --------------------------------
// Include
// --------------------------------
#include "Statistics.h"
#include "Device.h"
#include "Draw.h"
#include <math.h>
// --------------------------------
// Global Variables
// --------------------------------
XnPixelStatistics* g_PixelStatistics = NULL;
enum XnCollectionStatus
{
NOT_COLLECTING,
SHOULD_COLLECT,
COLLECTING,
COLLECTION_ENDED,
} g_StatisticsState;
// --------------------------------
// Code
// --------------------------------
void statisticsInit()
{
g_PixelStatistics = NULL;
g_StatisticsState = NOT_COLLECTING;
const DepthMetaData* pDepthMD = getDepthMetaData();
if (pDepthMD != NULL)
{
g_PixelStatistics = new XnPixelStatistics[pDepthMD->FullXRes() * pDepthMD->FullYRes()];
}
}
void statisticsStart(int)
{
const DepthMetaData* pDepthMD = getDepthMetaData();
if (pDepthMD == NULL)
{
displayMessage("Can't collect statistics: Depth is turned off!");
return;
}
int nCount = pDepthMD->FullXRes() * pDepthMD->FullYRes();
XnDepthPixel nMaxDepth = getDepthGenerator()->GetDeviceMaxDepth();
XnPixelStatistics* pPixel = g_PixelStatistics;
for (int i = 0; i < nCount; ++i, ++pPixel)
{
xnOSMemSet(pPixel, 0, sizeof(XnPixelStatistics));
pPixel->nMin = nMaxDepth;
}
g_StatisticsState = SHOULD_COLLECT;
}
void toggleStatistics(int)
{
switch (g_StatisticsState)
{
case SHOULD_COLLECT:
case COLLECTING:
statisticsStop(0);
break;
case NOT_COLLECTING:
case COLLECTION_ENDED:
statisticsStart(0);
break;
}
}
void statisticsStop(int)
{
g_StatisticsState = COLLECTION_ENDED;
}
void statisticsClear(int)
{
g_StatisticsState = NOT_COLLECTING;
}
void statisticsAddFrame()
{
if (g_StatisticsState == SHOULD_COLLECT)
{
g_StatisticsState = COLLECTING;
}
if (g_StatisticsState == COLLECTING)
{
const DepthMetaData* pDepthMD = getDepthMetaData();
const XnDepthPixel* pDepth = pDepthMD->Data();
for (XnUInt32 y = pDepthMD->YOffset(); y < pDepthMD->YRes() + pDepthMD->YOffset(); ++y)
{
XnPixelStatistics* pPixel = &g_PixelStatistics[y*pDepthMD->FullXRes() + pDepthMD->XOffset()];
for (XnUInt32 x = pDepthMD->XOffset(); x < pDepthMD->XRes() + pDepthMD->XOffset(); ++x, ++pDepth, ++pPixel)
{
XnDepthPixel nDepth = *pDepth;
if (nDepth > pPixel->nMax)
pPixel->nMax = nDepth;
if (nDepth < pPixel->nMin)
pPixel->nMin = nDepth;
pPixel->nSum += nDepth;
pPixel->nSumSquare += (nDepth * nDepth);
pPixel->nCount++;
pPixel->dAverage = pPixel->nSum / pPixel->nCount;
pPixel->dStdDev = sqrt( (1.0/pPixel->nCount) * (pPixel->nSumSquare) - (pPixel->dAverage * pPixel->dAverage) );
}
}
}
}
bool isStatisticsActive()
{
return (g_StatisticsState == COLLECTING || g_StatisticsState == COLLECTION_ENDED);
}
void getStatisticsMessage(char* csMessage)
{
switch (g_StatisticsState)
{
case NOT_COLLECTING:
case SHOULD_COLLECT:
csMessage[0] = 0;
break;
case COLLECTING:
case COLLECTION_ENDED:
const DepthMetaData* pDepthMD = getDepthMetaData();
sprintf(csMessage, "Collected Statistics for %u frames", g_PixelStatistics[pDepthMD->YOffset()*pDepthMD->FullXRes() + pDepthMD->XOffset()].nCount);
break;
}
}
|