using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using Emgu.CV; using Emgu.CV.Structure; using bbiwarg.Output; using bbiwarg.Recognition.FingerRecognition; using bbiwarg.Recognition.HandRecognition; using bbiwarg.Utility; namespace bbiwarg.Images { /// /// DepthImage stores a processed version of the depth image read by the camera as an /// public class DepthImage { /// /// image size of the deph image /// public ImageSize Size { get; private set; } /// /// the processed depth image /// public Image Image { get; private set; } /// /// the minimum depth in the raw depth image /// public Int16 MinDepth { get; private set; } /// /// the maximum depth which is considered important /// public Int16 MaxDepth { get; private set; } /// /// Construct a DepthImage from the raw depth data, the image size and a confidenceImage. /// Filters the image using confidenceImage and also thresholds and smoothes it. /// /// the raw depth data /// the image size /// the confidence image public DepthImage(IntPtr rawDepthData, ImageSize size, ConfidenceImage confidenceImage) { Size = size; Image rawDepthImage = new Image(Size.Width, Size.Height, Size.Width * 2, rawDepthData); // filter with confidenceImage mask rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert().Mul(Int16.MaxValue)); // smooth with median filter rawDepthImage = rawDepthImage.SmoothMedian(Parameters.DepthImageMedianSize); // threshold min&maxDepth MinDepth = findMinDepth(rawDepthImage); MaxDepth = (Int16)(MinDepth + Parameters.DepthImageDepthRange); // threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src) Image = (rawDepthImage - MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert(); // smooth with median filter Image = Image.SmoothMedian(Parameters.DepthImageMedianSize); } /// /// Returns the depth in the processed image at a given point. /// /// the point /// depth at the point public Int16 getDepthAt(Point point) { return getDepthAt(point.X, point.Y); } /// /// Returns the depth in the processed image at a given position. /// /// x coordinate of the position /// y coordinate of the position /// the depth at the position public Int16 getDepthAt(int x, int y) { return (Int16)(MinDepth + Image.Data[y, x, 0]); } /// /// Sets the depth in the processed image. /// /// point where the depth is set /// new depth value public void setDepthAt(Point point, Int16 depth) { setDepthAt(point.X, point.Y, depth); } /// /// Sets the depth in the processed image. /// /// x coordinate of position to set depth /// y coordinate of position to set depth /// new depth value public void setDepthAt(int x, int y, Int16 depth) { Image.Data[y, x, 0] = (byte)(depth - MinDepth); } /// /// Returns the minimum depth in an /// /// the image /// the minimum depth private Int16 findMinDepth(Image image) { // min and max values double[] min, max; // min and max locations Point[] minLoc, maxLoc; image.MinMax(out min, out max, out minLoc, out maxLoc); return (Int16)min[0]; } } }