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
|
#include <iostream>
using namespace std;
unsigned short mDepthBuffer[480*640];
void FindDepth(unsigned short SearchDepth, int Variation, int& top, int& left, int& bottom, int& right)
{
bool found = false;
int rowW = 0, rowStart = 640;
right = 0;
left = 640;
bottom = 0;
top = -1;
//unsigned int TotalDepth = 0;
unsigned short Depth;
for (int y = 0, i=0; y < 480; y++)
{
rowW = 0;
for (int x = 0; x < 640; x++, i++)
{
Depth = mDepthBuffer[i];
if (Depth < SearchDepth + Variation && Depth > SearchDepth - Variation)
{
rowW++;
if (rowW == 1) // Save the start of the row's x pos
rowStart = x;
//TotalDepth += Depth;
}
else if (rowW > 0) // If the depth doesn't match but its not the first match in the row
{
break;
}
}
if (rowW > right) // Update the max width only if it is the largest yet
right = rowW;
if (rowW && top == -1) // Find the first y to have a match
top = y;
if (rowStart < left) // Find the first horozontal match
left = rowStart;
if (right && rowW == 0) // This makes sure that we match only one solid object
bottom = y - 1; break;
}
right += left; // Make them coords
bottom += top;
return;
}
void MakeDepth()
{
for (int y = 0, i = 0; y < 480; y++)
{
for(int x = 0; x < 640; x++, i++)
{
if (x > 100 && x < 300 && y > 50 && y < 250)
mDepthBuffer[i] = 901;
else
mDepthBuffer[i] = 1500;
}
}
return;
}
int main()
{
int top, left, bottom, right;
cout << "Creating...";
MakeDepth();
cout << "Finding...";
FindDepth(900, 50, top, left, bottom, right);
cout << "(" << left << ", " << top << ")\t"
<< "(" << right << ", " << bottom << ")\n";
cin.ignore();
}
|