Пример #1
0
        /// <summary>
        /// Compute the min DTW distance between seq2 and all possible endings of seq1.
        /// </summary>
        /// <param name="seq1">The master array of sequences to compare</param>
        /// <param name="seq2">The learner array of sequences to compare</param>
        /// <returns>The best match</returns>
        public double DtwComputation(ArrayList seq1, ArrayList seq2, ArrayList learnerframe, ArrayList learnercolorframe, string path, double anglethreshold)
        {
            // Init
            var seq1R = new ArrayList(seq1);
            //seq1R.Reverse();
            var seq2R = new ArrayList(seq2);

            var learnerf = new ArrayList();

            var learnerc = new ArrayList();
            //seq2R.Reverse();

            var tab = new double[seq1R.Count, seq2R.Count];

            for (int i = 0; i < seq1R.Count; i++)
            {
                for (int j = 0; j < seq2R.Count; j++)
                {
                    //tab[i, j] = Double.PositiveInfinity;
                    tab[i, j] = Marker((System.Windows.Point[])seq1R[i], (System.Windows.Point[])seq2R[j], anglethreshold);
                }
            }
            tab[0, 0] = 0;

            if (_learnerskeletonstream != null)
                _learnerskeletonstream.Close();
            while (FileDelete(@path + "LearnerSelected")) ;
            _learnerskeletonstream = File.Create(@path + "LearnerSelected");
            _lrecorder = new KinectRecorder(KinectRecordOptions.Skeletons, _learnerskeletonstream);

            if (_learnercolorstream != null)
                _learnercolorstream.Close();
            while (FileDelete(@path + "LearnerSelectedColor")) ;
            _learnercolorstream = File.Create(@path + "LearnerSelectedColor");
            _lcolorrecorder = new BinaryWriter( _learnercolorstream);

            int []max_slope = new int [3];

            // Dynamic computation of the DTW matrix.
            for (int i = 1; i < seq1R.Count; i++)
            {
                for (int j = 1; j < seq2R.Count; j++)
                {
                    switch (min(tab[i, j - 1], tab[i - 1, j - 1], tab[i - 1, j]))
                    {
                        case 1:
                            if(max_slope[0]++ %6 != 0)
                             tab[i, j] = Marker((System.Windows.Point[])seq1R[i], (System.Windows.Point[])seq2R[j], anglethreshold) + tab[i, j - 1];
                            else tab[i, j] = Marker((System.Windows.Point[])seq1R[i], (System.Windows.Point[])seq2R[j], anglethreshold) + tab[i - 1, j - 1];
                            max_slope[1] = 0;
                            max_slope[2] = 0;
                            break;
                        case 2:
                            tab[i, j] = Marker((System.Windows.Point[])seq1R[i], (System.Windows.Point[])seq2R[j], anglethreshold) + tab[i - 1, j - 1];
                            max_slope[0] = 0;
                            max_slope[2] = 0;
                            break;
                        case 3:
                            if (max_slope[2]++ % 6 != 0)
                            tab[i, j] = Marker((System.Windows.Point[])seq1R[i], (System.Windows.Point[])seq2R[j], anglethreshold) + tab[i - 1, j - 1];
                            else tab[i, j] = Marker((System.Windows.Point[])seq1R[i], (System.Windows.Point[])seq2R[j], anglethreshold) + tab[i - 1, j];
                            max_slope[0] = 0;
                            max_slope[1] = 0;
                            break;
                        default:
                            tab[i, j] = double.PositiveInfinity;
                            break;
                    }
                }
            }

            int totalframe = 0;
            int correctframe = 0;
            bool chosen = false;
            //Reconstruct the best matched path  //int currentI = bestMatchI;
            //int currentJ = bestMatchI;
            int currentI = seq1R.Count - 1;
            int currentJ = seq2R.Count - 1;
            while (currentI != 0 && currentJ != 0)
            {
                //Console.WriteLine(target.I + " " + target.J);
                switch (min(tab[currentI, currentJ - 1], tab[currentI - 1, currentJ - 1], tab[currentI - 1, currentJ]))
                {
                    case 1:
                        currentJ--;
                        chosen = false;
                        break;
                    case 2:
                        currentI--;
                        currentJ--;
                        learnerf.Add((SkeletonFrame)learnerframe[currentJ]);
                        if (currentJ < learnercolorframe.Count) learnerc.Add((int)learnercolorframe[currentJ]);
                        correctframe++;
                        chosen = false;
                        break;
                    case 3:
                        currentI--;
                        learnerf.Add((SkeletonFrame)learnerframe[currentJ]);
                        if(currentJ < learnercolorframe.Count) learnerc.Add((int)learnercolorframe[currentJ]);// sometimes the color image frame number is smaller
                        if(!chosen) correctframe++;
                        chosen = true;
                        break;
                    default:
                        currentJ--;
                        chosen = false;
                        break;
                }
                totalframe++;
            }

            while (learnerf.Count != 0)
            {
                _lrecorder.Record((SkeletonFrame)learnerf[learnerf.Count - 1]);
                learnerf.RemoveAt(learnerf.Count - 1);
            }

            using (FileStream fs = File.OpenRead(".\\colorStream"))
            {
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    int temp = reader.ReadInt32();
                    _lcolorrecorder.Write(temp);
                    CreateFromReader(reader);
                    while (learnerc.Count != 0)
                    {
                        if (FrameNumber == (int)learnerc[learnerc.Count - 1])
                        {
                            Record(_lcolorrecorder);
                            learnerc.RemoveAt(learnerc.Count - 1);
                        }
                        else
                        {
                            CreateFromReader(reader);
                        }
                    }
                }
            }

            _learnerskeletonstream.Close();
            _lrecorder.Stop();
            _learnercolorstream.Close();
            _lcolorrecorder.Close();
            return (Double)correctframe / (Double)totalframe * 100;
        }