コード例 #1
0
 private void ExportInOutdoorHistToCsv(StreamWriter file, List <FileInfo> imagesOnDisk, ColorHistogramInOutDoorFeature inoutdoorFeature)
 {
     foreach (var image in imagesOnDisk)
     {
         using (Image <Bgr, Byte> tempImage = new Image <Bgr, Byte>(image.FullName))
         {
             inoutdoorFeature.Compute(tempImage);
             var descriptor = string.Join(",", inoutdoorFeature.DescriptorArray);
             file.WriteLine(descriptor);
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Find the pedestrian in the image
        /// </summary>
        /// <param name="image">The image</param>
        /// <returns>The image with pedestrian highlighted.</returns>
        public void Detect(Image <Bgr, Byte> image)
        {
            ColorHistogramInOutDoorFeature inoutdoorFeature = new ColorHistogramInOutDoorFeature();

            inoutdoorFeature.Compute(image);
            float response = _detector.Predict(inoutdoorFeature.Descriptors);

            if (response > 0.0f)
            {
                _label = @"Outdoor";
            }
            else if (response < 0.0f)
            {
                _label = @"Indoor";
            }
            else
            {
                _label = @"";
            }
        }
コード例 #3
0
        private void trainIndoorOutdoorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog.RootFolder = System.Environment.SpecialFolder.MyComputer;

            DialogResult dialogResult = this.folderBrowserDialog.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string root = folderBrowserDialog.SelectedPath;
                ColorHistogramInOutDoorFeature inoutdoorFeature = new ColorHistogramInOutDoorFeature();
                DirectoryInfo trainPosDir = new DirectoryInfo(root + @"\Train\pos");
                DirectoryInfo trainNegDir = new DirectoryInfo(root + @"\Train\neg");
                DirectoryInfo testPosDir  = new DirectoryInfo(root + @"\Test\pos");
                DirectoryInfo testNegDir  = new DirectoryInfo(root + @"\Test\neg");

                var posImagesOnDisk = FileHelper.GetImages(trainPosDir);
                var negImagesOnDisk = FileHelper.GetImages(trainNegDir);

                int trainingCount = posImagesOnDisk.Count + negImagesOnDisk.Count;

                int offset    = 0;
                int index     = 0;
                int dimension = 3 * 2 * 64;

                Matrix <float> trainClasses = new Matrix <float>(trainingCount, 1);

                float[,] flatDescriptors = new float[trainingCount, dimension];
                foreach (var image in posImagesOnDisk)
                {
                    using (Image <Bgr, Byte> tempImage = new Image <Bgr, Byte>(image.FullName))
                    {
                        inoutdoorFeature.Compute(tempImage);
                        var descriptor = inoutdoorFeature.Descriptors;
                        Buffer.BlockCopy(descriptor.ManagedArray, 0, flatDescriptors, offset, sizeof(float) * descriptor.ManagedArray.Length);
                        offset += sizeof(float) * descriptor.ManagedArray.Length;

                        trainClasses[index, 0] = 1.0f;
                        ++index;
                    }
                }

                foreach (var image in negImagesOnDisk)
                {
                    using (Image <Bgr, Byte> tempImage = new Image <Bgr, Byte>(image.FullName))
                    {
                        inoutdoorFeature.Compute(tempImage);
                        var descriptor = inoutdoorFeature.Descriptors;
                        Buffer.BlockCopy(descriptor.ManagedArray, 0, flatDescriptors, offset, sizeof(float) * descriptor.ManagedArray.Length);
                        offset += sizeof(float) * descriptor.ManagedArray.Length;

                        trainClasses[index, 0] = -1.0f;
                        ++index;
                    }
                }

                Matrix <float> trainData = new Matrix <float>(flatDescriptors);
                using (SVM model = new 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(1000, 0.01);

                    bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 5);

                    model.Save(root + @"\inoutdoor.model");
                }
            }
        }