Example #1
0
        /// <summary>
        /// Clustering the normal mega blocks of frames using K-means algorithm
        /// and save the codewords (Centers) of the clusters in the disk.
        /// </summary>
        /// <param name="Source_video_path">Path of the video on the disk.</param>
        /// <param name="Codeword_path">Path of the clusters centers on the disk.</param>
        /// <returns></returns>
        public void cluster_video(String Source_video_path, String Codeword_path)
        {
            MotionInfluenceMap  MIG = new MotionInfluenceMap();
            List <double[][][]> MotionInfOfFrames = MIG.get_motion_influence_map(Source_video_path, out xBlockSize, out yBlockSize, out noOfRowInBlock, out noOfColInBlock, out total_frames, true);
            MegaBlocks          MB = new MegaBlocks();

            double[][][][] MegaBlock = MB.createMegaBlocks(MotionInfOfFrames, xBlockSize, yBlockSize, noOfRowInBlock, noOfColInBlock, (int)total_frames);
            double[][][][] codewords = MB.kmeans(MegaBlock, xBlockSize, yBlockSize, (int)total_frames);

            int n   = 2;
            int row = xBlockSize / n + 1;
            int col = yBlockSize / n + 1;

            using (StreamWriter st = new StreamWriter(Codeword_path))
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        for (int q = 0; q < 5; q++)
                        {
                            for (int w = 0; w < 8; w++)
                            {
                                if (w != 7)
                                {
                                    st.Write(codewords[i][j][q][w] + " ");
                                }
                                else
                                {
                                    st.Write(codewords[i][j][q][w]);
                                }
                            }
                            st.WriteLine();
                        }
                    }
                }
        }
Example #2
0
        /// <summary>
        /// Takes tha data and create motion influance map and mega blocks and process them
        /// so we can create min distination matrix and create array of centers of normal frames.
        /// </summary>
        /// <param name="video_path">Path of the video we want to process.</param>
        /// <param name="Codeword_path">Path of the centers of normal events in the train videos.</param>
        /// <param name="frame_number">the number of frame we want to start the processing from.</param>
        /// <returns></returns>
        public AbnormalDetection(String video_path, String Codeword_path, int frame_number)
        {
            fram_num          = frame_number;
            MIG               = new MotionInfluenceMap();
            MotionInfOfFrames = MIG.get_motion_influence_map(video_path, out xBlockSize, out yBlockSize, out noOfRowInBlock, out noOfColInBlock, out total_frames, false, fram_num);
            MB        = new MegaBlocks();
            MegaBlock = MB.createMegaBlocks(MotionInfOfFrames, xBlockSize, yBlockSize, noOfRowInBlock, noOfColInBlock, (int)total_frames);
            n         = 2;
            row       = xBlockSize / n + 1;
            col       = yBlockSize / n + 1;
            cluster_n = 5;
            int i = 0, j = 0, k = 0;

            codewords = new double[row][][][];


            for (int r = 0; r < row; r++)
            {
                codewords[r] = new double[col][][];

                for (int rr = 0; rr < col; rr++)
                {
                    codewords[r][rr] = new double[cluster_n][];
                    for (int rrr = 0; rrr < cluster_n; rrr++)
                    {
                        codewords[r][rr][rrr] = new double[8];
                    }
                }
            }

            minDistMatrix = new double[(int)total_frames][][];

            for (int r = 0; r < total_frames; r++)
            {
                minDistMatrix[r] = new double[row][];
                for (int rr = 0; rr < row; rr++)
                {
                    minDistMatrix[r][rr] = new double[col];
                }
            }


            using (StreamReader reader = new StreamReader(Codeword_path))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    double[] arr = Array.ConvertAll(line.Split(' '), Double.Parse);
                    codewords[i][j][k] = arr;
                    k++;
                    if (k == cluster_n)
                    {
                        k = 0;
                        j++;
                        if (j == col)
                        {
                            j = 0;
                            if (i < row - 1)
                            {
                                i++;
                            }
                        }
                    }
                }
            }
        }