123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 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;
- using bbiwarg.Graphics;
- namespace bbiwarg.Images
- {
- class DepthImage
- {
- public Image<Gray, byte> Image { get; private set; }
- public int Width { get; private set; }
- public int Height { get; private set; }
- public Int16 MinDepth { get; private set; }
- public Int16 MaxDepth { get; private set; }
- public DepthImage(Image<Gray, Int16> image, OutputImage outputImage)
- {
- Width = image.Width;
- Height = image.Height;
- //threshold min&maxDepth
- MinDepth = findMinDepth(image);
- MaxDepth = (Int16)(MinDepth + 200); // max = minDepth+255 (else it can't fit whole range in byte image)
- //smooth+threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
- Image = image.SmoothMedian(3).Sub(new Gray(MinDepth)).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
- // draw depth
- Image<Gray, byte> tmpDepth = (MaxDepth - MinDepth) - Image;
- outputImage.Image[0] = outputImage.Image[1] = outputImage.Image[2] = tmpDepth;
- }
- public Int16 getDepthAt(Point point)
- {
- return getDepthAt(point.X, point.Y);
- }
- public Int16 getDepthAt(int x, int y)
- {
- return (Int16)(MinDepth + 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] = (byte)(depth - MinDepth);
- }
- 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;
- }
- private Int16 findMinDepth(Image<Gray, Int16> 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];
- }
- }
- }
|