Ejemplo n.º 1
0
        /// <summary>
        /// 计算正样本最大相关系数,数值越大,被检测对象越接近目标
        /// </summary>
        /// <param name="rect">检测目标区域</param>
        /// <param name="bmp">位图</param>
        /// <returns>正样本最大相关系数</returns>
        public double MostAssociate(Rectangle rect, Bitmap bmp)
        {
            double maxcoef    = 0; // 返回值
            double coef       = 0;
            double maxposcoef = 0;
            double maxnegcoef = 0;

            if (rect == Rectangle.Empty)
            {
                return(maxcoef);
            }
            Bitmap patch = ImgOper.CutImage(bmp, rect.X, rect.Y, rect.Width, rect.Height);

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

            foreach (ValuedBitmap posbmp in PosMapCollection)
            {
                coef = ImgStatCompute.ComputeAssociationCoef(patch, posbmp.VBitmap);
                if (maxposcoef < coef)
                {
                    maxposcoef = coef;
                }
            }

            foreach (ValuedBitmap negbmp in NegMapCollection)
            {
                coef = ImgStatCompute.ComputeAssociationCoef(patch, negbmp.VBitmap);
                if (maxnegcoef < coef)
                {
                    maxnegcoef = coef;
                }
            }

            if (maxnegcoef != 0 || maxposcoef != 0)
            {
                maxcoef = maxposcoef / (maxposcoef + maxnegcoef);
            }

            return(maxcoef);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 计算最近正样本距离系数,按照距离而不是相关系数,这样效率高,系数越小,检测对象越接近目标
        /// </summary>
        /// <param name="rect">检测目标区域</param>
        /// <param name="bmp">整幅位图</param>
        /// <returns>最近距离系数, double.MaxValue表示计算异常或没计算</returns>
        public double MinDistance(RectangleF rect, Bitmap bmp)
        {
            double mindistance = double.MaxValue;   // 返回值
            double dist        = double.MaxValue;
            double minposdist  = double.MaxValue;
            double minnegdist  = double.MaxValue;

            if (rect == Rectangle.Empty)
            {
                return(mindistance);
            }

            DateTime dt     = DateTime.Now;
            double   elapse = 0;
            Bitmap   patch  = ImgOper.CutImage(bmp, (int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);

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

            int[,] patchgram = ImgOper.Integrogram(patch, 1);

            byte[] patchdata = ImgOper.GetGraybmpData(patch);
            double patchmean = (double)patchgram[patch.Height - 1, patch.Width - 1] / (double)(patch.Width * patch.Height);

            double[] patchdatad = new double[patchdata.Length];
            for (int i = 0; i < patchdata.Length; i++)
            {
                patchdatad[i] = patchdata[i] - patchmean;
            }

            foreach (ValuedBitmap posbmp in PosMapCollection)
            {
                int[,] posgram = ImgOper.Integrogram(posbmp.VBitmap, 1);
                byte[]   posdata  = ImgOper.GetGraybmpData(posbmp.VBitmap);
                double   posmean  = (double)posgram[posbmp.VBitmap.Height - 1, posbmp.VBitmap.Width - 1] / (double)(posbmp.VBitmap.Width * posbmp.VBitmap.Height);
                double[] posdatad = new double[posdata.Length];
                for (int i = 0; i < posdata.Length; i++)
                {
                    posdatad[i] = posdata[i] - posmean;
                }
                dist = ImgStatCompute.ComputeDistance(patchdatad, posdatad);
                if (dist < minposdist)
                {
                    minposdist = dist;
                }
            }

            foreach (ValuedBitmap negbmp in NegMapCollection)
            {
                int[,] neggram = ImgOper.Integrogram(negbmp.VBitmap, 1);
                byte[]   negdata  = ImgOper.GetGraybmpData(negbmp.VBitmap);
                double   negmean  = (double)neggram[negbmp.VBitmap.Height - 1, negbmp.VBitmap.Width - 1] / (double)(negbmp.VBitmap.Width * negbmp.VBitmap.Height);
                double[] negdatad = new double[negdata.Length];
                for (int i = 0; i < negdata.Length; i++)
                {
                    negdatad[i] = negdata[i] - negmean;
                }
                dist = ImgStatCompute.ComputeDistance(patchdatad, negdatad);
                if (dist < minnegdist)
                {
                    minnegdist = dist;
                }
            }

            if (minnegdist != 0 || minposdist != 0)
            {
                // 带归一化的系数,如果用minposdist/minnegdist,值可能会溢出
                mindistance = minposdist / (minposdist + minnegdist);
            }

            elapse = DateTime.Now.Subtract(dt).TotalMilliseconds;
            return(mindistance);
        }
Ejemplo n.º 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);
        }