/// <summary> /// /// </summary> /// <param name="depthImg"></param> /// <param name="colorImg">The aligned color image has the same ROI as the depth image.</param> /// <param name="depthFrame"></param> /// <param name="mapper"></param> public static void AlignImageDepthToColor(Image <Gray, Byte> depthImg, Image <Gray, Byte> colorImg, short[] depthFrame, CoordinateConverter mapper) { var width = colorImg.Width; var height = colorImg.Height; CvInvoke.cvZero(colorImg.Ptr); var depthData = depthImg.Data; var colorData = colorImg.Data; var roiWidth = width; var roidHeight = height; var roi = depthImg.ROI; if (!roi.IsEmpty) { roiWidth = roi.Width; roidHeight = roi.Height; } for (int r = roi.Top; r < roi.Top + roidHeight; r++) { for (int c = roi.Left; c < roi.Left + roiWidth; c++) { var depthPixel = depthFrame[r * width + c]; var depth = DepthUtil.RawToDepth(depthPixel); var cp = mapper.MapDepthPointToColorPoint(c, r, depth); if (cp.X >= 0 && cp.X < width && cp.Y >= 0 && cp.Y < height) { colorData[cp.Y, cp.X, 0] = depthData[r, c, 0]; } } } CvInvoke.cvMorphologyEx(colorImg.Ptr, colorImg.Ptr, IntPtr.Zero, IntPtr.Zero, CV_MORPH_OP.CV_MOP_CLOSE, 1); }
void UpdatePlayerDepthImage(short[] depthFrame, Byte[, ,] playerMask, Byte[, ,] skinMask, Rectangle roi) { // Clear depth image. CvInvoke.cvZero(DepthImage.Ptr); var data = DepthImage.Data; var scale = (float)255 / HandInputParams.MaxDepth; var roiWidth = width; var roiHeight = height; if (!roi.IsEmpty) { roiWidth = roi.Width; roiHeight = roi.Height; } for (int r = roi.Top; r < roi.Top + roiHeight; r++) { for (int c = roi.Left; c < roi.Left + roiWidth; c++) { var index = r * width + c; short pixel = depthFrame[index]; var depth = DepthUtil.RawToDepth(pixel); if (IsFilteredPixel(playerMask, skinMask, c, r, depth)) { data[r, c, 0] = (byte)(Math.Max(0, HandInputParams.MaxDepth - depth) * scale); } } } }
public static Image <Gray, Single> CreateCVImage(short[] src, Single[, ,] dst, int width, int height) { for (int r = 0; r < height; r++) { for (int c = 0; c < width; c++) { dst[r, c, 0] = DepthUtil.RawToDepth(src[r * width + c]); } } return(new Image <Gray, Single>(dst)); }
/// <summary> /// /// </summary> /// <param name="depthRect"></param> /// <param name="depthPixel"></param> /// <param name="width">frame width</param> /// <param name="height">frame height</param> /// <returns></returns> public Rectangle MapDepthRectToColorRect(Rectangle depthRect, short[] depthPixel, int width, int height) { var y = Clip(depthRect.Top, 0, height - 1); var x = Clip(depthRect.Left, 0, width - 1); int depth = DepthUtil.RawToDepth(depthPixel[y * width + x]); var cpUpperLeft = MapDepthPointToColorPoint(depthRect.X, depthRect.Y, depth); y = Clip(depthRect.Bottom, 0, height - 1); x = Clip(depthRect.Right, 0, width - 1); depth = DepthUtil.RawToDepth(depthPixel[y * width + x]); var cpBottomRight = MapDepthPointToColorPoint(depthRect.Right, depthRect.Bottom, depth); return(new Rectangle(cpUpperLeft.X, cpUpperLeft.Y, cpBottomRight.X - cpUpperLeft.X, cpBottomRight.Y - cpUpperLeft.Y)); }
/// <summary> /// Updates player depth image using the player contour. /// </summary> /// <param name="depthFrame"></param> /// <param name="contour"></param> /// <param name="skinMask"></param> void UpdatePlayerDepthImage(short[] depthFrame, Seq <Point> contour, Byte[, ,] skinMask) { CvInvoke.cvZero(DepthImage.Ptr); var data = DepthImage.Data; var scale = (float)255 / HandInputParams.MaxDepth; for (int r = 0; r < height; r++) { for (int c = 0; c < width; c++) { var index = r * width + c; short pixel = depthFrame[index]; var depth = DepthUtil.RawToDepth(pixel); if (IsPlayerPixel(contour, skinMask, c, r, depth)) { data[r, c, 0] = (byte)(Math.Max(0, HandInputParams.MaxDepth - depth) * scale); } } } }