123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- 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
- {
- class DepthImage
- {
- private int width;
- private int height;
- private Image<Gray, Int16> image;
- private Image<Gray, Int16> edges;
- private bool[,] fingerPoints;
- private Int16 minDepth;
- private Int16 maxDepth;
- public DepthImage(int width, int height, Image<Gray, Int16> image) {
- this.width = width;
- this.height = height;
- this.image = image;
- this.image = this.image.SmoothMedian(3);
- //minDepth
- findMinDepth();
- //maxDepth
- maxDepth = (Int16) (minDepth + 200);
- thresholdDepth(minDepth, maxDepth);
- //edges
- calculateEdges();
- //findFingerPoints
- findFingerPoints();
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- public Int16 getMinDepth() {
- return minDepth;
- }
- public Int16 getMaxDepth() {
- return maxDepth;
- }
- public Int16 getDepthAt(int x, int y) {
- return image.Data[y, x, 0];
- }
- public Int16 getEdgeAt(int x, int y)
- {
- return edges.Data[y, x, 0];
- }
- public bool isFingerPoint(int x, int y) {
- return fingerPoints[x,y];
- }
- private void setDepthAt(int x, int y, Int16 value) {
- image.Data[y, x, 0] = value;
- }
- private void 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;
- }
- }
- }
- private void thresholdDepth(int min, int max)
- {
- image = image.ThresholdToZero(new Gray(min));
- image = image.ThresholdTrunc(new Gray(max));
- for (int x = 0; x < width; ++x)
- {
- for (int y = 0; y < height; ++y)
- {
- if (getDepthAt(x, y) == 0)
- setDepthAt(x, y, (short)max);
- }
- }
- }
- private void calculateEdges() {
- Image<Gray, Byte> tmp = image.Convert<Byte>(delegate(Int16 depth) { return (byte)(((depth - minDepth) * Byte.MaxValue) / (maxDepth - minDepth)); });
- Image<Gray, byte> tmp2 = tmp.Canny(100, 75, 3);
- edges = tmp2.Convert<Int16>(delegate(byte depth) { if(depth > 0) {return Int16.MaxValue;} else {return 0;}});//((depth * Int16.MaxValue) / Byte.MaxValue); });
- }
- private void findFingerPoints()
- {
- int maxFingerSize = 30;
- int minFingerSize = 10;
- fingerPoints = new bool[width, height];
- bool[,] pixelsCheckedHorizontal = new bool[width, height];
- bool[,] pixelsCheckedVertical = new bool[width, height];
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- if (pixelsCheckedHorizontal[x,y] == false && getDepthAt(x, y) < maxDepth && getEdgeAt(x,y) == 0) {
- pixelsCheckedHorizontal[x, y] = true;
- pixelsCheckedVertical[x, y] = true;
- //check horizontal
- int edgeLeftX = x-1;
- int edgeRightX = x+1;
- bool edgeLeftFound = false;
- bool edgeRightFound = false;
- while (edgeLeftFound == false && (x-edgeLeftX) < maxFingerSize && edgeLeftX > 0)
- {
- if (getEdgeAt(edgeLeftX, y) > 0)
- edgeLeftFound = true;
- edgeLeftX--;
- }
- while (edgeRightFound == false && (edgeRightX - x) < maxFingerSize && edgeRightX < width)
- {
- if (getEdgeAt(edgeRightX, y) > 0)
- edgeRightFound = true;
- edgeRightX++;
- }
- if (edgeLeftFound && edgeRightFound && (edgeRightX - edgeLeftX) < maxFingerSize && (edgeRightX - edgeLeftX) > minFingerSize) {
- for (int i = edgeLeftX; i < edgeRightX; i++) {
- pixelsCheckedHorizontal[i, y] = true;
- }
- fingerPoints[(edgeLeftX+edgeRightX)/2, y] = true;
- }
- //check vertical
- int edgeTopY = y-1;
- int edgeBottomY = y+1;
- bool edgeTopFound = false;
- bool edgeBottomFound = false;
- while (edgeTopFound == false && (y-edgeTopY) < maxFingerSize && edgeTopY > 0)
- {
- if (getEdgeAt(x, edgeTopY) > 0)
- edgeTopFound = true;
- edgeTopY--;
- }
- while (edgeBottomFound == false && (edgeBottomY-y) < maxFingerSize && edgeBottomY < height)
- {
- if (getEdgeAt(x, edgeBottomY) > 0)
- edgeBottomFound = true;
- edgeBottomY++;
- }
- if (edgeBottomFound && edgeTopFound && (edgeBottomY - edgeTopY) < maxFingerSize && (edgeBottomY - edgeTopY) > minFingerSize)
- {
- for (int i = edgeTopY; i < edgeBottomY; i++)
- {
- pixelsCheckedVertical[x, i] = true;
- }
- fingerPoints[x, (edgeTopY+edgeBottomY)/2] = true;
- }
- }
- }
- }
- }
- }
- }
|