Пример #1
0
 public Load_L2BOF(int classnum, string src)
 {
     SecondLayerDic = null;
     directory = src;
     classNum = classnum;
     bowTrainer = new BOWKMeansTrainer(classNum, new MCvTermCriteria(10, 0.01), 3,
     KMeansInitType.PPCenters);
         IFormatter formatter = new BinaryFormatter();
         FileStream fs = File.OpenRead(directory + "\\obj\\dic.xml");
         SecondLayerDic = (Matrix<float>)formatter.Deserialize(fs);
         fs.Dispose();
         bowDe.SetVocabulary(SecondLayerDic);
         SecondLayerSVM = new SVM();
         SecondLayerSVM.Load(directory + "\\obj\\svm.xml");
         Console.WriteLine("Finished Loading L2 SVM.");loaded = true;
 }
Пример #2
0
        public Load_L1BOF(int classnum,string loc)
        {
            prefix = loc;
            topLayerDic = null;
            classNum = classnum;
            bowTrainer = new BOWKMeansTrainer(classNum, new MCvTermCriteria(10, 0.01), 3,
            KMeansInitType.PPCenters);

                IFormatter formatter = new BinaryFormatter();
                FileStream fs = File.OpenRead(loc + "obj\\dic.xml");
                topLayerDic = (Matrix<float>)formatter.Deserialize(fs);
                fs.Dispose();
                bowDe.SetVocabulary(topLayerDic);
                topLayerSVM = new SVM();
                topLayerSVM.Load(loc + "obj\\svm.xml");
                Console.WriteLine("Finished Loading L1 SVM.");
                loaded = true;
        }
