Пример #1
0
        /// <summary>
        /// 用模板去检测图像中的物体
        /// </summary>
        /// <param name="bmpSource">源图像,需24位或32位真彩位图</param>
        /// <param name="template">基础分类器模板</param>
        /// <param name="fern">Fern</param>
        /// <returns></returns>
        public static RectangleCollection DetectObject(Bitmap bmpSource, BaseClassifierTemplate template, Fern fern)
        {
            RectangleCollection rc = new RectangleCollection();
            Bitmap bmp             = bmpSource;

            if (bmpSource.Width < template.BmpWidth || bmpSource.Height < template.BmpHeight)
            {
                return(null);
            }

            if (bmp != null && (bmp.PixelFormat == PixelFormat.Format24bppRgb || bmp.PixelFormat ==
                                PixelFormat.Format32bppRgb || bmp.PixelFormat == PixelFormat.Format32bppArgb))
            {
                UInt32[] featurecode = null;
                bmp          = ImgOper.Grayscale(bmpSource);
                int[,] igram = ImgOper.Integrogram(bmp, 1);

                while (template.BmpWidth < bmp.Width && template.BmpHeight < bmp.Height)
                {
                    for (int y = 0; y < bmp.Height - template.BmpHeight + 1; y += (template.BmpHeight / 10))
                    {
                        for (int x = 0; x < bmp.Width - template.BmpWidth + 1; x += (template.BmpWidth / 10))
                        {
                            //int posnum = 0;
                            //int negnum = 0;
                            featurecode = BaseClassifierTemplate.GetFeatureCodeGroup(igram, bmp.Width, bmp.Height, template, x, y);
                            double prob = 0;
                            for (int i = 0; i < template.GroupNum; i++)
                            {
                                prob += fern.Probability[featurecode[i]];
                                //prob = fern.Probability[featurecode[i]];
                                //if (prob > 0.5)
                                //{
                                //    posnum++;
                                //}
                                //else
                                //{
                                //    negnum++;
                                //}
                            }
                            prob = prob / template.GroupNum;
                            if (prob > 0.5)
                            //if (posnum > negnum)
                            {
                                Rectangle rect = new Rectangle(x, y, template.BmpWidth, template.BmpHeight);
                                rc.Add(rect);
                            }
                        }
                    }
                    template.ResizeTemplate(1.2);
                }
            }
            return(rc);
        }
Пример #2
0
        /// <summary>
        /// 负样本检测专家,专门检测被误判为正的负样本和增加负样本集合
        /// </summary>
        /// <param name="detectCollection">检测模块产生的区域集合</param>
        /// <param name="trackerRect">跟踪模块产生的区域</param>
        /// <param name="bmp">被检测的位图</param>
        /// <returns>返回最可信对象区域</returns>
        public Rectangle NegativeExpert(RectangleCollection detectCollection, RectangleF trackerRect, Bitmap bmp)
        {
            // 复制一个矩形集合是为了让NExpert独立与PExpert
            RectangleCollection newRectCollection = new RectangleCollection();

            if (detectCollection != null)
            {
                foreach (Rectangle detectrect in detectCollection)
                {
                    newRectCollection.Add(detectrect);
                }
            }
            if (trackerRect != Rectangle.Empty)
            {
                // 将跟踪到的目标也加入待评估的对象集合
                newRectCollection.Add(new Rectangle((int)trackerRect.X, (int)trackerRect.Y, (int)trackerRect.Width, (int)trackerRect.Height));
            }

            DateTime dt = DateTime.Now;
            // 最可信的对象
            Rectangle confidentRect = MinDistanceObject(newRectCollection, bmp);
            double    elapse        = DateTime.Now.Subtract(dt).TotalMilliseconds;

            if (confidentRect != Rectangle.Empty)
            {
                newRectCollection.Remove(confidentRect);
            }

            dt = DateTime.Now;
            foreach (Rectangle rect in newRectCollection)
            {
                // 判断目标是否确实为背景,正距离归一化系数大于0.5
                // 目标加入负样本队列的第一道关,目标必须看起来像负样本(正距离归一化系数大于0.5)
                if (MinDistance(rect, bmp) > Parameter.MEDIAN_COEF)
                {
                    double areainsect = AreaProportion(confidentRect, rect);
                    // 与最可信对象交集面积小于AREA_INTERSECT_PROPORTION的为负样本,加入负样本列表
                    if (areainsect < Parameter.AREA_INTERSECT_PROPORTION)
                    {
                        Bitmap patch = ImgOper.CutImage(bmp, rect.X, rect.Y, rect.Width, rect.Height);
                        TrainNegative(patch);
                    }
                }
            }
            elapse = DateTime.Now.Subtract(dt).TotalMilliseconds;

            return(confidentRect);
        }
