Example #1
0
        /// <summary>
        /// 训练正样本
        /// </summary>
        /// <param name="bmp">正样本位图</param>
        public void TrainPositive(Bitmap bmp)
        {
            Bitmap samplebmp    = null;
            double neg_distance = 0;
            double pos_distance = 0;
            bool   hasinserted  = false; // 指明样本是否已插入队列

            samplebmp = ImgOper.ResizeImage(bmp, Parameter.DETECT_WINDOW_SIZE.Width, Parameter.DETECT_WINDOW_SIZE.Height);
            samplebmp = ImgOper.Grayscale(samplebmp);

            for (double angle = (-1) * Parameter.ANGLE_BORDER; angle < Parameter.ANGLE_BORDER; angle += Parameter.ANGLE_INTERVAL)
            {
                Bitmap bmpclone = ImgOper.RotateImage(samplebmp, angle);
                bmpclone = ImgOper.ResizeImage(bmpclone, Parameter.DETECT_WINDOW_SIZE.Width, Parameter.DETECT_WINDOW_SIZE.Height);

                for (double scale = (-1) * Parameter.SCALE_BORDER; scale < Parameter.SCALE_BORDER; scale += Parameter.SCALE_INTERVAL)
                {
                    // 往两个方向去,所以是减号
                    IntPoint lt       = new IntPoint((int)(bmpclone.Width * scale / 2), (int)(bmpclone.Height * scale / 2));
                    IntPoint rt       = new IntPoint(bmpclone.Width - 1 - (int)(bmpclone.Width * scale / 2), (int)(bmpclone.Height * scale / 2));
                    IntPoint rb       = new IntPoint(bmpclone.Width - 1 - (int)(bmpclone.Width * scale / 2), bmpclone.Height - 1 - (int)(bmpclone.Height * scale / 2));
                    IntPoint lb       = new IntPoint((int)(bmpclone.Width * scale / 2), bmpclone.Height - 1 - (int)(bmpclone.Height * scale / 2));
                    Bitmap   scalebmp = ImgOper.QuadrilateralTransform(bmpclone, lt, rt, rb, lb);

                    HogGram             hogGram   = HogGram.GetHogFromBitmap(scalebmp, Parameter.CELL_SIZE.Width, Parameter.CELL_SIZE.Height, Parameter.PART_NUMBER);
                    NormBlockVectorGram blockGram = new NormBlockVectorGram(hogGram, Parameter.BLOCK_SIZE.Width, Parameter.BLOCK_SIZE.Height);

                    Rectangle rect = new Rectangle(0, 0, hogGram.HogSize.Width, hogGram.HogSize.Height);
                    double[]  vect = blockGram.GetHogWindowVec(rect);

                    if (Dimension != 0 && vect.Length != Dimension)
                    {
                        throw new Exception("输入正样本的尺寸与其他样本尺寸不一致!");
                    }

                    ValuedBitmap vbmp = null;
                    if (NegCenter != null && PosCenter != null)
                    {
                        // 计算离正负中心的距离
                        for (int i = 0; i < vect.Length; i++)
                        {
                            neg_distance += Math.Abs(vect[i] - NegCenter[i]);
                            pos_distance += Math.Abs(vect[i] - PosCenter[i]);
                        }

                        // 与负样本中心重合时,说明是负样本,不能插入正样本队列
                        if (neg_distance == 0)
                        {
                            return;
                        }

                        // 检测到的正样本加入样本队列的第二道关,如果不够接近正样本中心,就无法加入队列
                        // 按照Hog检测的判定条件,正距离乘以Parameter.POS_DIST_COEF,使其避开边界
                        if (neg_distance < pos_distance * Parameter.POS_DIST_COEF)
                        {
                            return;
                        }

                        // 带归一化的系数,如果用pos_distance/neg_distance,值可能会溢出;
                        // 将pos_distance / (pos_distance + neg_distance)作为正样本的评价系数,值越小越接近正样本
                        vbmp = new ValuedBitmap(scalebmp, pos_distance / (pos_distance + neg_distance));
                    }
                    else
                    {
                        // 如果正或负样本库还没建立起来,则Val暂时赋值为1
                        vbmp = new ValuedBitmap(scalebmp, 1);
                    }

                    // 检测到的正样本加入样本队列的第三道关,与正样本评价系数的有序队列比较后,决定是否加入样本队列
                    hasinserted = InsertValuedBitmap(ref PosMapCollection, vbmp, Parameter.POS_LIMITED_NUMBER);
                    PosLength   = PosMapCollection.Count;

                    //// 人工观察正样本插入情况
                    //if (hasinserted && vbmp != null)
                    //{
                    //    vbmp.VBitmap.Save("Image\\pos_save\\" + poscnt + "_" + vbmp.Val + ".jpg");
                    //    poscnt++;
                    //}

                    // 如果样本已经插入队列,说明样本比较可信,重新计算样本中心
                    if (hasinserted)
                    {
                        if (PosCenter == null)
                        {
                            Dimension = vect.Length;
                            PosCenter = new double[Dimension];
                        }

                        for (int i = 0; i < Dimension; i++)
                        {
                            PosCenter[i] = (PosCenter[i] * PosLength + vect[i]) / (PosLength + 1);
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Hog检测, 被检测到图像自动缩放到BMPLIMITSIZE容忍范围内,并在检测完后将检测框自动放大之前缩小的倍率
        /// </summary>
        /// <param name="bmp">位图</param>
        public RectangleCollection HogDetect(Bitmap bmp)
        {
            RectangleCollection resultCollection = null;

            if (bmp == null)
            {
                return(null);
            }

            if (NegCenter == null && PosCenter == null)
            {
                return(null);
            }

            DateTime dt     = DateTime.Now;
            double   elapse = 0;

            // 针对原图的缩放倍率
            double se = 1;

            if (bmp.Width > Parameter.BMPLIMITSIZE.Width || bmp.Height > Parameter.BMPLIMITSIZE.Height)
            {
                se = bmp.Width / (double)Parameter.BMPLIMITSIZE.Width > bmp.Height / (double)Parameter.BMPLIMITSIZE.Height ?
                     bmp.Width / (double)Parameter.BMPLIMITSIZE.Width : bmp.Height / (double)Parameter.BMPLIMITSIZE.Height;
                bmp = ImgOper.ResizeImage(bmp, (int)(bmp.Width / se), (int)(bmp.Height / se));
            }
            bmp = ImgOper.Grayscale(bmp);

            //bmp = ImgOper.GaussianConvolution(bmp, GAUSSIAN_SIGMA, GAUSSIAN_SIZE);   // 高斯卷积,使得图像平滑

            // 所有层的检测结果
            ArrayList resultlayers = new ArrayList();
            // 初始缩放因子
            double scalecoef = 1.0;
            Bitmap scalebmp  = null;
            int    newwidth  = (int)(bmp.Width / scalecoef);
            int    newheight = (int)(bmp.Height / scalecoef);
            // 每层最小距离点的集合
            ArrayList idx_layermindistance = new ArrayList();
            int       cnt = 0;

            do
            {
                scalebmp = ImgOper.ResizeImage(bmp, newwidth, newheight);
                HogGram             hogGram   = HogGram.GetHogFromBitmap(scalebmp, Parameter.CELL_SIZE.Width, Parameter.CELL_SIZE.Height, Parameter.PART_NUMBER);
                NormBlockVectorGram blockGram = new NormBlockVectorGram(hogGram, Parameter.BLOCK_SIZE.Width, Parameter.BLOCK_SIZE.Height);

                DetectResultLayer detectlayer = new DetectResultLayer();
                // !!!!!!检测窗口的像素尺寸必须能被cell尺寸整除!!!!!!像素尺寸除以hog尺寸就是检测窗口的尺寸
                detectlayer.DetectResult = blockGram.DetectImgByHogWindow(
                    new Size(Parameter.DETECT_WINDOW_SIZE.Width / Parameter.CELL_SIZE.Width, Parameter.DETECT_WINDOW_SIZE.Height / Parameter.CELL_SIZE.Height),
                    NegCenter, PosCenter, Parameter.POS_DIST_COEF);
                if (detectlayer.DetectResult == null)
                {
                    return(null);
                }

                detectlayer.ScaleCoef = scalecoef;
                resultlayers.Add(detectlayer);        // 本层检测结果加入队列

                scalecoef *= Parameter.SCALE_COEF;    // 逐次缩小图像
                newwidth   = (int)(bmp.Width / scalecoef);
                newheight  = (int)(bmp.Height / scalecoef);
                cnt++;
            } while (newwidth > 2 * Parameter.DETECT_WINDOW_SIZE.Width && newheight > 2 * Parameter.DETECT_WINDOW_SIZE.Height);

            elapse = DateTime.Now.Subtract(dt).TotalSeconds;

            // 框出所有可能的物体
            WindowResult[] wr = null;
            Rectangle      rect;
            double         mindist       = -1;
            WindowResult   min_obj       = null;
            double         min_scalecoef = 1;

            resultCollection = new RectangleCollection();

            foreach (DetectResultLayer layer in resultlayers)
            {
                wr = layer.DetectResult;
                for (int i = 0; i < wr.Length; i++)
                {
                    if (wr[i].label == 1)
                    {
                        if (mindist == -1 || mindist > wr[i].PosDistance)
                        {
                            mindist       = wr[i].PosDistance;
                            min_obj       = wr[i];
                            min_scalecoef = layer.ScaleCoef;
                        }

                        rect = new Rectangle((int)(wr[i].ImageRegion.X * layer.ScaleCoef * se),
                                             (int)(wr[i].ImageRegion.Y * layer.ScaleCoef * se),
                                             (int)(wr[i].ImageRegion.Width * layer.ScaleCoef * se),
                                             (int)(wr[i].ImageRegion.Height * layer.ScaleCoef * se));
                        resultCollection.Add(rect);
                    }
                }
            }

            //rect = new Rectangle((int)(min_obj.ImageRegion.X * min_scalecoef * se),
            //    (int)(min_obj.ImageRegion.Y * min_scalecoef * se),
            //    (int)(min_obj.ImageRegion.Width * min_scalecoef * se),
            //    (int)(min_obj.ImageRegion.Height * min_scalecoef * se));
            //resultCollection.Add(rect);
            return(resultCollection);
        }
Example #3
0
        /// <summary>
        /// 计算最近正样本距离系数,按照距离而不是相关系数,这样效率高,系数越小,检测对象越接近目标
        /// </summary>
        /// <param name="rect">检测目标区域</param>
        /// <param name="bmp">位图</param>
        /// <returns>最近距离系数, double.MaxValue表示计算异常或没计算</returns>
        private double NearestNeighbour(Rectangle rect, Bitmap bmp)
        {
            Bitmap sample = null;
            Bitmap detect = null;

            Rectangle gramRect = Rectangle.Empty;

            HogGram sampleHGram = null;
            HogGram detectHGram = null;

            NormBlockVectorGram sampleBlock = null;
            NormBlockVectorGram detectBlock = null;

            double[]  detectvect = null;
            double[]  samplevect = null;
            ArrayList posvects   = null;
            ArrayList negvects   = null;

            double minposdist  = double.MaxValue;
            double minnegdist  = double.MaxValue;
            double dist        = 0;
            double nearestdist = double.MaxValue;


            if (PosLength == 0 || NegLength == 0)
            {
                return(nearestdist);
            }

            // 正样本载入
            posvects = new ArrayList();
            for (int i = 0; i < PosLength; i++)
            {
                sample = PosMapCollection[i].VBitmap;
                sample = ImgOper.ResizeImage(sample, Parameter.DETECT_WINDOW_SIZE.Width, Parameter.DETECT_WINDOW_SIZE.Height);
                sample = ImgOper.Grayscale(sample);

                sampleHGram = HogGram.GetHogFromBitmap(sample, Parameter.CELL_SIZE.Width, Parameter.CELL_SIZE.Height, Parameter.PART_NUMBER);
                sampleBlock = new NormBlockVectorGram(sampleHGram, Parameter.BLOCK_SIZE.Width, Parameter.BLOCK_SIZE.Height);

                gramRect   = new Rectangle(0, 0, sampleHGram.HogSize.Width, sampleHGram.HogSize.Height);
                samplevect = sampleBlock.GetHogWindowVec(gramRect);
                posvects.Add(samplevect);
            }

            // 负样本载入
            negvects = new ArrayList();
            for (int i = 0; i < NegLength; i++)
            {
                sample = NegMapCollection[i].VBitmap;
                sample = ImgOper.ResizeImage(sample, Parameter.DETECT_WINDOW_SIZE.Width, Parameter.DETECT_WINDOW_SIZE.Height);
                sample = ImgOper.Grayscale(sample);

                sampleHGram = HogGram.GetHogFromBitmap(sample, Parameter.CELL_SIZE.Width, Parameter.CELL_SIZE.Height, Parameter.PART_NUMBER);
                sampleBlock = new NormBlockVectorGram(sampleHGram, Parameter.BLOCK_SIZE.Width, Parameter.BLOCK_SIZE.Height);

                gramRect   = new Rectangle(0, 0, sampleHGram.HogSize.Width, sampleHGram.HogSize.Height);
                samplevect = sampleBlock.GetHogWindowVec(gramRect);
                negvects.Add(samplevect);
            }


            detect = ImgOper.CutImage(bmp, rect.X, rect.Y, rect.Width, rect.Height);
            detect = ImgOper.ResizeImage(detect, Parameter.DETECT_WINDOW_SIZE.Width, Parameter.DETECT_WINDOW_SIZE.Height);
            detect = ImgOper.Grayscale(detect);

            detectHGram = HogGram.GetHogFromBitmap(detect, Parameter.CELL_SIZE.Width, Parameter.CELL_SIZE.Height, Parameter.PART_NUMBER);
            detectBlock = new NormBlockVectorGram(detectHGram, Parameter.BLOCK_SIZE.Width, Parameter.BLOCK_SIZE.Height);

            gramRect   = new Rectangle(0, 0, detectHGram.HogSize.Width, detectHGram.HogSize.Height);
            detectvect = detectBlock.GetHogWindowVec(gramRect);

            foreach (double[] svect in posvects)
            {
                dist = ImgStatCompute.ComputeDistance(detectvect, svect);
                if (dist < minposdist)
                {
                    minposdist = dist;
                }
            }

            foreach (double[] svect in negvects)
            {
                dist = ImgStatCompute.ComputeDistance(detectvect, svect);

                if (dist < minnegdist)
                {
                    minnegdist = dist;
                }
            }

            if (minnegdist != 0 || minposdist != 0)
            {
                nearestdist = minposdist / (minposdist + minnegdist);
            }

            return(nearestdist);
        }
Example #4
0
        /// <summary>
        /// 训练负样本
        /// </summary>
        /// <param name="bmp">负样本位图</param>
        public void TrainNegative(Bitmap bmp)
        {
            Bitmap samplebmp    = null;
            double neg_distance = 0;
            double pos_distance = 0;
            bool   hasinserted  = false; // 指明样本是否已插入队列

            if (bmp.Width / Parameter.DETECT_WINDOW_SIZE.Width > bmp.Height / Parameter.DETECT_WINDOW_SIZE.Height)
            {
                samplebmp = ImgOper.ResizeImage(bmp,
                                                (int)(bmp.Width * Parameter.DETECT_WINDOW_SIZE.Height / bmp.Height), Parameter.DETECT_WINDOW_SIZE.Height);
            }
            else
            {
                samplebmp = ImgOper.ResizeImage(bmp, Parameter.DETECT_WINDOW_SIZE.Width,
                                                (int)(bmp.Height * Parameter.DETECT_WINDOW_SIZE.Width / bmp.Width));
            }
            samplebmp = ImgOper.CutImage(samplebmp, 0, 0, Parameter.DETECT_WINDOW_SIZE.Width, Parameter.DETECT_WINDOW_SIZE.Height);
            samplebmp = ImgOper.Grayscale(samplebmp);

            HogGram             hogGram   = HogGram.GetHogFromBitmap(samplebmp, Parameter.CELL_SIZE.Width, Parameter.CELL_SIZE.Height, Parameter.PART_NUMBER);
            NormBlockVectorGram blockGram = new NormBlockVectorGram(hogGram, Parameter.BLOCK_SIZE.Width, Parameter.BLOCK_SIZE.Height);

            Rectangle rect = new Rectangle(0, 0, hogGram.HogSize.Width, hogGram.HogSize.Height);

            double[] vect = blockGram.GetHogWindowVec(rect);

            if (Dimension != 0 && vect.Length != Dimension)
            {
                throw new Exception("输入负样本的尺寸与其他样本尺寸不一致!");
            }

            ValuedBitmap vbmp = null;

            if (PosCenter != null && NegCenter != null)
            {
                // 计算离正负中心的距离
                for (int i = 0; i < vect.Length; i++)
                {
                    neg_distance += Math.Abs(vect[i] - NegCenter[i]);
                    pos_distance += Math.Abs(vect[i] - PosCenter[i]);
                }

                // 与正样本中心重合时,说明是正样本,不能插入负样本队列
                if (pos_distance == 0)
                {
                    return;
                }

                // 负样本加入样本队列的第二道关,如果不够接近负样本中心,就无法加入队列
                // 按照Hog检测的判定条件,正距离乘以Parameter.POS_DIST_COEF,使其避开边界
                if (pos_distance * Parameter.POS_DIST_COEF < neg_distance)
                {
                    return;
                }

                // 带归一化的系数,如果用neg_distance / pos_distance,值可能会溢出;
                // 将neg_distance / (pos_distance + neg_distance)作为负样本的评价系数,值越小越接近负样本
                vbmp = new ValuedBitmap(samplebmp, neg_distance / (pos_distance + neg_distance));
            }
            else
            {
                // 如果正样本库还没建立起来,则Val暂时赋值为1
                vbmp = new ValuedBitmap(samplebmp, 1);
            }

            // 负样本加入样本队列的第三道关,与负样本评价系数的有序队列比较后,决定是否加入样本队列
            hasinserted = InsertValuedBitmap(ref NegMapCollection, vbmp, Parameter.NEG_LIMITED_NUMBER);
            NegLength   = NegMapCollection.Count;

            //// 人工观察负样本插入情况
            //if (hasinserted && vbmp != null)
            //{
            //    vbmp.VBitmap.Save("Image\\neg_save\\" + negcnt + "_" + vbmp.Val + ".jpg");
            //    negcnt++;
            //}

            // 如果样本已经插入队列,说明样本比较可信,重新计算样本中心
            if (hasinserted)
            {
                if (NegCenter == null)
                {
                    Dimension = vect.Length;
                    NegCenter = new double[Dimension];
                }

                for (int i = 0; i < Dimension; i++)
                {
                    NegCenter[i] = (NegCenter[i] * NegLength + vect[i]) / (NegLength + 1);
                }
            }
        }