123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Emgu.CV;
- using Emgu.CV.Structure;
- namespace bbiwarg.Images
- {
- class DepthImage
- {
- public Image<Gray, Int16> Image { get; private set; }
- public int Width { get { return Image.Width; } }
- public int Height { get { return Image.Height; } }
- public Int16 MinDepth { get; private set; }
- public Int16 MaxDepth { get; private set; }
- public DepthImage(Image<Gray, Int16> image)
- {
- this.Image = image;
- //smooth
- this.Image = image.SmoothMedian(3);
- //threshold min&maxDepth
- MinDepth = findMinDepth();
- MaxDepth = (Int16)(MinDepth + 200);
- thresholdDepth(MinDepth, MaxDepth);
- }
- public Int16 getDepthAt(Point point)
- {
- return getDepthAt(point.X, point.Y);
- }
- public Int16 getDepthAt(int x, int y)
- {
- return Image.Data[y, x, 0];
- }
- public void setDepthAt(Point point, Int16 depth)
- {
- setDepthAt(point.X, point.Y, depth);
- }
- public void setDepthAt(int x, int y, Int16 depth)
- {
- Image.Data[y, x, 0] = depth;
- }
- public float getRelativeDepthAt(Point point)
- {
- return getRelativeDepthAt(point.X, point.Y);
- }
- public float getRelativeDepthAt(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;
- }
- 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)
- setDepthAt(x, y, max);
- }
- }
- }
- }
- }
|