emgucv 抠图[通俗易懂]

我的环境的KinectSDK2.0+EmguCV3.0.0依旧还是WinFrom和ImageBox因为需要用到BodyIndex的数据,但BodyIndex的分辨率和RGB图像的分辨率不同,所以需要用的CoordinateMap类中的坐标转换函数。然后直接对colorimage的像素点进行操作。同样,需要用的指针,要把项目调整为允许不安全的代码。代码和注释如

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

我的环境的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.   
  14. namespace Kinect_koutu_2  
  15. {  
  16.     public partial class Form1 : Form  
  17.     {  
  18.         KinectSensor kinect = null;  
  19.         MultiSourceFrameReader framereader = null;  
  20.         FrameDescription fd = null;  
  21.         FrameDescription cfd = null;  
  22.         CoordinateMapper coordinate = null;  
  23.         Image<Bgra, byte> colorimg = null;  
  24.         DepthSpacePoint[] colorMappedToDepthPoints = null;  
  25.         byte[] colordata = null;  
  26.         public Form1()  
  27.         {  
  28.             InitializeComponent();  
  29.             CvInvoke.UseOpenCL = true;  
  30.             kinect = KinectSensor.GetDefault();  
  31.             coordinate = kinect.CoordinateMapper;  
  32.             framereader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color | FrameSourceTypes.BodyIndex);  
  33.             framereader.MultiSourceFrameArrived += Framereader_MultiSourceFrameArrived;  
  34.             fd = kinect.BodyIndexFrameSource.FrameDescription;  
  35.             cfd = kinect.ColorFrameSource.FrameDescription;  
  36.             colorMappedToDepthPoints = new DepthSpacePoint[cfd.Width * cfd.Height];  
  37.             colorimg = new Image<Bgra, byte>(cfd.Width, cfd.Height);  
  38.             colordata = new byte[colorimg.Bytes.Count<byte>()];  
  39.             kinect.Open();  
  40.         }  
  41.         private void Framereader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)  
  42.         {  
  43.             MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();  
  44.             if (multiSourceFrame == null)  
  45.                 return;  
  46.             ColorFrame cFrame = multiSourceFrame.ColorFrameReference.AcquireFrame();  
  47.             BodyIndexFrame bframe = multiSourceFrame.BodyIndexFrameReference.AcquireFrame();  
  48.             DepthFrame dframe = multiSourceFrame.DepthFrameReference.AcquireFrame();  
  49.             if (dframe == null || bframe == null || cFrame == null)  
  50.             {  
  51.                 Console.WriteLine(“null”);  
  52.                 return;  
  53.             }  
  54.             cFrame.CopyConvertedFrameDataToArray(colordata, ColorImageFormat.Bgra);  
  55.             //colorimg.Bytes = colordata;  
  56.             //imageBox1.Image = colorimg;  
  57.             using (KinectBuffer dB = dframe.LockImageBuffer())  
  58.             {  
  59.                 coordinate.MapColorFrameToDepthSpaceUsingIntPtr(dB.UnderlyingBuffer, dB.Size, colorMappedToDepthPoints);       //坐标转换并储存到数组  
  60.             }  
  61.               
  62.             using (KinectBuffer kB = bframe.LockImageBuffer())  
  63.             {  
  64.                 ProcessBodyIndexFrameData(kB.UnderlyingBuffer);  
  65.                 colorimg.Bytes = colordata;  
  66.                 imageBox1.Image = colorimg;  
  67.             }  
  68.               
  69.             dframe.Dispose();  
  70.             cFrame.Dispose();  
  71.             bframe.Dispose();  
  72.         }  
  73.   
  74.         private unsafe void ProcessBodyIndexFrameData(IntPtr bodyIndexFrameData)  
  75.         {  
  76.             byte* frameData = (byte*)bodyIndexFrameData;  
  77.             int colorMappedToDepthPointCount = this.colorMappedToDepthPoints.Length;  
  78.             fixed (DepthSpacePoint* colorMappedToDepthPointsPointer = this.colorMappedToDepthPoints)  
  79.             {  
  80.                 for (int i = 0; i < colorMappedToDepthPointCount; ++i)  
  81.                 {  
  82.                     float colorMappedToDepthX = colorMappedToDepthPointsPointer[i].X;  
  83.                     float colorMappedToDepthY = colorMappedToDepthPointsPointer[i].Y;  
  84.                     int depthX = (int)(colorMappedToDepthX + 0.5f);         //colorimage的像素点的位置在景深图的对应位置  
  85.                     int depthY = (int)(colorMappedToDepthY + 0.5f);  
  86.                     if ((depthX >= 0) && (depthX < 512) && (depthY >= 0) && (depthY < 424))  
  87.                     {  
  88.                         int depthIndex = (depthY * 512) + depthX;  
  89.                         if (frameData[depthIndex] ==255)             //在检测范围内frameData[depthIndex] !=255 为检测到人的像素点,不予以操作,并将其他像素点设置为黑色  
  90.                         {  
  91.                             colordata[i * 4] = 0;  
  92.                             colordata[i * 4 + 1] = 0;  
  93.                             colordata[i * 4 + 2] = 0;  
  94.                             colordata[i * 4 + 3] = 255;  
  95.                         }  
  96.                     }  
  97.                     else  
  98.                     {  
  99.                         colordata[i * 4] = 0;  
  100.                         colordata[i * 4 + 1] = 0;  
  101.                         colordata[i * 4 + 2] = 0;  
  102.                         colordata[i * 4 + 3] = 255;  
  103.                     }  
  104.                 }  
  105.             }  
  106.         }  
  107.   
  108.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  109.         {  
  110.             if (this.kinect != null)  
  111.             {  
  112.                 this.kinect.Close();  
  113.                 this.kinect = null;  
  114.             }  
  115.         }  
  116.     }  
  117. }  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 二分归并排序算法_并归排序法

    二分归并排序算法_并归排序法#include<iostream>#include<climits>usingnamespacestd;voidMerge(intSourceArry[],intStart,intMid,intEnd){ intlen1,len2;…

    2022年10月22日
  • 解决win10下VM12虚拟机桥接模式不能上网的方法(亲测可行)[通俗易懂]

    解决win10下VM12虚拟机桥接模式不能上网的方法(亲测可行)[通俗易懂]解决win10下VM12虚拟机桥接模式不能上网的方法(亲测可行)本文的方法可解决如下两个问题:局域网中其他机器ping不通本机中的虚拟机本机中的虚拟机采用桥接模式不能上网,甚至主机也不能上网。注意:自己局域网的IP起始地址及路由器地址,可以通过登陆路由器查看,也可以在所有操作之前在CMD中通过命令ipconfig(windows)或ifconfig(linux)查看。一般路由器的地…

  • MBus协议详解(三)[通俗易懂]

    MBus协议详解(三)[通俗易懂]这节主要集中在MBus协议物理层和数据链路层的硬件实现上,其关键点包括:1、由主到从传输的时候电压的调制;2、由从到主传输的时候电流脉冲的调制;3、总线短路保护。1、由主到从传输的时候电压的调制如上图所示,信号在-27V、0V、+15V上进行调制,采用2个MOS管P201、P202,+15V电压通过稳压器降压到+12V。由主到从传输数据的时…

    2022年10月15日
  • date和localdatetime转换_localDate

    date和localdatetime转换_localDateLocalDateTime是jdk8的新增的类,还有LocalDate,LocalTime;我们可能用到类里面的一些方法,例如传入的时间和当前时间做比较,就需要将Date转为LocalDate或其他两个,Date转换为LocalDateDatedate=newDate();LocalDatelocalDate=date.toInstant().atZone(ZoneId.systemDefault()) //设置当前系统时区.toLocalDat

  • grid布局方式_grid网格布局

    grid布局方式_grid网格布局GridBagConstraints特征:由GridBagConstraints类实现的布局管理器称为网格组布局管理器,它实现了一个动态的矩形网格,这个矩形风格由无数个矩形单元格组成,每个组件可以占用一个或多个这样的单元格。动态矩形网格:可以根据实际需要随意增减矩形网格的行数和列数。它实现的矩形网格的绘制方向由容器决定,网格的索引从0开始。下面写一个测试方法来讲解GridBagC

  • 最炫民族风70个版本大合集[通俗易懂]

    最炫民族风70个版本大合集[通俗易懂]杜甫和奥特曼都已经沦陷了…..01.女生宿舍版02.青岛理工版03.杜甫版04.凉宫春日版05.名扬四海版06.奥特曼版……查看看更多转载于:https://www.cnblogs.com/ShiningRay/archive/2012/04/08/2437543.html…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号