using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Emgu.CV; using Emgu.CV.Structure; namespace bbiwarg.Images { class DepthImage { private int width; private int height; private Image image; private Int16 minDepth; private Int16 maxDepth; public DepthImage(int width, int height, Image image) { this.width = width; this.height = height; this.image = image; //smooth this.image = image.SmoothMedian(3); //threshold min&maxDepth minDepth = findMinDepth(); maxDepth = (Int16) (minDepth + 200); thresholdDepth(minDepth, maxDepth); } public Int16 getDepthAt(int x, int y) { return image.Data[y, x, 0]; } public void setDepthAt(int x, int y, Int16 depth) { image.Data[y, x, 0] = depth; } public float getRelativeDepth(int x, int y) { float minMaxInterval = Math.Max(maxDepth - minDepth, 1); return (getDepthAt(x, y)-minDepth) / minMaxInterval; } public Int16 getMinDepth() { return minDepth; } public Int16 getMaxDepth() { return maxDepth; } public int getWidth() { return width; } public int getHeight() { return height; } public Image getImage() { return image; } private Int16 findMinDepth() { minDepth = Int16.MaxValue; for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { Int16 depth = getDepthAt(x, y); if (depth < minDepth) minDepth = depth; } } return minDepth; } private void thresholdDepth(Int16 min, Int16 max) { for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { Int16 depth = getDepthAt(x, y); if (depth > max || depth < min) image.Data[y, x, 0] = max; } } } } }