Beispiel #1
0
        public Window1()
        {
            

            colorWindow = new MainWindow();
            nui = Runtime.Kinects[0];
            nui.Initialize(RuntimeOptions.UseColor);
            nui.NuiCamera.ElevationAngle = 0;

            


            // initialize grid
            gridSettings = new GridSettings(colorWindow.Width, colorWindow.Height);
            toleranceSettings = new ToleranceSettings();
            calibrationSettings = new CalibrationSettings();

            smoothSkeleton = new SmoothSkeleton();
       
            this.updateGrid();

            // this will set default crosshair heights
            colorWindow.changeRotationMode(gridSettings.rotation);

            InitializeComponent();

            Screen[] screens = Screen.AllScreens;
            findMonitorSizes(screens);
            
            // set windows' start positions
            colorWindow.WindowStartupLocation = WindowStartupLocation.Manual;
                // put the color window inside the secondary screen's bounds
            colorWindow.Top = screens[1].Bounds.Top;
            colorWindow.Left = screens[1].Bounds.Left;
            
            
            colorWindow.Show();



            toleranceOption1.IsChecked = true;
            gridSettings.setRotationMode(1);
            toggleRotationMode();

            
            
        }
Beispiel #2
0
        void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {

            SkeletonFrame allSkeletons = e.SkeletonFrame;
            // allSkeletons is not null iff allSkeletons has a skeleton?
            if (allSkeletons != null)
            {
                skeleton = (from s in allSkeletons.Skeletons
                            where s.TrackingState == SkeletonTrackingState.Tracked
                            select s).FirstOrDefault();

                if (skeleton != null)
                {
                    // unsmoothed y-values
                    var leftHandY = skeleton.Joints[JointID.HandLeft].ScaleTo(dev_scaledDepthCamWidth, dev_scaledDepthCamHeight, .5f, .5f).Position.Y;
                    var rightHandY = skeleton.Joints[JointID.HandRight].ScaleTo(dev_scaledDepthCamWidth, dev_scaledDepthCamHeight, .5f, .5f).Position.Y;


                    if (allSkeletons.FrameNumber % 1 == 0)              // how many skeletons/second should be looked at and smoothed?
                    {
                        // initial skeleton
                        if (smoothSkeleton == null)
                        {
                            smoothSkeleton = new SmoothSkeleton(leftHandY, rightHandY);
                        }
                        // process every new skeleton
                        else
                        {
                            smoothSkeleton.update(leftHandY, rightHandY);
                        }

                        // 
                        // Console.Write(allSkeletons.FrameNumber + " " + allSkeletons.TimeStamp + " ");

                        // set background color
                        //SetColor(proportion, smoothSkeleton);
                        colorWindow.changeColor(toleranceSettings.getCurrentColor(smoothSkeleton));
                        if (CalibrationSettings.isCalibrated) 
                            colorWindow.setCrosshairs(toleranceSettings.calibratedLeftHandPos, toleranceSettings.calibratedRightHandPos);
                    }
                }
            }
        }
        //private void SetColor(double setProportion, SkeletonData skeleton)
        private void SetColor(double setProportion, SmoothSkeleton skeleton)
        {


            // top of screen = 0
            // bottom of screen = 480
            double leftHandPosition = skeleton.leftOutput;
            double rightHandPosition = skeleton.rightOutput;



            crosshair1.SetValue(Canvas.BottomProperty, Math.Min(0, leftHandPosition - calibrationBaseline) * crosshairRate);
            crosshair2.SetValue(Canvas.BottomProperty, Math.Min(0, rightHandPosition - calibrationBaseline) * crosshairRate);


            double calibratedLeftHandPosition = leftHandPosition - calibrationBaseline;
            double calibratedRightHandPosition = rightHandPosition - calibrationBaseline;
            if (calibratedRightHandPosition == 0)
            {
                calibratedRightHandPosition += .0001;
            }

            // TODO Don't divide by zero
            double currentProportion = calibratedLeftHandPosition / calibratedRightHandPosition;


            // tolerance settings
            if (toleranceMode == true)            // relative tolerance is enabled
            {
                toleranceLeftHand = Math.Abs(leftHandPosition - 480);
                toleranceRightHand = Math.Abs(rightHandPosition - 480);

                if (toleranceLeftHand > toleranceRightHand)
                {
                    maxHandPosition = toleranceLeftHand;
                }
                else
                {
                    maxHandPosition = toleranceRightHand;
                }

                tolerance = (maxHandPosition * ((maxTolerance - minTolerance) / 480) + minTolerance) / 100;       // dimensions

                sendMessage("tolerance: " + tolerance.ToString());
            }


            // display heights
            textBlock1.Text = currentProportion.ToString();

            // determine gradient points
            double bottomTolerance = setProportion - (tolerance * 1/2);
            double topTolerance = setProportion + (tolerance * 1/2);
            double topYellow = setProportion + (tolerance * 1 / 2) + greenYellowFade;
            double bottomYellow = setProportion - (tolerance * 1 / 2) - greenYellowFade;
            double topRed = setProportion + (tolerance * 1 / 2) + greenYellowFade + yellowRedFade;
            double bottomRed = setProportion - (tolerance * 1 / 2) - greenYellowFade - yellowRedFade;

            // determine gradient mappings
            // slope*currentProportion - y-intercept
            double topToleranceYellowMap = ((1 / (greenYellowFade)) * currentProportion) - ((1 / (greenYellowFade)) * topTolerance);
            double topYellowRedMap = ((1 / (yellowRedFade)) * currentProportion) - ((1 / (greenYellowFade)) * topYellow);
            double bottomYellowToleranceMap = ((1 / (greenYellowFade)) * currentProportion) - ((1 / (greenYellowFade)) * bottomYellow);
            double bottomRedYellowMap = ((1 / (yellowRedFade)) * currentProportion) - ((1 / (greenYellowFade)) * bottomRed);




            System.Windows.Media.Color interpolatedColor = Colors.Red;
            if (currentProportion >= bottomTolerance &&
                currentProportion <= topTolerance)   // green
            {
                interpolatedColor = Colors.Green;
            }


            // green to yellow (top)
            else if (currentProportion > topTolerance &&
                currentProportion <= topYellow)
            {
                interpolatedColor = ColorInterpolator.InterpolateBetween(Colors.Green, Colors.Yellow, topToleranceYellowMap);
            }
            // green to yellow (bottom)
            else if (currentProportion < bottomTolerance &&
                currentProportion >= bottomYellow)
            {
                interpolatedColor = ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Green, bottomYellowToleranceMap);
            }


            // yellow to red (top) 
            // (.57, .59]
            else if (currentProportion > topYellow &&
                currentProportion <= topRed)
            {
                interpolatedColor = ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Red, topYellowRedMap);
            }
            // yellow to red (bottom)
            else if (currentProportion < bottomYellow &&
                currentProportion >= bottomRed)
            {
                interpolatedColor = ColorInterpolator.InterpolateBetween(Colors.Red, Colors.Yellow, bottomRedYellowMap);
            }
            else
                interpolatedColor = Colors.Red;

            //interpolatedColor = ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Green, currentProportion);
            SolidColorBrush interpolatedColorBrush = new SolidColorBrush(interpolatedColor);

            MainCanvas.Background = interpolatedColorBrush;      // set Background to a colorString, e.g. "#113355FF"
        }