Пример #3
0
        /// <summary>
        /// 正样本检测专家,专门检测被误判为负的正样本
        /// </summary>
        /// <param name="detectCollection">检测模块产生的区域集合</param>
        /// <param name="trackerRect">跟踪模块产生的区域</param>
        /// <param name="bmp">被检测位图</param>
        public void PositiveExpert(RectangleCollection detectCollection, RectangleF trackerRect, Bitmap bmp)
        {
            double areaproportion = 0;
            bool   nointersect    = true;    // 指明跟踪模块和检测模块得到的区域是否无交集

            if (trackerRect == Rectangle.Empty)
            {
                return;
            }

            foreach (Rectangle rect in detectCollection)
            {
                areaproportion = AreaProportion(trackerRect, rect);
                if (areaproportion > Parameter.AREA_INTERSECT_PROPORTION)
                {
                    nointersect = false;
                    break;
                }
            }

            // 没有交集,说明存在被误判为负的正样例
            if (nointersect)
            {
                // 判断跟踪到的目标是否确实为要识别的物体,正距离归一化系数小于0.5
                // 目标加入正样本队列的第一道关,目标必须看起来像正样本(正距离归一化系数小于0.5)
                if (MinDistance(trackerRect, bmp) < Parameter.MEDIAN_COEF)
                {
                    for (double lrshift = (-1) * Parameter.SHIFT_BORDER; lrshift < Parameter.SHIFT_BORDER + Parameter.SHIFT_INTERVAL; lrshift += Parameter.SHIFT_INTERVAL)
                    {
                        for (double tbshift = (-1) * Parameter.SHIFT_BORDER; tbshift < Parameter.SHIFT_BORDER + Parameter.SHIFT_INTERVAL; tbshift += Parameter.SHIFT_INTERVAL)
                        {
                            if (trackerRect.X + lrshift >= 0 && trackerRect.X + trackerRect.Width - 1 + lrshift < bmp.Width - 1 &&
                                trackerRect.Y + tbshift >= 0 && trackerRect.Y + trackerRect.Height - 1 + tbshift < bmp.Height - 1)
                            {
                                Bitmap patch = ImgOper.CutImage(bmp, (int)(trackerRect.X + lrshift), (int)(trackerRect.Y + tbshift),
                                                                (int)trackerRect.Width, (int)trackerRect.Height);
                                TrainPositive(patch);
                            }
                        }
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 从目标集合中找出最大相关系数的目标,即最可信对象
        /// </summary>
        /// <param name="rectCollection">目标集合</param>
        /// <param name="bmp">位图</param>
        /// <returns>最可信对象</returns>
        public Rectangle MostAssociateObject(RectangleCollection rectCollection, Bitmap bmp)
        {
            double    coef          = 0;
            double    maxcoef       = 0;
            Rectangle confidentRect = Rectangle.Empty;

            if (rectCollection == null || rectCollection.Count == 0)
            {
                return(confidentRect);
            }

            foreach (Rectangle rect in rectCollection)
            {
                coef = MostAssociate(rect, bmp);
                if (maxcoef < coef)
                {
                    maxcoef       = coef;
                    confidentRect = rect;
                }
            }

            return(confidentRect);
        }
Пример #5
0
        /// <summary>
        /// 在疑似目标集合中找到最可信的目标
        /// </summary>
        /// <param name="rectCollection">目标集合</param>
        /// <param name="bmp">位图</param>
        /// <returns>最可信目标</returns>
        private Rectangle MostConfidentObject(RectangleCollection rectCollection, Bitmap bmp)
        {
            double    distcoef      = double.MaxValue;
            double    mindistcoef   = double.MaxValue;
            Rectangle confidentRect = Rectangle.Empty;

            if (rectCollection == null || rectCollection.Count == 0)
            {
                return(confidentRect);
            }

            foreach (Rectangle rect in rectCollection)
            {
                distcoef = NearestNeighbour(rect, bmp);
                if (distcoef < mindistcoef)
                {
                    mindistcoef   = distcoef;
                    confidentRect = rect;
                }
            }

            return(confidentRect);
        }
Пример #6
0
        private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
        {
            Bitmap nowImg = AForge.Imaging.Image.Clone(image);

            nowImg = ImgOper.Grayscale(nowImg);

            // 做高斯模糊,取样和检测都会影响到
            nowImg = ImgOper.GaussianConvolution(nowImg, Parameter.GAUSSIAN_SIGMA, Parameter.GAUSSIAN_SIZE);

            // 将图像传递到取样窗口
            pri_bmp = AForge.Imaging.Image.Clone(nowImg);

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

            pri_obj_regions = pri_tld.HogDetect(nowImg);
            elapse          = DateTime.Now.Subtract(dt).TotalMilliseconds;

            float[] vect = new float[3];   // 描述区域位移和缩放
            //nowImg.Save("Image\\VideoSave\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg");
            ArrayList points = null;

            if (pri_optical.IPoints == null || pri_tracker_rect == Rectangle.Empty)
            {
                pri_optical.I = pri_optical.TransformBmpToLayerImg(nowImg);

                // 尚未确定跟踪对象,不需要PositiveExpert
                pri_tracker_rect = pri_tld.NegativeExpert(pri_obj_regions, pri_tracker_rect, nowImg);

                pri_optical.IPoints = new ArrayList();
                points = pri_optical.ChooseRectRandomPoints(pri_tracker_rect, Parameter.INITIAL_POINTS_NUMBER);
                foreach (PointF pt in points)
                {
                    pri_optical.IPoints.Add(new PointF(pt.X, pt.Y));
                }
            }
            else
            {
                if (pri_optical.J != null)
                {
                    pri_optical.I       = pri_optical.J;
                    pri_optical.IPoints = pri_optical.JPoints;
                }
                pri_optical.J = pri_optical.TransformBmpToLayerImg(nowImg);

                //pri_optical.JPoints = new ArrayList();
                // 跟踪时wx和wy设为3,或者更大
                dt   = DateTime.Now;
                vect = pri_optical.ComputerDisplacement(pri_optical.I, pri_optical.J, pri_optical.IPoints,
                                                        1, 3, 3, 20, 0.2f, ref pri_optical.IPoints, ref pri_optical.JPoints);
                elapse = DateTime.Now.Subtract(dt).TotalMilliseconds;

                if (vect != null)
                {
                    pri_tracker_rect.X      = pri_tracker_rect.X + vect[0];
                    pri_tracker_rect.Y      = pri_tracker_rect.Y + vect[1];
                    pri_tracker_rect.Width  = pri_tracker_rect.Width * vect[2];
                    pri_tracker_rect.Height = pri_tracker_rect.Height * vect[2];

                    if (pri_tracker_rect.X < 0)
                    {
                        pri_tracker_rect.Width = pri_tracker_rect.Width + pri_tracker_rect.X;
                        pri_tracker_rect.X     = 0;
                    }
                    else if (pri_tracker_rect.X + pri_tracker_rect.Width - 1 > nowImg.Width - 1)
                    {
                        pri_tracker_rect.Width = nowImg.Width - pri_tracker_rect.X;
                    }

                    if (pri_tracker_rect.Y < 0)
                    {
                        pri_tracker_rect.Height = pri_tracker_rect.Height + pri_tracker_rect.Y;
                        pri_tracker_rect.Y      = 0;
                    }
                    else if (pri_tracker_rect.Y + pri_tracker_rect.Height - 1 > nowImg.Height - 1)
                    {
                        pri_tracker_rect.Height = nowImg.Height - pri_tracker_rect.Y;
                    }
                }
                else
                {
                    pri_tracker_rect = Rectangle.Empty;
                }

                // 在跟踪框被NExpert产生的最可信对象替代前保存原始状态
                if (pri_tracker_rect != Rectangle.Empty)
                {
                    pri_obj_rect = new RectangleF(pri_tracker_rect.X, pri_tracker_rect.Y, pri_tracker_rect.Width, pri_tracker_rect.Height);
                }

                dt = DateTime.Now;
                // 有跟踪对象时,PositvieExpert与NegativeExpert都开始工作
                pri_tld.PositiveExpert(pri_obj_regions, pri_tracker_rect, nowImg);
                elapse = DateTime.Now.Subtract(dt).TotalMilliseconds;

                dt = DateTime.Now;
                pri_tracker_rect = pri_tld.NegativeExpert(pri_obj_regions, pri_tracker_rect, nowImg);
                elapse           = DateTime.Now.Subtract(dt).TotalMilliseconds;

                pri_optical.JPoints = new ArrayList();
                points = pri_optical.ChooseRectRandomPoints(pri_tracker_rect, Parameter.INITIAL_POINTS_NUMBER);
                // 如果pri_tracker_rect为空,则pri_optical.JPoints为空,那么下次跟踪的pri_optical.Ipoints也为空,无法继续跟踪
                foreach (PointF pt in points)
                {
                    pri_optical.JPoints.Add(new PointF(pt.X, pt.Y));
                }
            }

            Graphics g   = Graphics.FromImage(image);
            Pen      pen = new Pen(Color.Red);

            if (pri_tracker_rect != Rectangle.Empty)
            {
                g.DrawRectangle(pen, pri_tracker_rect.X, pri_tracker_rect.Y, pri_tracker_rect.Width, pri_tracker_rect.Height);

                FontFamily f       = new FontFamily("宋体");
                Font       font    = new System.Drawing.Font(f, 12);
                SolidBrush myBrush = new SolidBrush(Color.Blue);

                StringBuilder str = new StringBuilder();
                if (vect != null)
                {
                    str.AppendFormat("跟踪框相对位移:({0}, {1}),缩放:{2} \r\n" +
                                     "跟踪产生的Rectangle:({3}, {4}, {5}, {6}) \r\n" +
                                     "最可信的Rectangle:({7}, {8}, {9}, {10})", vect[0], vect[1], vect[2],
                                     pri_obj_rect.X, pri_obj_rect.Y, pri_obj_rect.Width, pri_obj_rect.Height,
                                     pri_tracker_rect.X, pri_tracker_rect.Y, pri_tracker_rect.Width, pri_tracker_rect.Height);
                }
                else
                {
                    str.AppendFormat("最可信的Rectangle:({0}, {1}, {2}, {3})", pri_tracker_rect.X, pri_tracker_rect.Y, pri_tracker_rect.Width, pri_tracker_rect.Height);
                }

                g.DrawString(str.ToString(), font, myBrush, 0, 0);
            }

            //if (pri_obj_regions != null && pri_obj_regions.Count > 0)
            //{
            //    foreach (Rectangle rect in pri_obj_regions)
            //    {
            //        g.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
            //    }
            //}
        }
Пример #7
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);
        }