Пример #3
0
        private void button7_Click(object sender, EventArgs e)
        {
            ///////////////////////////////////////////// Test ///////////////////////////////////////
            //Create a matrix
            //Matrix<Byte> matrix1 = new Matrix<Byte>(10, 10);
            //Byte element = 0;
            ////Set the elements
            //for (int i = 0; i < 10; i++)
            //{
            //    for (int j = 0; j < 10; j++)
            //    {
            //        matrix1.Data[i, j] = 1;
            //        //matrix1.Data[i, j] = element;
            //        element++;
            //    }
            //}

            //int WW = matrix1.Cols;
            //int HH = matrix1.Rows;
            ///////////////////////////////////////////// Test ///////////////////////////////////////
            int W = Train_I.Cols;
            int H = Train_I.Rows;
            int rW = (mW - 1) / 2;
            int rH = (mH - 1) / 2;
            double pp = 0;
            double ee = 0;
            int ind = 0;
            int count;
            int maskCenter_H_start = rH + 1;
            int maskCenter_W_start = rW + 1;
            int maskCenter_H_end = H - rH;
            int maskCenter_W_end = W - rW;

            double[] fVave = new double[ ( maskCenter_H_end - maskCenter_H_start) * (maskCenter_W_end - maskCenter_W_start) ];
            double[] fVstd = new double[( maskCenter_H_end - maskCenter_H_start) * (maskCenter_W_end - maskCenter_W_start) ];
            double[] fVentropy = new double[( maskCenter_H_end - maskCenter_H_start) * (maskCenter_W_end - maskCenter_W_start) ];
            classV = new Matrix<float>( (maskCenter_H_end - maskCenter_H_start) * (maskCenter_W_end - maskCenter_W_start), 1);
            
            
            // Mask scan over the whole image  "gR_R_Picked_Map"
            for (int i = maskCenter_H_start; i < maskCenter_H_end; i++)
            {
                for (int j = maskCenter_W_start; j < maskCenter_W_end; j++)
                {
                    double sum = 0;
                    double ave = 0;
                    double sumDiff = 0;
                    double std = 0;
                    entropy = 0;
                    count = 0;
                    // initial value all 0, since every mask should count independently
                    for (int k = 0; k < mH; k++)
                    {
                        for (int l = 0; l < mW; l++)
                        {                         
                            int mask_x = j - rW + l - 1;
                            int mask_y = i - rH + k - 1;
                            sum = sum + gR_R_Picked_Map[ mask_y, mask_x ];         // Bug????
                            //sum = sum + matrix1[i - rH + k - 1  , j - rW + l - 1  ]; // test 

                            // judge mask is PART of TREE or NOT
                            if (Class_I.Data[ mask_y,   mask_x , 0] == 1)
                            {
                                count ++;
                            }
                        }
                    }  // end of mask
                    // To detect the binary values in the mask and record it in classV
                    float maskSampleCount = mH * mW;
                    float mask_plant_percent = (count /maskSampleCount);
                    if (  mask_plant_percent > 0.7 )
                    {
                        classV[ind, 0] = 1;
                    }
                    else
                    {
                        classV[ind, 0] = 0;
                    }
                    // Ave                          
                    ave = sum / (maskSampleCount);
                    // StandardDeviation
                    for (int k = 0; k < mH; k++)
                    {
                        for (int l = 0; l < mW; l++)
                        {
                            int mask_x = j - rW + l - 1;
                            int mask_y = i - rH + k - 1;
                            sumDiff = sumDiff + Math.Pow((gR_R_Picked_Map[ mask_y, mask_x] - ave), 2);
                            // sumDiff = sumDiff + Math.Pow((matrix1[i - rH + k - 1, j - rW + l - 1] - ave), 2); //test
                        }
                    }
                    std = Math.Sqrt(sumDiff / (maskSampleCount - 1));
                    // Entropy
                    for (int k = 1; k < mH; k++)
                    {
                        for (int l = 1; l < mW; l++)
                        {
                            int mask_x = j - rW + l - 1;
                            int mask_y = i - rH + k - 1;
                            pp = gR_R_Picked_Map[mask_y, mask_x] / sum;
                            //p = matrix1[i - rH + k - 1, j - rW + l - 1] / sum;
                            if (pp > 0) // Log p, p > 0
                            {
                                ee = Math.Log(pp);
                            }
                            else
                            {
                                ee = 0;
                            }
                            //System.Console.WriteLine("-");
                            //System.Console.WriteLine(p);
                            //System.Console.WriteLine(ee);
                            //System.Console.WriteLine("-");
                            entropy = entropy + (-(pp * ee));
                        }
                    }
                    fVave[ind] = ave;
                    fVstd[ind] = std;
                    fVentropy[ind] = entropy;
                    ind++;
                } // for i 
            } // for j      

            trainData_all = new Matrix<float>(ind, 3);
            for (int i = 0; i < ind; i++)
            {
                trainData_all.Data[i, 0] = (float)fVave[i];
                trainData_all.Data[i, 1] = (float)fVstd[i];
                trainData_all.Data[i, 2] = (float)fVentropy[i];
            }


            using (Emgu.CV.ML.SVM model = new Emgu.CV.ML.SVM())
            {
                SVMParams p = new SVMParams();
                /* p.KernelType = Emgu.CV.ML.MlEnum.SVM_KERNEL_TYPE.LINEAR;
                 p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.C_SVC;
                 p.C = 1;
                 p.TermCrit = new MCvTermCriteria(100, 0.00001);*/
                p.KernelType = Emgu.CV.ML.MlEnum.SVM_KERNEL_TYPE.RBF;
                p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.C_SVC;
                p.Gamma = 0.1;
                p.Coef0 = 0;
                p.Degree = 2;
                p.C = 1;
                p.TermCrit = new MCvTermCriteria(500, 0.00001); //迭代的終止準則,最大迭代次數500次,結果的精確性

                //bool trained = model.Train(trainData, trainClasses, null, null, p);
                bool trained = model.TrainAuto(trainData_all, classV, null, null, p.MCvSVMParams, 5);
                model.Save(@"D:\Code\Data\train_func.xml");
            }

            System.Console.WriteLine(" END OF button7_Click Masking ");
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            Image<Bgr, byte> predict_image = Train_I2.Clone();
            using (Emgu.CV.ML.SVM model = new Emgu.CV.ML.SVM())
            {
                model.Load(@"D:\Code\Data\train_func.xml");
                int ind_predict = 0;

                for (int i = maskCenter_H_start; i < maskCenter_H_end; i++)
                    for (int j = maskCenter_W_start; j < maskCenter_W_end; j++)
                    {                     
                        Matrix<float> feature_test = TakeRow(trainData_all.Data, ind_predict);
                        //Matrix<float> feature_test = trainData_all.GetRow( ind_predict ) ;
                      // System.Console.WriteLine(feature_test);
                        float predict_class = model.Predict( feature_test );
                        if (predict_class == 1)
                        {
                            for (int k = 0; k < mH; k++)
                                for (int l = 0; l < mW; l++)
                                {
                                    int mask_x = j - rW + l  -1;
                                    int mask_y = i - rH + k  -1;
                                    predict_image.Data[ mask_y, mask_x , 0] = 255;
                                    predict_image.Data[mask_y , mask_x , 2] = 255;
                                }
                        }
                        ind_predict++;
                       // System.Console.WriteLine(predict_class);
                    }  // end of all

                //String name2 = name.Insert(name.Length - 4, "_predict");
                String name3 = "predict";
                predict_image.Save(@"D:\Code\Data\predict.bmp");
                pictureBox9.Image = Image.FromFile(@"D:\Code\Data\predict.bmp");
                // Image<Bgr, byte> predict_image = Train_I2.Clone();
                System.Console.WriteLine(" END OF Predict ");
            }
        }
Пример #4
0
 public static SVM CreateSvm()
 {
     var model = new SVM();
     model.Load(ModelFileName);
     return model;
 }
