《我们来说说景深数据Depth Data,这是Kinect的Depth.docx》由会员分享,可在线阅读,更多相关《我们来说说景深数据Depth Data,这是Kinect的Depth.docx(7页珍藏版)》请在第壹文秘上搜索。
1、我位来说说景深数iKDepthData,这是KineCt的DePthCamera为我们供应的全新功能,以往的技术只能郎功过图像识别未完成的一些工作,我们可以借助景深来帮我们完成了.比方.前景jf1.f景的别玄,以前只健将背设设置为武屏或者绿屏,但是现在有了景深数榭,我们就可以很蒲沽地将前景物体从背兔中别尚出来.当然,须要特殊说明的是,DepthCamera技术是由以色列公司PrimeSense供应的“程序布局在这章里.我In,完成的工作特别M沽.依擀物体距离Kinea的远近,设置成不同的颜色.首先.我们要创立一个新的WPF1.:出,愿后溢颌一个Image控件:然后是MaInWindOW.xam
2、1.es中的核心代码,这局部代码。之附的代码大体一样,所以不做过多说明了,/KinectRuntimeRuntineRuntiaeni=newRuntime。;privatevoidWindow_1.oaded(objectsender,RoutedEventArgse)/UseDepthAndP1.ayerIndexandUseSke1.eta1.Trackingnui.InitIa1.ize(RuntImeOptions.UseDepthAndP1.ayerIndexRuntiticoptionseUscSkc1.eta1.Tracking);/registerforeventui.Dcp
3、thFraaeRcady-newEventHand1.er(nui-DepthFrameReady);/DepthAndP1.ayerIndexImageTypenui.DepthStream.Open(ImageStream!ype.Depth,2,ItiageReso1.ution.Reso1.utio3224,IiidgeTypetDepthAndP1.ayerIndex);privatevoidWindow-C1.oscd(objectsender,EvcntArgse)nui.Unin1.tia1.izeO;唯一须要大宗例怠的是,我们在初始化函数中传递的参数是Runtimeoptio
4、ns-UseDepthAndPIayerIndex,这表示景深传息中会包含PIayerIndex的信息,接下来,我们就爱花点时间来即用DepthAndP1.ayerIndex的实际构造.SMDepthAndPIayerIndex先来价DePthFrameReady*务处理函数,如下:voidnui-DepthFrameReady(objectsender,ImageFraffieReadyEventArgse)(/ConvertdepthInformationforapixe1.Intoco1.orinformationbyteCo1.oredBytes-Generate3Bits1.5);D
5、epthAndPIayer1.ndeximage费型的第一个bytes位包括P1.ayerIndex信息P1.ayer1.ndex最多包含六个可使他:0,没有玩家:1.玩宗1:2.玩冢2,以此类推.我们n以用下面的方法来抉得P1.ayerIndex信息:privatestaticintGetPIayerIndex(bytefirstFrame)(/returns-nop1.ayer,11stp1.ayer,2-2ndp1.ayer.return(int)fIrstFrame&7;)设WR色接卜来我们创立一个bytes数组用来存储颜色鱼.if(distance)/wearethefarthest
6、Co1.orFrameindexB1.ueIndexJ;CoIorFrameindexGreenIndex=255;co1.orFrameindexRedIndex255;)当然,我们M好是设置几个边界值,柒定义不同距禽的不【可颜色,/equa1.co1.oringforIionochromatIchistogramvarintensity=Ca1.cu1.ateIntensityFronDepth(distance);CO1.OrFrameindexB1.ueIndcx-intensity;co1.orFrameindex+GreenIndexJ=intensity;co1.orFrame(
7、inJeRedIndex)-intensity;constf1.oatMaxDepthDistance=4006;/maxva1.uereturnedconstf1.oatMinDepthDistance85;/inva1.uereturnedconstf1.oatMaxDepthDistanceOffset=MaxDepthDistance-MinOepthDistance;pub1.icstaticbyteCa1.cu1.ateIntcnsityFromcpth(intdistance)(/formu1.aforca1.cu1.atingmonochromeintensityforhist
8、ogramreturn(byte)(255-(255Math.Ma(distance-MinDepthDistancej)/(MaxDepthDistanceOffset);峨终.把全部代码放在一起我们设定的颜色值为,屋色小J900:900fJ2000.愫色、大于2000.红色代码如下,privatebyte()GencratcCo1.oredBytes(ImagoFramcImageFname)(intheight三imageFname.I三age.Height;Intwidth=ImageFrame.Image.Width;/Depthdataforeachpixe1.BytedepthD
9、ata=IiiageFraae.Image.Bits;/Co1.orFramccontainsco1.orinformationfora1.1.pixe1.sinimage/HeightWidthx4(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/YoumustsettransparencyforBgra
10、as.NETdefau1.tsabyteto0-fu1.1.ytransparent/hardcoded1.ocationstoB1.ue,Green,Red(BGR)indexpositionsconstintB1.ueIndex;constintGreenIndex1;constintRedIndex=2;vardepthindex=0;for(vary-;yheight;y)(varheightoffset三yxwidth;for(varx-0;xwidth;X)(varindex=(width-1)+heightOHset)*4;/vardistance=GetDistance(dep
11、thDatadepthInde,depthDatadepthindex1);vardistance-GetDista11ceWithP1.ayerIndex(depthOatadepthindex,depthDatadepthindex1);if(distance900&distance2)Wearethefarthestco1.orFrane(indexB1.ueIndex-0;co1.orFraneindexGreenIndex;co1.orFraf1.eindex*RedIndex=255;/Co1.orap1.ayerif(GetP1.ayerIde(depthDatadepthindex)WCarethefarthestco1.orFra11eindexB1.ueIndexJ-0;co1.orFraneideGreenIndex三255;co1.orFraeinde*RedIndex=2