我们来说说景深数据Depth Data,这是Kinect的Depth.docx
我位来说说景深数iKDepthData,这是KineCt的DePthCamera为我们供应的全新功能,以往的技术只能郎功过图像识别未完成的一些工作,我们可以借助景深来帮我们完成了.比方.前景'jf1.f景的别玄,以前只健将背设设置为武屏或者绿屏,但是现在有了景深数榭,我们就可以很蒲沽地将前景物体从背兔中别尚出来.当然,须要特殊说明的是,DepthCamera技术是由以色列公司PrimeSense供应的“程序布局在这章里.我In,完成的工作特别M沽.依擀物体距离Kinea的远近,设置成不同的颜色.首先.我们要创立一个新的WPF1.:出,愿后溢颌一个Image控件:<ImageHeight="24。"Horizonta1.A1.ignment=n1.eft"Margin="62,41,0"Name-image1.Stretch-F1.H-Vertic*1.A1.ignment-"Top-Width-32"/>然后是MaInWindOW.xam1.es中的核心代码,这局部代码。之附的代码大体一样,所以不做过多说明了,/KinectRuntimeRuntineRuntiaeni=newRuntime。;privatevoidWindow_1.oaded(objectsender,RoutedEventArgse)/UseDepthAndP1.ayerIndexandUseSke1.eta1.Trackingnui.InitIa1.ize(RuntImeOptions.UseDepthAndP1.ayerIndexRuntiticoptionseUscSkc1.eta1.Tracking);/registerforeventui.DcpthFraaeRcady÷-newEventHand1.er<ImageFPaneReadyEventArgs>(nui-DepthFrameReady);/DepthAndP1.ayerIndexImageTypenui.DepthStream.Open(ImageStream!ype.Depth,2,ItiageReso1.ution.Reso1.utio32×24,IiidgeTypetDepthAndP1.ayerIndex);privatevoidWindow-C1.oscd(objectsender,EvcntArgse)nui.Unin1.tia1.izeO;唯一须要大宗例怠的是,我们在初始化函数中传递的参数是Runtimeoptions-UseDepthAndPIayerIndex,这表示景深传息中会包含PIayerIndex的信息,接下来,我们就爱花点时间来即用DepthAndP1.ayerIndex的实际构造.SMDepthAndPIayerIndex先来价DePthFrameReady*务处理函数,如下:voidnui-DepthFrameReady(objectsender,ImageFraffieReadyEventArgse)(/ConvertdepthInformationforapixe1.Intoco1.orinformationbyteCo1.oredBytes-Generate<o1.oredBytes(e.InageFrawe);/createanimagebasedontheco1.oredbytesP1.anarImageimage-e.ImageFrane.Image;iage1.Source=BitaapSource.Create(inage.Width,image.Height,96,96,Pixe1.Formats.Bgr32,nu1.1.,Co1.oredBytesjInage.width*Pixe1.ForMts.Bgr32.BitsPerPixe1.I8);)DepthFrameReady”务公返回一个ImageFQme对象,其中金包含一个PIanarImage对我PIanar1.mage对象会包含个byte。数孤这个数组中包含每个像卷的深段信息。这个数组的特点是,从图像左上角开场、从左到右.然后从上到卜.该数组中待个像衮由两个bytes&示.我力须要通过位运停的方法来茨得好个依森的位置信息.ftxta½DepthImageType,将其次个bytes上杉8位即叽Distance(,)-(int)(Bits)its(1.«8);假如是DePthAndPIayerIndeXImageType.第,个bytes右移:位去K1.PIayerIndeX信息.其次个bytes左移5位,Distance(e,)=(int)(Bits0>>3Bits1.<<5);DepthAndPIayer1.ndeximage费型的第一个bytes位包括P1.ayerIndex信息P1.ayer1.ndex最多包含六个可使他:0,没有玩家:1.玩宗1:2.玩冢2,以此类推.我们n以用下面的方法来抉得P1.ayerIndex信息:privatestaticintGetPIayer'Index(bytefirstFrame)(/returns-nop1.ayer,11stp1.ayer,2-2ndp1.ayer.return(int)fIrstFrame&7;)设WR色接卜来我们创立一个bytes数组用来存储颜色鱼.if(distance<-90)(/Zweareveryc1.oseco1.orFrameinde×+B1.ueIndex=255;co1.orFrameindexGreenIndex;co1.orFrameinde×+RedIndex=0;if(GetP1.ayerInde×(depthDatadepthindex)>)/wearethefarthestCo1.orFrameindexB1.ueIndexJ;CoIorFrameindex÷GreenIndex=255;co1.orFrameindexRedIndex255;)当然,我们M好是设置几个边界值,柒定义不同距禽的不【可颜色,/equa1.co1.oringforIionochromatIchistogramvarintensity=Ca1.cu1.ateIntensityFronDepth(distance);CO1.OrFrameindexB1.ueIndcx-intensity;co1.orFrameindex+GreenIndexJ=intensity;co1.orFrame(in<Je×RedIndex)-intensity;constf1.oatMaxDepthDistance=4006;/maxva1.uereturnedconstf1.oatMinDepthDistance85;/inva1.uereturnedconstf1.oatMaxDepthDistanceOffset=MaxDepthDistance-MinOepthDistance;pub1.icstaticbyteCa1.cu1.ateIntcnsityFromcpth(intdistance)(/formu1.aforca1.cu1.atingmonochromeintensityforhistogramreturn(byte)(255-(255Math.Ma×(distance-MinDepthDistancej)/(MaxDepthDistanceOffset);峨终.把全部代码放在一起我们设定的颜色值为,屋色小J900:900fJ2000.愫色、大于2000.红色代码如下,privatebyte()GencratcCo1.oredBytes(ImagoFramcImageFname)(intheight三imageFname.I三age.Height;Intwidth=ImageFrame.Image.Width;/Depthdataforeachpixe1.BytedepthData=IiiageFraae.Image.Bits;/Co1.orFramccontainsco1.orinformationfora1.1.pixe1.sinimage/Height×Widthx4(Red,Green,B1.ue,erptybyte)Byte(Co1.orFraee=newbyteImageFrame.Image.Height*ItiageFrame.Iwagc.Width*4);/Bgr32-B1.ue,Green,Red,emptybyte/Bgra32-B1.ue1Green,Red,transparency/YoumustsettransparencyforBgraas.NETdefau1.tsabyteto0-fu1.1.ytransparent/hardcoded1.ocationstoB1.ue,Green,Red(BGR)indexpositionsconstintB1.ueIndex;constintGreenIndex1;constintRedIndex=2;vardepthindex=0;for(vary-;y<height;y÷÷)(varheightoffset三yxwidth;for(varx-0;x<width;X)(varindex=(width-×-1)+heightOHset)*4;/vardistance=GetDistance(depthDatadepthInde×,depthDatadepthindex1);vardistance-GetDista11ceWithP1.ayerIndex(depthOatadepthindex,depthDatadepthindex÷1);if(distance<90)/weareveryc1.oseCoIorFrateindexB1.ueIndex=255;co1.orFrafte(inde×GreenIndex=0;co1.orFrawe(indexRcdIndexJ-;)e1.seif(distance>900&&distance<200)wearcabitfurtherawayco1.orFrawe(inde×B1.ueIndexO;CoIorFrafIeindex+GreenIndex三255;co1.orFraf1.einde×*RedIndex三0;e1.seif(distance>2)Wearethefarthestco1.orFrane(indexB1.ueIndex-0;co1.orFraneindexGreenIndex;co1.orFraf1.eindex*RedIndex=255;/Co1.orap1.ayerif(GetP1.ayerIde×(depthDatadepthindex)>)WCarethefarthestco1.orFra11eindexB1.ueIndexJ-0;co1.orFraneide×GreenIndex三255;co1.orFraeinde×*RedIndex=2