コード例 #1
0
        public static SampleCollection LoadPosSamples(string dir, bool isPositive, ColorType colorType)
        {
            if (false == Directory.Exists(dir))
            {
                throw new DirectoryNotFoundException(string.Format("目录\"{0}\"不存在", dir));
            }
            string[]         files = ListImageFiles(dir);
            SampleCollection coll  = new SampleCollection(files.Length * 4);

            foreach (string filename in files)
            {
                Image <Bgr, Byte> img = new Image <Bgr, Byte>(filename);
                ISample           sample;

                sample = new HaarSample(img, isPositive, colorType);
                coll.Add(sample);

                img._Flip(FLIP.HORIZONTAL);
                sample = new HaarSample(img, isPositive, colorType);
                coll.Add(sample);

                img._Flip(FLIP.VERTICAL);
                sample = new HaarSample(img, isPositive, colorType);
                coll.Add(sample);

                img._Flip(FLIP.HORIZONTAL);
                sample = new HaarSample(img, isPositive, colorType);
                coll.Add(sample);
            }
            return(coll);
        }
コード例 #2
0
        public static SampleCollection LoadNegSamples(string dir, ColorType colorType, Size windowSize)
        {
            if (false == Directory.Exists(dir))
            {
                throw new DirectoryNotFoundException(string.Format("目录\"{0}\"不存在", dir));
            }
            string[]         files = ListImageFiles(dir);
            SampleCollection coll  = new SampleCollection(0);

            coll._negIndex = new int[files.Length + 1];
            List <HaarSample[]> negList = new List <HaarSample[]>(files.Length);
            int i = 0;

            foreach (string filename in files)
            {
                HaarSample[] samples = HaarSample.LoadNegSample(filename, colorType, windowSize, coll._negIndex[i]);
                negList.Add(samples);
                //if (i + 1 < coll._negIndex.Length)
                coll._negIndex[i + 1] = coll._negIndex[i] + samples.Length;
                i++;
            }
            coll.Capacity = coll._negIndex[i];
            foreach (HaarSample[] array in  negList)
            {
                coll._samples.AddRange(array);
            }
            coll._negCount = coll._samples.Count;
            return(coll);
        }
コード例 #3
0
        public Point[] Detect(Image <Bgr, Byte> img, int classifierId)
        {
            DetectHaarSample sample = new DetectHaarSample(img);
            int  width  = sample.Size.Width;
            int  height = sample.Size.Height;
            Size window = _cascade.Size;
            int  yEnd   = height - window.Height;
            int  xEnd   = width - window.Width;

            List <Point> result = new List <Point>((int)Math.Sqrt((xEnd + 1) * (yEnd + 1)));

            for (int y = 0; y <= yEnd; y += _yStep)
            {
                for (int x = 0; x <= xEnd; x += _xStep)
                {
                    Point      offset    = new Point(x, y);
                    HaarSample subSample = new HaarSample(sample, offset, window);
                    if (_cascade.PredictUseOneClassifier(subSample, classifierId))
                    {
                        result.Add(offset);
                    }
                }
            }

            return(result.ToArray());
        }
コード例 #4
0
ファイル: HaarSample.cs プロジェクト: asuper0/HaarCascade
        /// <summary>
        /// 从一个负样本图像中加载所有样本,共用一个图像
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="colorType"></param>
        /// <param name="windowSize">检测窗口大小</param>
        /// <returns></returns>
        public static HaarSample[] LoadNegSample(string filename, ColorType colorType, Size windowSize, int startIndex)
        {
            Image <Bgr, Byte> img = new Image <Bgr, Byte>(filename);

            MyFloat[,] grayIntergralImage = null, saturationIntergralImage = null, graySquareIntergralImage = null;;
            if ((colorType & ColorType.Gray) != 0)
            {
                Image <Gray, Byte>   gray = img.Convert <Gray, Byte>();
                Image <Gray, double> grayIntergral, squareIntergral;

                //MyFloat[,] norm = NormalizeVariance(gray);
                gray.Integral(out grayIntergral, out squareIntergral);
                grayIntergralImage       = ConvertIntergral(grayIntergral);
                graySquareIntergralImage = ConvertIntergral(squareIntergral);

//                 MyFloat[,] norm = NormalizeVariance(gray);
//                 grayIntergralImage = CalcIntergarl(norm);
//                 graySquareIntergralImage= new MyFloat[0,0];
//                 gray.Integral(out grayIntergral, out squareIntergral);
//                 grayIntergralImage = ConvertIntergral(grayIntergral);
//                 graySquareIntergralImage = ConvertIntergral(squareIntergral);
            }
            if ((colorType & ColorType.Saturation) != 0)
            {
                Image <Hsv, Byte>  hsv        = img.Convert <Hsv, Byte>();
                Image <Gray, Byte> saturation = hsv[1];
                saturationIntergralImage = CalcIntergarl(saturation);
            }

            int height = img.Rows, width = img.Cols;
            int yEnd = height - windowSize.Height + 1, xEnd = width - windowSize.Width + 1;

            HaarSample[] samples = new HaarSample[yEnd * xEnd];
            int          i       = 0;

            for (int y = 0; y < yEnd; y++)
            {
                for (int x = 0; x < xEnd; x++)
                {
                    HaarSample s = new HaarSample();
                    s._grayIntergralImage       = grayIntergralImage;
                    s._saturationIntergralImage = saturationIntergralImage;
                    s._graySquareIntergralImage = graySquareIntergralImage;
                    s._isPositive = false;
                    s._xOffset    = x;
                    s._yOffset    = y;
                    s._id         = startIndex + i;
                    s.CalcMeanAndStd(windowSize);
//                     s._mean = 0;
//                     s._std = 1;
                    samples[i++] = s;
                }
            }
            return(samples);
        }