emgucv 抠图[通俗易懂]

2022-07-28 17:19:38 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

我的环境的KinectSDK2.0 EmguCV3.0.0

依旧还是WinFrom和ImageBox

因为需要用到BodyIndex的数据,但BodyIndex的分辨率和RGB图像的分辨率不同,所以需要用的CoordinateMap类中的坐标转换函数。

代码和注释如下:

[csharp] view plain copy print ?

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Microsoft.Kinect;
  10. using Emgu.CV;
  11. using Emgu.CV.Structure;
  12. using Emgu.Util;
  13. namespace Kinect_koutu_2
  14. {
  15. public partial class Form1 : Form
  16. {
  17. KinectSensor kinect = null;
  18. MultiSourceFrameReader framereader = null;
  19. FrameDescription fd = null;
  20. FrameDescription cfd = null;
  21. CoordinateMapper coordinate = null;
  22. Image<Bgra, byte> colorimg = null;
  23. DepthSpacePoint[] colorMappedToDepthPoints = null;
  24. byte[] colordata = null;
  25. public Form1()
  26. {
  27. InitializeComponent();
  28. CvInvoke.UseOpenCL = true;
  29. kinect = KinectSensor.GetDefault();
  30. coordinate = kinect.CoordinateMapper;
  31. framereader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color | FrameSourceTypes.BodyIndex);
  32. framereader.MultiSourceFrameArrived = Framereader_MultiSourceFrameArrived;
  33. fd = kinect.BodyIndexFrameSource.FrameDescription;
  34. cfd = kinect.ColorFrameSource.FrameDescription;
  35. colorMappedToDepthPoints = new DepthSpacePoint[cfd.Width * cfd.Height];
  36. colorimg = new Image<Bgra, byte>(cfd.Width, cfd.Height);
  37. colordata = new byte[colorimg.Bytes.Count<byte>()];
  38. kinect.Open();
  39. }
  40. private void Framereader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
  41. {
  42. MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();
  43. if (multiSourceFrame == null)
  44. return;
  45. ColorFrame cFrame = multiSourceFrame.ColorFrameReference.AcquireFrame();
  46. BodyIndexFrame bframe = multiSourceFrame.BodyIndexFrameReference.AcquireFrame();
  47. DepthFrame dframe = multiSourceFrame.DepthFrameReference.AcquireFrame();
  48. if (dframe == null || bframe == null || cFrame == null)
  49. {
  50. Console.WriteLine(“null”);
  51. return;
  52. }
  53. cFrame.CopyConvertedFrameDataToArray(colordata, ColorImageFormat.Bgra);
  54. //colorimg.Bytes = colordata;
  55. //imageBox1.Image = colorimg;
  56. using (KinectBuffer dB = dframe.LockImageBuffer())
  57. {
  58. coordinate.MapColorFrameToDepthSpaceUsingIntPtr(dB.UnderlyingBuffer, dB.Size, colorMappedToDepthPoints); //坐标转换并储存到数组
  59. }
  60. using (KinectBuffer kB = bframe.LockImageBuffer())
  61. {
  62. ProcessBodyIndexFrameData(kB.UnderlyingBuffer);
  63. colorimg.Bytes = colordata;
  64. imageBox1.Image = colorimg;
  65. }
  66. dframe.Dispose();
  67. cFrame.Dispose();
  68. bframe.Dispose();
  69. }
  70. private unsafe void ProcessBodyIndexFrameData(IntPtr bodyIndexFrameData)
  71. {
  72. byte* frameData = (byte*)bodyIndexFrameData;
  73. int colorMappedToDepthPointCount = this.colorMappedToDepthPoints.Length;
  74. fixed (DepthSpacePoint* colorMappedToDepthPointsPointer = this.colorMappedToDepthPoints)
  75. {
  76. for (int i = 0; i < colorMappedToDepthPointCount; i)
  77. {
  78. float colorMappedToDepthX = colorMappedToDepthPointsPointer[i].X;
  79. float colorMappedToDepthY = colorMappedToDepthPointsPointer[i].Y;
  80. int depthX = (int)(colorMappedToDepthX 0.5f); //colorimage的像素点的位置在景深图的对应位置
  81. int depthY = (int)(colorMappedToDepthY 0.5f);
  82. if ((depthX >= 0) && (depthX < 512) && (depthY >= 0) && (depthY < 424))
  83. {
  84. int depthIndex = (depthY * 512) depthX;
  85. if (frameData[depthIndex] ==255) //在检测范围内frameData[depthIndex] !=255 为检测到人的像素点,不予以操作,并将其他像素点设置为黑色
  86. {
  87. colordata[i * 4] = 0;
  88. colordata[i * 4 1] = 0;
  89. colordata[i * 4 2] = 0;
  90. colordata[i * 4 3] = 255;
  91. }
  92. }
  93. else
  94. {
  95. colordata[i * 4] = 0;
  96. colordata[i * 4 1] = 0;
  97. colordata[i * 4 2] = 0;
  98. colordata[i * 4 3] = 255;
  99. }
  100. }
  101. }
  102. }
  103. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  104. {
  105. if (this.kinect != null)
  106. {
  107. this.kinect.Close();
  108. this.kinect = null;
  109. }
  110. }
  111. }
  112. }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128495.html原文链接:https://javaforall.cn

0 人点赞