Friday, October 7, 2011

Kinect SkeletonTracking-Sample

KinectNote

Project Type : window Form application


using Microsoft.Research.Kinect.Nui;
using Coding4Fun.Kinect.WinForm;





Step 1.
   Initialize
   ------------------------------------------------------------

   Runtime nui;
 
   InLoading Event
   nui = new Runtime();
   nui.Initialize(RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor);
           

   nui.VideoFrameReady += new EventHandler(nui_VideoFrameReady);
   nui.SkeletonFrameReady +=new EventHandler   (nui_SkeletonFrameReady);
   //OpenStram
   nui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480,
                                       ImageType.Color);
                                    1.ImageStreamType :Video ,Depth,Invalid.
                                    2.int : Depend on PC performance. 1 is batter .
                                    3.ImageResolution: Kinect Video Resolution 640x480 can keep frame 30.
                                    4.ImageType: Color,Depth.  



 1.RuntimeOption:
                      i.UseSkeletalTracking :Tracing Player Skeleton.
                      ii.UseColor : Image From Kinect.
                      iii.UseDepthAndPlayerIndex: Give multi player index And Depth Dector.

------------------------------------------------------------
Step 2.
   Method

        void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
        {
            //Using Coding4Fun's DLL
            //picColorVideo is PictureBox
            picColorVideo.Image= e.ImageFrame.ToBitmap();

            //If not using Coding4Fun's DLL
            //PlanerImage data = e.ImageFrame.Image;
            //picColorVideo.Source = BitmapSource.Create(data.Width,data.Height,96,96,
            //                        PixelFormats.Bgr32,null,data.Bits,data.Width*data.BytesPerPixel);
        }
 

       // Kinect coordinate Transfer Screen coordinate

        Point GetDisplayPosition(Joint joint)
        {
            float depthX, depthY;
            nui.SkeletonEngine.SkeletonToDepthImage(joint.Position, out depthX, out depthY);

            depthX = Math.Max(0,Math.Min(depthX *320,320));
            depthY = Math.Max(0, Math.Min(depthY * 240, 240));
            int colorX, colorY;

            nui.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(ImageResolution.Resolution640x480,
                                     new ImageViewArea(), (int)depthX, (int)depthY, (short)0, out colorX, out colorY);
            return new Point(colorX * picColorVideo.Width / 640, colorY * picColorVideo.Height / 480);
        }



        void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            foreach (SkeletonData data in e.SkeletonFrame.Skeletons)
            {
                if (data.TrackingState == SkeletonTrackingState.Tracked)
                {
                    JointsCollection jc = data.Joints;

                    StringBuilder sb = new StringBuilder();
                    foreach (JointID jid in Enum.GetValues(typeof(JointID)))
                    {
                        if (jid != JointID.Count)
                        {
                            Point p = GetDisplayPosition(jc[jid]);
                            sb.Append(string.Format("{0}={1},{2}\n", jid, p.X, p.Y));
                        }
                    }
                    //lblJoins is Labl
                    lblJoins.Text = sb.ToString();
                }
            }
        }




------------------------------------------------------------
SkeletonTrackingState
        Tracked - player have been detected.
        PositionOnly- player have been cut,Kinect hard to detect.
        NotTracked - The Joint doesn't be detect(It is hard to happen) 

Joints TransformSmooth
       nui.SkeletonEngine.TransformSmooth = true;
       Advance
            1.Use TransformSmoothParameters
               nui.SkeletonEngine.SmoothParameters.
                                                                           Correction
                                                                            JitterRadius
                                                                            MaxDeviation
                                                                            Prediction
                                                                            Smoothing
        








Saturday, October 1, 2011

Different about List between Add Method and Remove Method

C# List Note

http://allantech.blogspot.com/2007/03/performance-arraylist-vs-list.html


Array :  array element can be  of  any type,including an array type , an array with n elements and
            the "n" must be given a value.


List: List element can be a strongly typed list of objects that can be accessed by index.List with
       elements can be any Length.


ArrayList : ArrayList element can be of any type,ArrayList with n elements can be any Length.







--------------------------------------------------------------------------------------------------

Java arrayList Note

get(int) : Effective.
add(Object obj) : Effective.
set(int index,Object obj):Effective.

add(int index,Object obj):Low Effective.
remove(int index): Low Effective.
contains(Object obj):Low Effective.(Because Of Calling index Of)

Remove List Way:
1.Use Remove method,Delete Element.
2.Rebird new ArrayList,Insert Useless Element.

--------------------------------------------------------------------------------------------------
Java Example
There is an ArrayList include Integer,We need to Delete odd number



public class Example
{
    private static List exampleNew(int cnt)
    {
       List p = new ArrayList();
       for(int i =0; i < cnt;i++)
         p.Add(new Integer(i));

      List o = new ArrayList(100);
      for(int i = 0;i < p.Size();i++)
          if(i%2==0)
              o.Add(p.get(i));

     return o;
    }

    private static List exampleRemove(int cnt)
    {
         List p = new List();
         for(int i =0; i < cnt ; i++)
             p.Add(new Integer(i));

        for(int i = 0 ; i < p.Size();i++)
            if(i%2 != 0 )
                p.Remove(i);
           
        return p;
    }

     public static void example(int total,int cnt)
    {
         double time01,time02;
            time01 = System.currentTimeMillis();
            for(int i =0; i < total / cnt; i ++)
                exampleNew(cnt);
           time02 = System.currentTimeMillis();
         
           double time03 = time02-time01;
           time01 = System.currentTimeMillis();
           for(int i =0; i < total/cnt;i++)
                 exampleRemove(cnt);
           time02 = System.currentTimeMillis();

           double time04 = time02-time01

           System.out.println("List Size : " + cnt);
           System.out.println("using New List:" + time03+"ms");
           System.out.println("using Remove:"+time04+"ms");
           System.out.println("New ---- Remove" + time03/time04+ " :1");
    }



     public static void main(String[] args)
    {
         double time01,time02;
          int total = 100*1000;
          int cnt=1;
          while(cnt <= total)
          {
             example(total,cnt);
             cnt *=10;
          }
    }

 
}

--------------------------------------------------------------------------------------------------
Result:
      When ArrayList is equal or bigger then 1000,the effective will have big different,
therefore, Use Add Metod is greater then Remove Method.