コード例 #1
0
        public void Process(Mat rgbaImage)
        {
            Log.Info(TAG, "Process rgbaImages");

            Imgproc.PyrDown(rgbaImage, mPyrDownMat);
            Imgproc.PyrDown(mPyrDownMat, mPyrDownMat);

            Imgproc.CvtColor(mPyrDownMat, mHsvMat, Imgproc.ColorRgb2hsvFull);

            Core.InRange(mHsvMat, mLowerBound, mUpperBound, mMask);
            Imgproc.Dilate(mMask, mDilatedMask, new Mat());

            IList <MatOfPoint> contours = new JavaList <MatOfPoint>();

            Imgproc.FindContours(mDilatedMask, contours, mHierarchy, Imgproc.RetrExternal, Imgproc.ChainApproxSimple);

            // Find max contour area
            double maxArea = 0;

            foreach (var each in contours)
            {
                MatOfPoint wrapper = each;
                double     area    = Imgproc.ContourArea(wrapper);
                if (area > maxArea)
                {
                    maxArea = area;
                }
                Log.Info(TAG, "Process rgbaImages\t-- Imgproc.ContourArea(wrapper)");
            }

            // Filter contours by area and resize to fit the original image size
            mContours.Clear();
            foreach (var each in contours)
            {
                MatOfPoint contour = each;
                if (Imgproc.ContourArea(contour) > mMinContourArea * maxArea)
                {
                    Core.Multiply(contour, new Scalar(4, 4), contour);
                    mContours.Add(contour);
                    Log.Info(TAG, "Process rgbaImages\t-- mContours.Add(contour)");
                }
            }
        }
コード例 #2
0
        private bool Detect(Mat bin)
        {
            // Reset
            mRects.Clear();

            // Find contours
            Mat hierarchy = new Mat();
            IList <MatOfPoint> contoursList = new JavaList <MatOfPoint>();

            Imgproc.FindContours(bin.Clone(), contoursList, hierarchy, Imgproc.RetrTree, Imgproc.ChainApproxSimple);

            // Filter contours
            bool detected = false;

            for (int iContour = 0; iContour < contoursList.Count(); iContour++)
            {
                // Areas
                Core.Rect rect        = Imgproc.BoundingRect(contoursList[iContour]);
                double    ellipseArea = PI * (rect.Width / 2) * (rect.Height / 2);
                double    area        = Imgproc.ContourArea(contoursList[iContour]);

                // Ratios
                double boundWidthPerHeight = (double)rect.Width / rect.Height;
                double areaPerEllipse      = (double)(area) / ellipseArea;
                double rectPerFrame        = (double)(rect.Area()) / (bin.Size().Width *bin.Size().Height);

                // Check constraints
                if (rectPerFrame > MIN_SIGN_SIZE_PER_FRAME)
                {
                    if (1 - LIMIT_DIF_SIGN_SIZE < boundWidthPerHeight && boundWidthPerHeight < 1 + LIMIT_DIF_SIGN_SIZE)
                    {
                        if (1 - LIMIT_DIF_SIGN_AREA < areaPerEllipse && areaPerEllipse < 1 + LIMIT_DIF_SIGN_AREA)
                        {
                            mRects.Add(rect);
                            detected = true;
                        }
                    }
                }
            }

            return(detected);
        }
コード例 #3
0
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            Log.Info(Tag, "Menu Item selected " + item);

            if (item == _itemPickPhoto)
            {
                var imageIntent = new Intent();
                imageIntent.SetType("image/*");
                imageIntent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), 0);
            }
            else if (item == _itemGray)
            {
                // 灰度图
                _gray = new Mat(_raw.Width(), _raw.Height(), CvType.Cv8uc1);
                Imgproc.CvtColor(_raw, _gray, Imgproc.ColorRgb2gray);
                ShowImage(_gray);
            }
            else if (item == _itemThreshold)
            {
                // 二值化
                _threshold = new Mat(_image.Width, _image.Height, CvType.Cv8uc1);
                Imgproc.Threshold(_gray, _threshold, 168, 255, Imgproc.ThreshBinary);
                ShowImage(_threshold);
            }
            else if (item == _itemFindContours)
            {
                // 查找最大连通区域
                IList <MatOfPoint> contours = new JavaList <MatOfPoint>();
                Mat hierarchy = new Mat();
                var target    = _threshold.Clone();
                Imgproc.FindContours(target, contours, hierarchy, Imgproc.RetrExternal, Imgproc.ChainApproxNone);

                MatOfPoint max = new MatOfPoint();
                double     contour_area_max = 0;
                if (contours.Any())
                {
                    foreach (var contour in contours)
                    {
                        var contour_area_temp = Math.Abs(Imgproc.ContourArea(contour));
                        if (contour_area_temp > contour_area_max)
                        {
                            contour_area_max = contour_area_temp;
                            max = contour;
                        }
                    }
                }

                var last = new JavaList <MatOfPoint>();
                last.Add(max);

                Imgproc.DrawContours(_raw, last, -1, new Scalar(255, 0, 0));

                ShowImage(_raw);
            }
            else if (item == _itemCreateTrimap)
            {
                // 生成三元图  暂时先用生成的图替代
                var imageIntent = new Intent();
                imageIntent.SetType("image/*");
                imageIntent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), 1);
            }
            else if (item == _itemSharedMatting)
            {
                // 扣图
                var sharedMatting = new SharedMatting();
                sharedMatting.SetImage(_raw);
                sharedMatting.SetTrimap(_trimap);
                sharedMatting.SolveAlpha();
            }

            return(base.OnOptionsItemSelected(item));
        }