Exemplo n.º 1
0
        private Rect GetFaceRoiFastMat(Mat integralMask, Rect roi, int size, out double faceRatio)
        {
            var maxI = -1;
            var maxJ = -1;
            var max  = 0.0;

            var mat3    = new MatOfInt(integralMask);
            var indexer = mat3.GetGenericIndexer <int>();

            for (var i = roi.Y; i < roi.Y + roi.Height - size; i += 4)
            {
                for (var j = roi.X; j < roi.X + roi.Width - size; j += 4)
                {
                    var maskI = i + 1;
                    var maskJ = j + 1;

                    var count = indexer[maskI + size - 1, maskJ + size - 1];
                    count -= indexer[maskI - 1, maskJ + size - 1];
                    count -= indexer[maskI + size - 1, maskJ - 1];
                    count += indexer[maskI - 1, maskJ - 1];

                    var ratio = count * 1.0 / (size * size);

                    if (ratio > max)
                    {
                        max  = ratio;
                        maxI = i;
                        maxJ = j;
                    }
                }
            }

            faceRatio = max;

            if (maxI == -1)
            {
                return(Rect.Empty);
            }

            return(new Rect(maxJ, maxI, size, size));
        }
Exemplo n.º 2
0
        private void ImgList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int pos;

            if (ImgList != null && ImgList.SelectedItem != null)
            {
                if (DetectList != null)
                {
                    DetectList.Clear();
                }

                // var OriImageMat = new Mat(ImgList.SelectedItem.ToString());
                var ThrImageMat = new Mat();
                pos = ImgList.SelectedIndex;
                //CurrentFN = ImgList.SelectedItem.ToString();
                CurrentFN = fileEntries[pos];
                Mat OriImageMat = new Mat(CurrentFN);
                Mat GrayImage   = OriImageMat.CvtColor(ColorConversionCodes.BGR2GRAY);

                //int slideVal;
                //int.TryParse(SliderValue, out slideVal);

                Cv2.Threshold(GrayImage, ThrImageMat, int.Parse(SliderValue), 255, ThresholdTypes.Binary);

                int[] mask = { 1, 1, 1,
                               1, 1, 1,
                               1, 1, 1 };

                Mat kernel = new Mat(3, 3, MatType.CV_32F, mask);

                Cv2.MorphologyEx(ThrImageMat, ThrImageMat, MorphTypes.Open, kernel);
                Cv2.MorphologyEx(ThrImageMat, ThrImageMat, MorphTypes.Close, kernel);
                Cv2.BitwiseNot(ThrImageMat, ThrImageMat);

                var label     = new MatOfInt();
                var stats     = new MatOfInt();
                var centroids = new MatOfDouble();

                var nLabels = Cv2.ConnectedComponentsWithStats(ThrImageMat, label, stats, centroids, PixelConnectivity.Connectivity8, MatType.CV_32S);


                var statsIndexer = stats.GetGenericIndexer <int>();
                for (int i = 0; i < nLabels; i++)
                {
                    //Threshold줄때 일정크기 이하의 객체는 노이즈로 간주하고 패스
                    if (statsIndexer[i, 4] < 90)
                    {
                        continue;
                    }

                    var rect = new OpenCvSharp.Rect
                    {
                        X      = statsIndexer[i, 0],
                        Y      = statsIndexer[i, 1],
                        Width  = statsIndexer[i, 2],
                        Height = statsIndexer[i, 3]
                    };

                    Cv2.Rectangle(OriImageMat, rect, new Scalar(33, 113, 243), 3);

                    obj temp = new obj();

                    //중심좌표 x,y (0 ~ 1)
                    temp.X = ((rect.X + rect.Width) / 2.0) / OriImageMat.Width;
                    temp.Y = ((rect.Y + rect.Height) / 2.0) / OriImageMat.Height;
                    temp.W = (double)rect.Width / (double)OriImageMat.Width;
                    temp.H = (double)rect.Height / (double)OriImageMat.Height;

                    DetectList.Add(temp);
                }



                OriginalImg.Source  = OriImageMat.ToWriteableBitmap();
                ThresholdImg.Source = ThrImageMat.ToWriteableBitmap();
            }
        }