c# - Kinect constantly increasing joint position value -
i have application in c# skeleton tracking. showing (visualizing) skeleton properly, when want show actual value of chosen joint, not working right.
example: in app have button should actual y position of hip center , textbox value should displayed. standing still in front of kinect , everytime click button, value changes (even though not moving).
click : -56,236589
click : -89,214563
click : -100,254789
and on.
below full source code of app:
using system; using system.collections.generic; using system.linq; using system.windows; using system.windows.media; using microsoft.kinect; using system.io; using encog.neural.networks; using encog.neural.networks.layers; using encog.engine.network.activation; using encog.ml.data; using encog.neural.networks.training.propagation.resilient; using encog.ml.train; using encog.ml.data.basic; using encog.ml.svm; using encog.ml.svm.training; namespace wpfapplication1 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { kinectsensor sensor = kinectsensor.kinectsensors[0]; private double shoulderrighty; private double shoulderlefty; private double heady; private double hipy; joint rightshoulder = new joint(); joint leftshoulder = new joint(); joint head = new joint(); joint hip = new joint(); #region "variables" /// <summary> /// thickness of body center ellipse /// </summary> private const double bodycenterthickness = 10; /// <summary> /// thickness of clip edge rectangles /// </summary> private const double clipboundsthickness = 10; /// <summary> /// brush used draw skeleton center point /// </summary> private readonly brush centerpointbrush = brushes.blue; /// <summary> /// brush used drawing joints tracked /// </summary> private readonly brush trackedjointbrush = new solidcolorbrush(color.fromargb(255, 68, 192, 68)); /// <summary> /// brush used drawing joints inferred /// </summary> private readonly brush inferredjointbrush = brushes.yellow; /// <summary> /// pen used drawing bones tracked /// </summary> private readonly pen trackedbonepen = new pen(brushes.green, 6); /// <summary> /// pen used drawing bones inferred /// </summary> private readonly pen inferredbonepen = new pen(brushes.gray, 1); /// <summary> /// drawing image display /// </summary> private drawingimage imagesource; /// <summary> /// thickness of drawn joint lines /// </summary> private const double jointthickness = 3; /// <summary> /// drawing group skeleton rendering output /// </summary> private drawinggroup drawinggroup; /// <summary> /// width of output drawing /// </summary> private const float renderwidth = 640.0f; /// <summary> /// height of our output drawing /// </summary> private const float renderheight = 480.0f; #endregion public mainwindow() { initializecomponent(); //after initialization subscribe loaded event of form loaded += mainwindow_loaded; //after initialization subscribe unloaded event of form //we use event stop sensor when application being closed. unloaded += mainwindow_unloaded; } void mainwindow_unloaded(object sender, routedeventargs e) { //stop sestor sensor.stop(); } void mainwindow_loaded(object sender, routedeventargs e) { //create drawing group used drawing this.drawinggroup = new drawinggroup(); //create image source display our skeleton this.imagesource = new drawingimage(this.drawinggroup); //display image in our image control image.source = imagesource; try { //check if sensor connected if (sensor.status == kinectstatus.connected) { //start sensor sensor.start(); //tell kinect sensor use default mode(human skeleton standing) || seated(human skeleton sitting down) sensor.skeletonstream.trackingmode = skeletontrackingmode.default; //subscribe te sensor's skeletonframeready event track joins , create joins display on our image control sensor.skeletonframeready += sensor_skeletonframeready; //nice message colors alert if sensor working or not message.text = "kinect ready"; message.background = new solidcolorbrush(colors.green); message.foreground = new solidcolorbrush(colors.white); // turn on skeleton stream receive skeleton frames this.sensor.skeletonstream.enable(); } else if (sensor.status == kinectstatus.disconnected) { //nice message colors alert if sensor working or not message.text = "kinect sensor not connected"; message.background = new solidcolorbrush(colors.orange); message.foreground = new solidcolorbrush(colors.black); } else if (sensor.status == kinectstatus.notpowered) {//nice message colors alert if sensor working or not message.text = "kinect sensor not powered"; message.background = new solidcolorbrush(colors.red); message.foreground = new solidcolorbrush(colors.black); } } catch (exception ex) { messagebox.show(ex.message); } } /// <summary> //when skeleton ready must draw skeleton /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void sensor_skeletonframeready(object sender, skeletonframereadyeventargs e) { //declare array of skeletons skeleton[] skeletons = new skeleton[1]; //opens skeletonframe object, contains 1 frame of skeleton data. using (skeletonframe skeletonframe = e.openskeletonframe()) { //check if frame indeed open if (skeletonframe != null) { skeletons = new skeleton[skeletonframe.skeletonarraylength]; // copies skeleton data array of skeletons, each skeleton contains collection of joints. skeletonframe.copyskeletondatato(skeletons); //draw skeleton based on default mode(standing), "seated" if (sensor.skeletonstream.trackingmode == skeletontrackingmode.default) { //draw standing skeleton drawstandingskeletons(skeletons); } else if (sensor.skeletonstream.trackingmode == skeletontrackingmode.seated) { //draw seated skeleton 10 joints drawseatedskeletons(skeletons); } } } foreach (skeleton skeleton in skeletons) { if (skeleton != null) { hip = skeleton.joints[jointtype.hipcenter]; rightshoulder = skeleton.joints[jointtype.shoulderright]; leftshoulder = skeleton.joints[jointtype.shoulderleft]; head = skeleton.joints[jointtype.head]; shoulderrighty += rightshoulder.position.y; shoulderlefty += leftshoulder.position.y; heady += head.position.y; hipy += hip.position.y; } } } // button click method private void stoji_click(object sender, routedeventargs e) { file.appendalltext(@"e:\kinect\inputs.txt", shoulderrighty + " " + shoulderlefty + " " + heady + " " + hipy + environment.newline); file.appendalltext(@"e:\kinect\outputs.txt", "1" + environment.newline); } private void sedi_click(object sender, routedeventargs e) { file.appendalltext(@"e:\kinect\inputs.txt", shoulderrighty + " " + shoulderlefty + " " + heady + " " + hipy + environment.newline); file.appendalltext(@"e:\kinect\outputs.txt", "2" + environment.newline); } private void lezi_click(object sender, routedeventargs e) { file.appendalltext(@"e:\kinect\inputs.txt", shoulderrighty + " " + shoulderlefty + " " + heady + " " + hipy + environment.newline); file.appendalltext(@"e:\kinect\outputs.txt", "3" + environment.newline); } //thi function draws standing or default skeleton private void drawstandingskeletons(skeleton[] skeletons) { using (drawingcontext dc = this.drawinggroup.open()) { //draw transparent background set render size or our canvas dc.drawrectangle(brushes.black, null, new rect(0.0, 0.0, renderwidth, renderheight)); //if skeleton array has items if (skeletons.length != 0) { //loop through skeleton joins foreach (skeleton skel in skeletons) { renderclippededges(skel, dc); if (skel.trackingstate == skeletontrackingstate.tracked) { this.drawbonesandjoints(skel, dc); } else if (skel.trackingstate == skeletontrackingstate.positiononly) { dc.drawellipse(this.centerpointbrush, null, this.skeletonpointtoscreen(skel.position), bodycenterthickness, bodycenterthickness); } } } //prevent drawing outside canvas this.drawinggroup.clipgeometry = new rectanglegeometry(new rect(0.0, 0.0, renderwidth, renderheight)); } } private void drawseatedskeletons(skeleton[] skeletons) { using (drawingcontext dc = this.drawinggroup.open()) { //draw transparent background set render size dc.drawrectangle(brushes.black, null, new rect(0.0, 0.0, renderwidth, renderheight)); if (skeletons.length != 0) { foreach (skeleton skel in skeletons) { renderclippededges(skel, dc); if (skel.trackingstate == skeletontrackingstate.tracked) { this.drawbonesandjoints(skel, dc); } else if (skel.trackingstate == skeletontrackingstate.positiononly) { dc.drawellipse(this.centerpointbrush, null, this.skeletonpointtoscreen(skel.position), bodycenterthickness, bodycenterthickness); } } } //prevent drawing outside canvas this.drawinggroup.clipgeometry = new rectanglegeometry(new rect(0.0, 0.0, renderwidth, renderheight)); } } /// <summary> /// draws indicators show edges clipping skeleton data /// </summary> /// <param name="skeleton">skeleton draw clipping information for</param> /// <param name="drawingcontext">drawing context draw to</param> private static void renderclippededges(skeleton skeleton, drawingcontext drawingcontext) { if (skeleton.clippededges.hasflag(frameedges.bottom)) { drawingcontext.drawrectangle( brushes.red, null, new rect(0, renderheight - clipboundsthickness, renderwidth, clipboundsthickness)); } if (skeleton.clippededges.hasflag(frameedges.top)) { drawingcontext.drawrectangle( brushes.red, null, new rect(0, 0, renderwidth, clipboundsthickness)); } if (skeleton.clippededges.hasflag(frameedges.left)) { drawingcontext.drawrectangle( brushes.red, null, new rect(0, 0, clipboundsthickness, renderheight)); } if (skeleton.clippededges.hasflag(frameedges.right)) { drawingcontext.drawrectangle( brushes.red, null, new rect(renderwidth - clipboundsthickness, 0, clipboundsthickness, renderheight)); } } /// <summary> /// draws skeleton's bones , joints /// </summary> /// <param name="skeleton">skeleton draw</param> /// <param name="drawingcontext">drawing context draw to</param> private void drawbonesandjoints(skeleton skeleton, drawingcontext drawingcontext) { // render torso this.drawbone(skeleton, drawingcontext, jointtype.head, jointtype.shouldercenter); this.drawbone(skeleton, drawingcontext, jointtype.shouldercenter, jointtype.shoulderleft); this.drawbone(skeleton, drawingcontext, jointtype.shouldercenter, jointtype.shoulderright); this.drawbone(skeleton, drawingcontext, jointtype.shouldercenter, jointtype.spine); this.drawbone(skeleton, drawingcontext, jointtype.spine, jointtype.hipcenter); this.drawbone(skeleton, drawingcontext, jointtype.hipcenter, jointtype.hipleft); this.drawbone(skeleton, drawingcontext, jointtype.hipcenter, jointtype.hipright); // left arm this.drawbone(skeleton, drawingcontext, jointtype.shoulderleft, jointtype.elbowleft); this.drawbone(skeleton, drawingcontext, jointtype.elbowleft, jointtype.wristleft); this.drawbone(skeleton, drawingcontext, jointtype.wristleft, jointtype.handleft); // right arm this.drawbone(skeleton, drawingcontext, jointtype.shoulderright, jointtype.elbowright); this.drawbone(skeleton, drawingcontext, jointtype.elbowright, jointtype.wristright); this.drawbone(skeleton, drawingcontext, jointtype.wristright, jointtype.handright); // left leg this.drawbone(skeleton, drawingcontext, jointtype.hipleft, jointtype.kneeleft); this.drawbone(skeleton, drawingcontext, jointtype.kneeleft, jointtype.ankleleft); this.drawbone(skeleton, drawingcontext, jointtype.ankleleft, jointtype.footleft); // right leg this.drawbone(skeleton, drawingcontext, jointtype.hipright, jointtype.kneeright); this.drawbone(skeleton, drawingcontext, jointtype.kneeright, jointtype.ankleright); this.drawbone(skeleton, drawingcontext, jointtype.ankleright, jointtype.footright); // render joints foreach (joint joint in skeleton.joints) { brush drawbrush = null; if (joint.trackingstate == jointtrackingstate.tracked) { drawbrush = this.trackedjointbrush; } else if (joint.trackingstate == jointtrackingstate.inferred) { drawbrush = this.inferredjointbrush; } if (drawbrush != null) { drawingcontext.drawellipse(drawbrush, null, this.skeletonpointtoscreen(joint.position), jointthickness, jointthickness); } } } /// <summary> /// draws bone line between 2 joints /// </summary> /// <param name="skeleton">skeleton draw bones from</param> /// <param name="drawingcontext">drawing context draw to</param> /// <param name="jointtype0">joint start drawing from</param> /// <param name="jointtype1">joint end drawing at</param> private void drawbone(skeleton skeleton, drawingcontext drawingcontext, jointtype jointtype0, jointtype jointtype1) { joint joint0 = skeleton.joints[jointtype0]; joint joint1 = skeleton.joints[jointtype1]; // if can't find either of these joints, exit if (joint0.trackingstate == jointtrackingstate.nottracked || joint1.trackingstate == jointtrackingstate.nottracked) { return; } // don't draw if both points inferred if (joint0.trackingstate == jointtrackingstate.inferred && joint1.trackingstate == jointtrackingstate.inferred) { return; } // assume drawn bones inferred unless both joints tracked pen drawpen = this.inferredbonepen; if (joint0.trackingstate == jointtrackingstate.tracked && joint1.trackingstate == jointtrackingstate.tracked) { drawpen = this.trackedbonepen; } drawingcontext.drawline(drawpen, this.skeletonpointtoscreen(joint0.position), this.skeletonpointtoscreen(joint1.position)); } /// <summary> /// maps skeletonpoint lie within our render space , converts point /// </summary> /// <param name="skelpoint">point map</param> /// <returns>mapped point</returns> private point skeletonpointtoscreen(skeletonpoint skelpoint) { // convert point depth space. // not using depth directly, want points in our 640x480 output resolution. depthimagepoint depthpoint = this.sensor.coordinatemapper.mapskeletonpointtodepthpoint(skelpoint, depthimageformat.resolution640x480fps30); return new point(depthpoint.x, depthpoint.y); } private void classify_click(object sender, routedeventargs e) { double detect=program.siet(); vysledok.text = detect.tostring(); } private void testexample_click(object sender, routedeventargs e) { /* if (file.exists(@"e:\kinect\test.txt")) { file.delete(@"e:\kinect\test.txt"); }*/ file.appendalltext(@"e:\kinect\test.txt", shoulderrighty + " " + shoulderlefty + " " + heady + " " + hipy + environment.newline); } private void deletedata_click(object sender, routedeventargs e) { file.delete(@"e:\kinect\inputs.txt"); file.delete(@"e:\kinect\outputs.txt"); } private void vymaztest_click(object sender, routedeventargs e) { file.delete(@"e:\kinect\test.txt"); file.delete(@"e:\kinect\testout.txt"); } private void yhip_click(object sender, routedeventargs e) { ybox.text = hipy.tostring(); } } }
can pleeease tell me can fix bug? thank you
you use
hipy += hip.position.y
unless stand accurate @ center (position.y = 0) correct, hipy changes every time.
i think want
hipy = hip.position.y
and way:
- you should ensure skeleton tracked , hip joint tracked when set hip position.
- you should ensure track same skeleton when set hip position.
Comments
Post a Comment