Beispiel #4
0
        /*
        /// <summary>
        /// deprecated. application doesn't use this method anymore.
        /// </summary>
        private void createColorArray()
        {
            int arraySize = Convert.ToInt32((topRed - bottomRed) / arrayPrecision);
            brushArray = new SolidColorBrush[arraySize];

            int arrayIndex = 0;
            int yellowRedFadeColors = Convert.ToInt32(yellowRedFade / arrayPrecision);
            int greenYellowFadeColors = Convert.ToInt32(greenYellowFade / arrayPrecision);
            int greenColors = Convert.ToInt32(tolerance / arrayPrecision);
            
            // red to yellow
            for (double map = 0, index = arrayIndex; index < yellowRedFadeColors; map += (arrayPrecision/yellowRedFade), index++)
            {
                map = Math.Round(map, 3);
                Color color = ColorInterpolator.InterpolateBetween(Colors.Red, Colors.Yellow, map);
                arrayIndex = Convert.ToInt32(index);
                SolidColorBrush newBrush = new SolidColorBrush(color);
                brushArray[arrayIndex] = newBrush;
                brushArray[arraySize - arrayIndex - 1] = newBrush;
            }
            // at this point, arrayIndex = yellowRedFadeColors
           
            // yellow to green
            for (double map = 0, index = arrayIndex; index < yellowRedFadeColors + greenYellowFadeColors; map += (arrayPrecision / greenYellowFade), index++)
            {
                map = Math.Round(map, 3);
                Color color = ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Green, map);
                arrayIndex = Convert.ToInt32(index);
                SolidColorBrush newBrush = new SolidColorBrush(color);
                brushArray[arrayIndex] = newBrush;
                brushArray[arraySize - arrayIndex - 1] = newBrush;
            }
            // at this point, arrayIndex = yellowRedFadeColors + greenYellowFadeColors
            
            // half of green
            
            for (double map = 0, index = arrayIndex; index < yellowRedFadeColors + greenYellowFadeColors + greenColors; map += (arrayPrecision / greenYellowFade), index++)
            {
                arrayIndex = Convert.ToInt32(index);
                brushArray[arrayIndex] = greenBrush;
            }
        }
         * */

        
        
        public SolidColorBrush getCurrentColor(SmoothSkeleton skeleton)
        {
            // top of screen = 0
            // bottom of screen = 480
            double leftHandPosition = skeleton.leftOutput;
            double rightHandPosition = skeleton.rightOutput;

            calibratedLeftHandPos = -(leftHandPosition - CalibrationSettings.calibrationBaseline);
            if (calibratedLeftHandPos == 0)
            {
                calibratedLeftHandPos += .0001;
            }

            calibratedRightHandPos = -(rightHandPosition - CalibrationSettings.calibrationBaseline);
            if (calibratedRightHandPos == 0)
            {
                calibratedRightHandPos += .0001;
            }

            // SET THE CROSSHAIRS
            

            double currentProportion = Math.Round(calibratedLeftHandPos / calibratedRightHandPos, 3);
            if (currentProportion > 2)
            {
                // debug
            }

            // implement relative tolerance

            // display current proportion

            // proportional tolerance
            if (toleranceMode == false)
            {

                if (currentProportion < topTolerance && currentProportion >= bottomTolerance)
                {
                    return greenBrush;
                }

                // bottom red -> yellow
                else if (currentProportion >= bottomRed && currentProportion < bottomYellow)
                {
                    //int index = Convert.ToInt32((currentProportion - bottomRed) * (1 / arrayPrecision)) - 1;
                    //return brushArray[index];
                    double map = (1 / proportionalYellowRedFade) * (currentProportion - bottomRed);
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Red, Colors.Yellow, map));

                }
                // bottom yellow -> green
                else if (currentProportion >= bottomYellow && currentProportion <= bottomTolerance)
                {
                    double map = (1 / proportionalGreenYellowFade) * (currentProportion - bottomYellow);
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Green, map));
                }
                // top green -> yellow
                else if (currentProportion >= topTolerance && currentProportion < topYellow)
                {
                    double map = (1 / proportionalGreenYellowFade) * (currentProportion - topTolerance);
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Green, Colors.Yellow, map));
                }
                // top yellow -> red
                else if (currentProportion >= topYellow && currentProportion < topRed)
                {
                    double map = (1 / proportionalYellowRedFade) * (currentProportion - topYellow);
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Red, map));
                }
                else return redBrush;
            }


            // absolute tolerance:
            // height region that the lower hand moves through where screen is green. e.g., if absolute tolerance = 10,
            // and the proportion is 1:2 (left:right), when the right is at 200, the left hand can be anywhere between 
            // (100 - 10) and (100 + 10).
            else
            {
                
                
                // figure out the height of the highest hand
                //double higherHandHeight = Math.Max(calibratedLeftHandPos, calibratedRightHandPos);
                //double lowerHandHeight = Math.Min(calibratedLeftHandPos, calibratedRightHandPos);

                //
                double heightThatLeftHandShouldBeAt = proportion * calibratedRightHandPos;
                double upperAbsoluteToleranceBound = heightThatLeftHandShouldBeAt + absoluteTolerance;
                double lowerAbsoluteToleranceBound = heightThatLeftHandShouldBeAt - absoluteTolerance;

                topYellow = upperAbsoluteToleranceBound + absoluteGreenYellowFade;
                topRed = upperAbsoluteToleranceBound + absoluteGreenYellowFade + absoluteYellowRedFade;

                bottomYellow = lowerAbsoluteToleranceBound - absoluteGreenYellowFade;
                bottomRed = lowerAbsoluteToleranceBound - absoluteGreenYellowFade - absoluteYellowRedFade;


                if (calibratedLeftHandPos < upperAbsoluteToleranceBound 
                    && calibratedLeftHandPos > lowerAbsoluteToleranceBound)
                {
                    return greenBrush;
                }

                // bottom red -> yellow
                else if (calibratedLeftHandPos > bottomRed
                    && calibratedLeftHandPos < bottomYellow)
                {
                    double map = (calibratedLeftHandPos - bottomRed) / absoluteYellowRedFade;
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Red, Colors.Yellow, map));
                }

                // bottom yellow -> green
                else if (calibratedLeftHandPos > bottomYellow
                    && calibratedLeftHandPos < lowerAbsoluteToleranceBound)
                {
                    double map = (calibratedLeftHandPos - bottomYellow) / absoluteGreenYellowFade;
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Green, map));
                }

                // top green -> yellow
                else if (calibratedLeftHandPos > upperAbsoluteToleranceBound
                    && calibratedLeftHandPos < topYellow)
                {
                    double map = (calibratedLeftHandPos - upperAbsoluteToleranceBound) / absoluteGreenYellowFade;
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Green, Colors.Yellow, map));
                }
                // top yellow -> red
                else if (calibratedLeftHandPos > topYellow
                    && calibratedLeftHandPos < topRed)
                {
                    double map = (calibratedLeftHandPos - topYellow) / absoluteYellowRedFade;
                    return new SolidColorBrush(ColorInterpolator.InterpolateBetween(Colors.Yellow, Colors.Red, map));
                }
                else return redBrush;
            }
        }
        void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {

            SkeletonFrame allSkeletons = e.SkeletonFrame;
            //if (allSkeletons != null)
            //{
                // allSkeletons is not null iff allSkeletons has a skeleton?
                skeleton = (from s in allSkeletons.Skeletons                
                            where s.TrackingState == SkeletonTrackingState.Tracked
                            select s).FirstOrDefault();

                if (skeleton != null)
                {
                    var leftHandY = skeleton.Joints[JointID.HandLeft].ScaleTo(640, 480, .5f, .5f).Position.Y;
                    var rightHandY = skeleton.Joints[JointID.HandRight].ScaleTo(640, 480, .5f, .5f).Position.Y;


                    if (allSkeletons.FrameNumber % 1 == 0)              // how many skeletons/second should be looked at and smoothed?
                    {
                        // initial skeleton
                        if (smoothSkeleton == null)
                        {
                            smoothSkeleton = new SmoothSkeleton(leftHandY, rightHandY);
                        }
                        // process every new skeleton
                        else
                        {
                            smoothSkeleton.update(leftHandY, rightHandY);
                        }

                        // 
                        // Console.Write(allSkeletons.FrameNumber + " " + allSkeletons.TimeStamp + " ");

                        // set background color
                        SetColor(proportion, smoothSkeleton);
                    }
                }
            //}
        }