Example #1
0
        public async void Contours(SoftwareBitmap input, SoftwareBitmap output, Algorithm algorithm)
        {
            if (algorithm.AlgorithmName == "Contours")
            {
                using Mat mInput  = SoftwareBitmap2Mat(input);
                using Mat mOutput = new Mat(mInput.Rows, mInput.Cols, MatType.CV_8UC4);
                mInput.CopyTo(mOutput);
                using Mat gray  = mInput.CvtColor(ColorConversionCodes.BGRA2GRAY);
                using Mat edges = gray.Canny((double)algorithm.AlgorithmProperties[6].CurrentValue, (double)algorithm.AlgorithmProperties[7].CurrentValue);

                Cv2.FindContours(
                    image: edges,
                    contours: out OpenCvSharp.Point[][] contours,
                    hierarchy: out HierarchyIndex[] outputArray,
                    mode: (RetrievalModes)algorithm.AlgorithmProperties[0].CurrentValue,
                    method: (ContourApproximationModes)algorithm.AlgorithmProperties[1].CurrentValue,
                    offset: (Point)algorithm.AlgorithmProperties[2].CurrentValue);

                int maxLen = 0;
                int maxIdx = -1;

                for (int i = 0; i < contours.Length; i++)
                {
                    if (contours[i].Length > maxLen)
                    {
                        maxIdx = i;
                        maxLen = contours[i].Length;
                    }

                    if (contours[i].Length > (int)algorithm.AlgorithmProperties[8].CurrentValue)
                    {
                        Cv2.DrawContours(
                            mOutput,
                            contours,
                            contourIdx: i,
                            color: (Scalar)algorithm.AlgorithmProperties[3].CurrentValue,
                            thickness: (int)algorithm.AlgorithmProperties[4].CurrentValue,
                            lineType: (LineTypes)algorithm.AlgorithmProperties[5].CurrentValue,
                            hierarchy: outputArray,
                            maxLevel: 0);
                    }
                }
                if (maxIdx != -1)
                {
                    var res = Cv2.ApproxPolyDP(contours[maxIdx], 1, true);
                    //Cv2.DrawContours(
                    //    mOutput,
                    //    contours,
                    //    maxIdx,
                    //    (Scalar)algorithm.algorithmProperties[3].CurrentValue,
                    //    (int)algorithm.algorithmProperties[4].CurrentValue,
                    //    (LineTypes)algorithm.algorithmProperties[5].CurrentValue,
                    //    outputArray,
                    //    0);
                    ////return Cv2.ContourArea(res);
                }

                Mat2SoftwareBitmap(mOutput, output);

                // Must run on UI thread.  The winrt container also needs to be set.
                if (App.container != null)
                {
                    await App.container.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        Cv2.ImShow("Contours", mOutput);
                    });
                }
            }
        }