Example #1
0
        /// <summary>
        /// 用训练好的模型测试已标记样本的类别
        /// </summary>
        /// <param name="sample">已标记样本图像</param>
        /// <param name="template">基础分类器模板</param>
        /// <param name="fern">Fern</param>
        /// <returns>返回测试结果,类别</returns>
        public static int DetectSample(Bitmap sample, BaseClassifierTemplate template, Fern fern)
        {
            Bitmap bmp = null;

            if (sample.Width != template.BmpWidth || sample.Height != template.BmpHeight)
            {
                bmp = ImgOper.ResizeImage(sample, template.BmpWidth, template.BmpHeight);
            }
            else
            {
                bmp = sample;
            }

            if (bmp != null && (bmp.PixelFormat == PixelFormat.Format24bppRgb || bmp.PixelFormat ==
                                PixelFormat.Format32bppRgb || bmp.PixelFormat == PixelFormat.Format32bppArgb))
            {
                bmp          = ImgOper.Grayscale(bmp);
                int[,] igram = ImgOper.Integrogram(bmp, 1);
                UInt32[] featurecode = BaseClassifierTemplate.GetFeatureCodeGroup(igram, bmp.Width, bmp.Height, template, 0, 0);
                double   prob        = 0;
                for (int i = 0; i < template.GroupNum; i++)
                {
                    prob += fern.Probability[featurecode[i]];
                }
                prob = prob / template.GroupNum;
                if (prob > 0.5)
                {
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }
            return(0);
        }
Example #2
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);
        }