asmrobot
2019-11-21 589ed88a5924a7494e21b95b6bbff5e46ff49ddd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.ML;
using Emgu.CV.Structure;
 
namespace RichCreator.Editor.Utils
{
    public class HOGTrain
    {
 
        public HOGTrain(System.Drawing.Size winSize,
            System.Drawing.Size blockSize,
            System.Drawing.Size strideSize,
            System.Drawing.Size cellSize,
            Int32 channel)
        {
            this.WinSize = winSize;
            this.BlockSize = blockSize;
            this.StrideSize = strideSize;
            this.CellSize = cellSize;
            this.Channel = channel;
 
            Initizlize();
        }
 
 
        public System.Drawing.Size WinSize { get; set; }
 
        public System.Drawing.Size BlockSize { get; set; }
 
        public System.Drawing.Size StrideSize { get; set; }
 
        public System.Drawing.Size CellSize { get; set; }
 
        public Int32 Channel { get; set; }
 
        /// <summary>
        /// 训练数据
        /// </summary>
        private List<HogFeatureDataContainer> FeatureDatas = new List<HogFeatureDataContainer>();
 
        /// <summary>
        /// 维数
        /// </summary>
        public Int32 Dimensional { get; set; }
 
        private HOGDescriptor hog;
 
        private void Initizlize()
        {
            //计算Dimensional
            /// a = (winsize宽-block宽) / stride宽 +1                   23  
            /// b = (winsize高-block高) / stride高 +1                   14
            /// c = block宽 / cell宽                                    2
            /// d = block高 / block高                                   2
            /// 纬度 = a* b*c* d*channel数,本示例中:23*14*2*2*9=11592
            Int32 a = (this.WinSize.Width - this.BlockSize.Width) / this.StrideSize.Width + 1;
            Int32 b = (this.WinSize.Height - this.BlockSize.Height) / this.StrideSize.Height + 1;
            Int32 c = this.BlockSize.Width / this.CellSize.Width;
            Int32 d = this.BlockSize.Height / this.CellSize.Height;
            this.Dimensional = a * b * c * d * Channel;
 
            //第一个参数:winsize:窗口大小,我取样本大小;
            //第二个参数:block,我的是12 * 12;当然,也可以设置为18 * 18或者6 * 6,切记:BLOCK大小不能是技术值,至于为什么,看一下cell和block的关系即可。同时:设计的过程中,BLOCK要可被winsize整除。网上的资料都是16 * 16,也没有说清楚为什么,其实这个看懂了HOG后可以自行设置
            //第三个参数:stride,我的设置是6 * 6,当然也可以设置为12 * 12或者4 * 4,不同的设置,将得到不同的纬度数量;
            //第四个参数:cell,4个cell构成1个block,这也就是block一定要是偶数的原因。
            //第五个参数:需要得到几个channel的值,默认是9个,具体原因请参见HOG原论文
            hog = new HOGDescriptor(this.WinSize, this.BlockSize, this.StrideSize, this.CellSize, this.Channel);
        }
 
 
        /// <summary>
        /// 训练
        /// </summary>
        /// <param name="files">样本图片列表</param>
        /// <param name="featureClasses">样本类型,1正向样本,-1负向样本</param>
        public void Train(string[] files, Int32 featureClasses)
        {
            Mat tempMat = null;
            Image<Gray, byte> im = null;
            for (int f = 0; f < files.Length; f++)
            {
                tempMat = CvInvoke.Imread(files[f]);
                im = tempMat.ToImage<Gray, byte>();
 
                float[] fArr = hog.Compute(im);
                this.FeatureDatas.Add(new HogFeatureDataContainer(fArr, featureClasses));
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="saveToXmlPath"></param>
        public void Save(string saveToXmlPath)
        {
            Matrix<float> hogFeatureData = new Matrix<float>(this.FeatureDatas.Count, this.Dimensional);//样本数,HOG纬度
            Matrix<int> featureClasses = new Matrix<int>(this.FeatureDatas.Count, 1);//样本数
 
            HogFeatureDataContainer container;
            for (int i = 0; i < this.FeatureDatas.Count; i++)
            {
                container = this.FeatureDatas[i];
                for (int t = 0; t < container.FeatureDatas.Length; t++)
                {
                    hogFeatureData[i, t] = container.FeatureDatas[t];
                }
                featureClasses.Data[i, 0] = container.FeatureClass;
            }
 
            //训练并保存训练结果
            SVM svm = new SVM();
            svm.Type = SVM.SvmType.CSvc;
            svm.SetKernel(SVM.SvmKernelType.Linear);//线性
            svm.C = 1;
            svm.TermCriteria = new MCvTermCriteria(1000, 0.001);//1000次或者收敛达到0.001就跳出            
            svm.Train(hogFeatureData, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, featureClasses);
            svm.Save(saveToXmlPath);
        }
 
 
        /// <summary>
        /// 训练数据包装器
        /// </summary>
        private struct HogFeatureDataContainer
        {
            public HogFeatureDataContainer(float[] featureDatas, Int32 featureClass)
            {
                this.FeatureDatas = featureDatas;
                this.FeatureClass = featureClass;
            }
 
            public float[] FeatureDatas;
 
            public Int32 FeatureClass;
        }
    }
}