Пример #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = @"D:\Code\Data";
            try
            {
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    String[] files = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath);

                    foreach (String name in files)
                    {
                        if (name.Contains("_class") )/////////有分類標籤的影像
                        {
                        /*    if (Class_I != null)
                            {
                                Class_I.Dispose(); Class_I = null;
                            }
                            if (Train_I != null)
                            {
                                Train_I.Dispose(); Train_I = null;
                            }
                            //////////
                            String name2 = name.Replace("_class", "");
                            name2 = name2.Replace("bmp", "tif");
                            if (files.Contains(name2))
                            {
                                Class_I = new Image<Bgr, Byte>(name);  //////類別
                                Train_I = new Image<Bgr, Byte>(name2); //////原影像
                                if (trainData_all != null)
                                {
                                    trainData_all.Dispose(); trainData_all = null;
                                }
                                Cal_features();
                                Image<Bgr, byte> predict_image = Train_I.Clone();
                                using (Emgu.CV.ML.SVM model = new Emgu.CV.ML.SVM())
                                {
                                    model.Load(@"D:\程式區\Plant_training\train_func.xml");
                                    for (int i = 0; i < Train_I.Height; i++)
                                        for (int j = 0; j < Train_I.Width; j++)
                                        {
                                            float predict_class = model.Predict(trainData_all.GetRow(i * Train_I.Width + j));
                                            if (predict_class == 1)
                                            {
                                                predict_image.Data[i, j, 0] = 255;
                                                predict_image.Data[i, j, 2] = 255;
                                            }
                                        }
                                }
                                predict_image.Save(@"D:\程式區\Plant_training\predict.tif");
                            }*/
                        }
                        else
                        {
                            if (Train_I != null)
                            {
                                Train_I.Dispose(); Train_I = null;
                            }
                            if (Class_I != null)
                            {
                                Class_I.Dispose(); Class_I = null;
                            }
                            Train_I = new Image<Bgr, Byte>(name).Resize(scale, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); //////原影像
                            if (trainData_all != null)
                            {
                                trainData_all.Dispose(); trainData_all = null;
                            }
                            Cal_features();
                            Image<Bgr, byte> predict_image = Train_I.Clone();
                            using (Emgu.CV.ML.SVM model = new Emgu.CV.ML.SVM())
                            {
                                model.Load(@"D:\Code\Data\train_func.xml");
                                
                                for (int i = 0; i < Train_I.Height; i++)
                                   for (int j = 0; j < Train_I.Width; j++)
                                   {
                                       Matrix<float> feature_test = trainData_all.GetRow(i * Train_I.Width + j);
                                       int aaaaa = 1;
                                       float predict_class = model.Predict(feature_test);
                                        if (predict_class == 1)
                                        {
                                            predict_image.Data[i, j, 0] = 255;
                                            predict_image.Data[i, j, 2] = 255;
                                         }
                                    }
                              }
                              String name2 = name.Insert(name.Length - 4, "_predict");
                            //String name2 = "predict";
                            predict_image.Save(name2);
                            
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("讀取錯誤!");
            }
        }
Пример #6
0
 //SVMParams _parameters
 public SVMClassifier(string filename)
 {
     model = new SVM();
     model.Load(filename);
 }
Пример #7
0
        /*
        public void OnContactRecordGesture(object sender, FrameReceivedEventArgs e)
        {
            if (isTouching)
            {
                if (normalizedImage == null)
                {
                    e.TryGetRawImage(
                        ImageType.Normalized,
                        0, 0,
                        InteractiveSurface.DefaultInteractiveSurface.Width,
                        InteractiveSurface.DefaultInteractiveSurface.Height,
                        out normalizedImage,
                        out normalizedMetrics);
                }
                else //updates raw image data
                {
                    e.UpdateRawImage(
                        ImageType.Normalized,
                        normalizedImage,
                        0, 0,
                        InteractiveSurface.DefaultInteractiveSurface.Width,
                        InteractiveSurface.DefaultInteractiveSurface.Height);
                }

                capture.OnContactRecordHelper(normalizedImage, normalizedMetrics);
            }
        }
         */
        /// <summary>
        /// Controls what occurs when the user stops a gesture
        /// NOTE: This event fires more than it ought. So we have it ignore any data shorter than 600 millisecondss
        /// </summary>
        public void OffContactStopRecord(object sender, ContactEventArgs e)
        {
            capture.OffContactHelper();

            if (capture.totalMillisec > 600)
            {
                lock (obj)
                {
                    writeToCSV();
                }

                capture.calculateRatio(touchManager);

                advanceGesture();

                Gesture gnew = stListToGesture(touchManager);
                float result;

                using (SVM svm = new SVM())
                {
                    svm.Load(@"C:\Users\faculty\Desktop\svm-function3coord16.xml");

                    result = svm.Predict(getProcessedGesture(gnew));
                }

                if (result == 1)
                {
                    gestureOwner = "Evan";
                }
                else
                {
                    gestureOwner = "Stevie";
                }
            }
        }