Howdy,
I’m having trouble pulling the depth values and converting them to a usable distance. I’m pretty bad at image manipulations….
I pulled the image data out of memory and converted it to an array of bytes. I’m getting what looks like argb. Then i’m trying to convert that to greyscale using greyscaleArray[s] = 0.2989 * imageAsBytes[x+1] + 0.5870 * imageAsBytes[x+2] + 0.1140 * imageAsBytes[x+3];
once I get that I move on to resizing the array to a smaller size. 640*480 is way to big for our application. I’m not sure if my averaging is off or the greyscale. I would really appreciate some info on how to do this correctly. Preferably in C# but beggars really can’t be choosers!
Cheers
public static int[,] mapZValues(IntPtr pointerToImage)
{
byte[] imageAsBytes = new byte[(uint)640 * (uint)480 * 4];
Marshal.Copy(pointerToImage, imageAsBytes, 0, 640 * 480 * 4);
//Try to convert the image to greyscale
double[] greyscaleArray = new double[imageAsBytes.Length / 4];
int s = 0;
for (int x = 0; x < imageAsBytes.Length; x = x+4)
{
greyscaleArray[s] = 0.2989 * imageAsBytes[x+1] + 0.5870 * imageAsBytes[x+2] + 0.1140 * imageAsBytes[x+3];
s++;
}
//Create a 2d array to store the greyscale converted pixels
double[,] depth2dArray = new double[640, 480];
for (int x = 0; x < 640; x++)
{
for (int y = 0; y < 480; y++)
{
depth2dArray[x, y] = Math.Tan(greyscaleArray[y + (x * 480)] / 1024 + 0.5) * 33.825 + 5.7;
}
}
return averageMatrix(depth2dArray,2,2);
}
//I need a resized version of the array (smaller version)
public static int[,] averageMatrix(double[,] matrix, int newWidth, int newHeight)
{
double[] singleDimensionTempArray = new double[newWidth * newHeight];
int[,] newDepthArray = new int[newWidth, newHeight];
double average = 0;
if (newWidth * newHeight < matrix.Length && matrix != null)
{
int widthOfSubGridToAverage = matrix.GetLength(0) / newWidth;
int heightOfSubGridToAverage = matrix.Length / matrix.GetLength(0) / newHeight;
for (int i = 0; i < matrix.Length / (widthOfSubGridToAverage * heightOfSubGridToAverage); i++)
{
for (int j = 0; j < widthOfSubGridToAverage; j++)
{
int currentColumn = (j + (i * widthOfSubGridToAverage) % matrix.GetLength(0));
for (int k = 0; k < heightOfSubGridToAverage; k++)
{
int currentRow = (k + (i * heightOfSubGridToAverage) % (matrix.Length / matrix.GetLength(0)));
average += matrix[currentColumn, currentRow];
}
}
singleDimensionTempArray[i] = average / (widthOfSubGridToAverage * heightOfSubGridToAverage);
average = 0;
}
}
for (int x = 0; x < newWidth; x++)
{
for (int y = 0; y < newHeight; y++)
{
newDepthArray[x, y] = (int) singleDimensionTempArray[y + (x * newWidth)];
}
}
return newDepthArray;
}