コード例 #1
0
        static bool RunOn(string file, OpenCvSharp.Rect boundingBox)
        {
            int areas = 0;

            int[] quadrants = new int[4];
            using (OpenCvSharp.Mat m = new OpenCvSharp.Mat(file, OpenCvSharp.ImreadModes.Grayscale))
            {
                //blur the image a little
                using (var blurred = m.GaussianBlur(new Size(3, 3), 0))
                {
                    //make the image binary black or white and make black the background color
                    using (var g = blurred.Threshold(200, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu))
                    {
                        var element = Cv2.GetStructuringElement(
                            MorphShapes.Rect,
                            new Size(50, 1));
                        //remove lines from dark background image by creating a mask
                        using (var mask = g.MorphologyEx(MorphTypes.Open, element, iterations: 2))
                        {
                            using (Mat newMask = new Mat())
                            {
                                //mask bits should be 0 to skip copying items
                                Cv2.BitwiseNot(mask, newMask);



                                using (Mat newImage = new Mat())
                                {
                                    //make new image and apply mask so as to not copy the lines
                                    g.CopyTo(newImage, newMask);

                                    //create the box image
                                    using (OpenCvSharp.Mat box = new OpenCvSharp.Mat(new Size(boundingBox.Width, boundingBox.Height), MatType.CV_8U))
                                    {
                                        //copy to the box
                                        newImage[boundingBox].CopyTo(box);



                                        using (Mat labels = new Mat())
                                        {
                                            using (var centroids = new Mat())
                                            {
                                                using (Mat stats = new Mat())
                                                {
                                                    //find the white blobs
                                                    //populate the quadrants blobs appear in
                                                    //create total area of white stuff

                                                    int cnt = Cv2.ConnectedComponentsWithStats(box, labels, stats, centroids, PixelConnectivity.Connectivity8);
#if usequadrants
                                                    int qh = box.Size().Height / 2;
                                                    int qw = box.Size().Width / 2;

                                                    var tl = new Rect(0, 0, qw, qh);
                                                    var vl = new Rect(0, qh, qw, qh);
                                                    var tr = new Rect(qw, 0, qw, qh);
                                                    var br = new Rect(qw, qh, qw, qh);
#endif
                                                    for (var x = 1; x < stats.Size().Height; x++)
                                                    {
                                                        #if usequadrants
                                                        var left   = stats.Get <int>(x, (int)ConnectedComponentsTypes.Left);
                                                        var top    = stats.Get <int>(x, (int)ConnectedComponentsTypes.Top);
                                                        var width  = stats.Get <int>(x, (int)ConnectedComponentsTypes.Width);
                                                        var height = stats.Get <int>(x, (int)ConnectedComponentsTypes.Height);

                                                        var re = new Rect(left, top, width, height);
                                                        if (re.IntersectsWith(tl))
                                                        {
                                                            quadrants[0] = 1;
                                                        }
                                                        if (re.IntersectsWith(vl))
                                                        {
                                                            quadrants[1] = 1;
                                                        }
                                                        if (re.IntersectsWith(tr))
                                                        {
                                                            quadrants[2] = 1;
                                                        }
                                                        if (re.IntersectsWith(br))
                                                        {
                                                            quadrants[3] = 1;
                                                        }
#endif
                                                        areas += stats.Get <int>(x, (int)ConnectedComponentsTypes.Area);
                                                    }
                                                }
                                            }
                                        }

                                        var boxarea = box.Size().Width *box.Size().Height;



                                        double[] areasTest = new double[] { areas };
                                        double[] boxAreas  = new double[] { boxarea };


                                        //use infer.net to determine if the mean is good or not
                                        VariableArray <bool> ytest = Variable.Array <bool>(new Range(areasTest.Length));
                                        BayesPointMachine(areasTest, boxAreas, Variable.Random(wPosterior), ytest);
                                        var res  = (DistributionStructArray <Bernoulli, bool>)engine.Infer(ytest);
                                        var mean = res[0].GetMean();


                                        Console.WriteLine(boxarea + " " + areas + " " + mean + " "
                                            #if usequadrants
                                                          + quadrants.Sum());