123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MathNet.Numerics.LinearAlgebra.Single;
- using bbiwarg.Graphics;
- namespace bbiwarg.DataSource
- {
- class VideoHandle : IVideoHandle
- {
- private IInputProvider inputProvider;
- private DepthImage depthImage;
- private ConfidenceImage confidenceImage;
- private UVImage uvImage;
- private ColorImage colorImage;
- private float maxU;
- private float maxV;
- public VideoHandle(IInputProvider inputProvider) {
- this.inputProvider = inputProvider;
- inputProvider.init();
- inputProvider.start();
- inputProvider.updateFrame();
- createImageData();
- maxU = (float)(Math.Tan(inputProvider.getHFOV() / 2f));
- maxV = (float)(Math.Tan(inputProvider.getVFOV() / 2f));
- }
- ~VideoHandle()
- {
- inputProvider.stop();
- }
- public void nextFrame()
- {
- inputProvider.releaseFrame();
- inputProvider.updateFrame();
- createImageData();
- }
- private void createImageData()
- {
- depthImage = inputProvider.getDepthImage();
- confidenceImage = inputProvider.getConfidenceImage();
- uvImage = inputProvider.getUVImage();
- colorImage = inputProvider.getColorImage();
- }
- public short getDepth(int x, int y)
- {
- return depthImage.getDepth(x, y);
- }
- public short getConfidence(int x, int y)
- {
- return confidenceImage.getConfidence(x, y);
- }
- public Color getColor(int x, int y)
- {
- float u = uvImage.getU(x, y);
- float v = uvImage.getV(x, y);
- if (u < 0 || v < 0)
- return Color.Black;
- int colorImageWidth = colorImage.getWidth();
- int colorImageHeight = colorImage.getHeight();
- int xInColorImage = (int)(u * colorImageWidth) % colorImageWidth;
- int yInColorImage = (int)(v * colorImageHeight) % colorImageHeight;
- return colorImage.getColor(xInColorImage, yInColorImage);
- }
- public Vector getPalmPosition3D(uint handIndex)
- {
- //improved palm recognition or just return the value provided by inputprovider
- return inputProvider.getPalmPosition3D(handIndex);
- }
- public VertexArray getVertexArray()
- {
- int width = depthImage.getWidth();
- int height = depthImage.getHeight();
- Vertex[] vertices = new Vertex[width * height];
- Color[] colors = new Color[width * height];
- int index = 0;
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- int depth = depthImage.getDepth(x, y);
- vertices[index] = create3DVertexFrom2D(x, y, depth);
- colors[index] = getColor(x, y);
- index++;
- }
- }
- return new VertexArray(vertices, colors);
- }
- public Vertex create3DVertexFrom2D(float pixelX, float pixelY, int depth)
- {
- int width = depthImage.getWidth();
- int height = depthImage.getHeight();
- float convertedDepth = depth / 1000f; // mm into m
- float u = (pixelX / (float)width - 0.5f) * 2f;
- float v = ((1 - pixelY / (float)height) - 0.5f) * 2f;
- float relX = (u * maxU);
- float relY = (v * maxV);
- float z = convertedDepth / (float)Math.Sqrt(1 + relX * relX + relY * relY);
- float x = relX * z;
- float y = relY * z;
- return new Vertex(x, y, z);
- }
-
-
- }
- }
|