예제 #1
0
 public void WeightedMedianFilter()
 {
     using var src = Image("lenna.png", ImreadModes.Grayscale);
     using var dst = new Mat();
     CvXImgProc.WeightedMedianFilter(src, src, dst, 7);
     ShowImagesWhenDebugMode(dst);
 }
예제 #2
0
 public void Thinning()
 {
     using var src = Image("blob/shapes2.png", ImreadModes.Grayscale);
     using var dst = new Mat();
     CvXImgProc.Thinning(src, dst, ThinningTypes.ZHANGSUEN);
     ShowImagesWhenDebugMode(src, dst);
 }
예제 #3
0
 public void AnisotropicDiffusion()
 {
     using var src = Image("blob/shapes2.png", ImreadModes.Color);
     using var dst = new Mat();
     CvXImgProc.AnisotropicDiffusion(src, dst, 1, 1, 1);
     ShowImagesWhenDebugMode(src, dst);
 }
예제 #4
0
 public void Niblack(LocalBinarizationMethods method)
 {
     using var src = Image("mandrill.png", ImreadModes.Grayscale);
     using var dst = new Mat();
     CvXImgProc.NiblackThreshold(src, dst, 255, ThresholdTypes.Binary, 5, 0.5, method);
     ShowImagesWhenDebugMode(dst);
 }
예제 #5
0
 public void EdgePreservingFilter()
 {
     using var src = Image("lenna.png", ImreadModes.Color);
     using var dst = new Mat();
     CvXImgProc.EdgePreservingFilter(src, dst, 7, 10.0);
     ShowImagesWhenDebugMode(src, dst);
 }
예제 #6
0
        public void PeiLinNormalization()
        {
            using var src  = Image("peilin_plane.png", ImreadModes.Grayscale);
            using var tMat = src.Clone();
            CvXImgProc.PeiLinNormalization(src, tMat);
            var tArray = CvXImgProc.PeiLinNormalization(src);

            Assert.Equal(MatType.CV_64FC1, tMat.Type());
            Assert.Equal(2, tMat.Rows);
            Assert.Equal(3, tMat.Cols);
            Assert.Equal(2, tArray.GetLength(0));
            Assert.Equal(3, tArray.GetLength(1));

            for (int r = 0; r < 2; r++)
            {
                for (int c = 0; c < 3; c++)
                {
                    Assert.Equal(tArray[r, c], tMat.At <double>(r, c));
                }
            }

            if (Debugger.IsAttached)
            {
                using var warped = new Mat();
                Cv2.WarpAffine(src, warped, tMat, src.Size());
                Window.ShowImages(src, warped);
            }
        }
예제 #7
0
        public override void RunTest()
        {
            using var src     = Cv2.ImRead(ImagePath.Binarization, ImreadModes.Grayscale);
            using var niblack = new Mat();
            using var sauvola = new Mat();
            using var nick    = new Mat();
            int kernelSize = 51;

            var sw = new Stopwatch();

            sw.Start();
            CvXImgProc.NiblackThreshold(src, niblack, 255, ThresholdTypes.Binary, kernelSize, -0.2, LocalBinarizationMethods.Niblack);
            sw.Stop();
            Console.WriteLine($"Niblack {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            CvXImgProc.NiblackThreshold(src, sauvola, 255, ThresholdTypes.Binary, kernelSize, 0.1, LocalBinarizationMethods.Sauvola);
            sw.Stop();
            Console.WriteLine($"Sauvola {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            CvXImgProc.NiblackThreshold(src, nick, 255, ThresholdTypes.Binary, kernelSize, -0.14, LocalBinarizationMethods.Nick);
            sw.Stop();
            Console.WriteLine($"Nick {sw.ElapsedMilliseconds} ms");

            using (new Window("src", src, WindowFlags.AutoSize))
                using (new Window("Niblack", niblack, WindowFlags.AutoSize))
                    using (new Window("Sauvola", sauvola, WindowFlags.AutoSize))
                        using (new Window("Nick", nick, WindowFlags.AutoSize))
                        {
                            Cv2.WaitKey();
                        }
        }
예제 #8
0
 public void BrightEdges()
 {
     using var src = Image("lenna.png", ImreadModes.Color);
     using var dst = new Mat();
     CvXImgProc.BrightEdges(src, dst);
     ShowImagesWhenDebugMode(src, dst);
 }
예제 #9
0
 public void CreateAndDispose2()
 {
     using (var eb = CvXImgProc.CreateEdgeBoxes())
     {
         GC.KeepAlive(eb);
     }
 }
예제 #10
0
        public void Run()
        {
            using var src     = Cv2.ImRead(@"G:\其他项目\YuZe.ColoredPencil\YuZe.ColoredPencil\P10424-172645.jpg", ImreadModes.Grayscale);
            using var niblack = new Mat();
            using var sauvola = new Mat();
            using var bernsen = new Mat();
            using var nick    = new Mat();
            int kernelSize = 51;

            var sw = new Stopwatch();

            sw.Start();
            CvXImgProc.NiblackThreshold(src, niblack, 255, ThresholdTypes.Binary, kernelSize, -0.2, LocalBinarizationMethods.Niblack);
            sw.Stop();
            Console.WriteLine($"Niblack {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            CvXImgProc.NiblackThreshold(src, sauvola, 255, ThresholdTypes.Binary, kernelSize, 0.1, LocalBinarizationMethods.Sauvola);
            sw.Stop();
            Console.WriteLine($"Sauvola {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            CvXImgProc.NiblackThreshold(src, nick, 255, ThresholdTypes.Binary, kernelSize, -0.14, LocalBinarizationMethods.Nick);
            sw.Stop();
            Console.WriteLine($"Nick {sw.ElapsedMilliseconds} ms");

            using (new Window("src", src, WindowFlags.Normal))
                using (new Window("Niblack", niblack, WindowFlags.Normal))
                    using (new Window("Sauvola", sauvola, WindowFlags.Normal))
                        using (new Window("Nick", nick, WindowFlags.Normal))
                        {
                            Cv2.WaitKey();
                        }
        }
예제 #11
0
 public void GradientDeriche()
 {
     using var src  = Image("lenna.png", ImreadModes.Color);
     using var dstX = new Mat();
     using var dstY = new Mat();
     CvXImgProc.GradientDericheX(src, dstX, 10.0, 10.0);
     CvXImgProc.GradientDericheX(src, dstY, 10.0, 10.0);
     ShowImagesWhenDebugMode(src, dstX, dstY);
 }
예제 #12
0
 public void Niblack()
 {
     using (var src = Image("lenna.png", ImreadModes.GrayScale))
         using (var dst = new Mat())
         {
             CvXImgProc.NiblackThreshold(src, dst, 255, ThresholdTypes.Binary, 5, 0.5);
             ShowImagesWhenDebugMode(dst);
         }
 }
예제 #13
0
        public void CovarianceEstimation()
        {
            const int windowSize = 7;

            using var src = Image("lenna.png", ImreadModes.Grayscale);
            using var dst = new Mat();
            CvXImgProc.CovarianceEstimation(src, dst, windowSize, windowSize);
            // TODO
            Assert.Equal(windowSize * windowSize, dst.Rows);
            Assert.Equal(windowSize * windowSize, dst.Cols);
            Assert.Equal(MatType.CV_32FC2, dst.Type());
        }
예제 #14
0
        public void CovarianceEstimation()
        {
            const int windowSize = 7;

            using (var src = Image("lenna.png", ImreadModes.GrayScale))
                using (var dst = new Mat())
                {
                    CvXImgProc.CovarianceEstimation(src, dst, windowSize, windowSize);
                    // TODO
                    Assert.That(dst.Rows, Is.EqualTo(windowSize * windowSize));
                    Assert.That(dst.Cols, Is.EqualTo(windowSize * windowSize));
                    Assert.That(dst.Type(), Is.EqualTo(MatType.CV_32FC2));
                }
        }
예제 #15
0
 public void Sauvola()
 {
     foreach (var r in new double[] { 16, 32, 64, 128 })
     {
         using var src = Image("mandrill.png", ImreadModes.Grayscale);
         using var dst = new Mat();
         CvXImgProc.NiblackThreshold(
             src, dst,
             255,
             ThresholdTypes.Binary,
             5, 0.5,
             LocalBinarizationMethods.Sauvola,
             r);
         ShowImagesWhenDebugMode(new[] { dst }, new[] { $"r={r}" });
     }
 }
예제 #16
0
        public void ColorMatchTemplate()
        {
            using var src      = Image("lenna.png", ImreadModes.Color);
            using var template = src[new Rect(200, 230, 150, 150)];
            using var dst      = new Mat();

            CvXImgProc.ColorMatchTemplate(src, template, dst);
            Assert.False(dst.Empty());
            Assert.Equal(MatType.CV_64FC1, dst.Type());

            dst.MinMaxLoc(out var minVal, out var maxVal, out var minLoc, out var maxLoc);

            using var view = src.Clone();
            view.Rectangle(maxLoc, new Point(maxLoc.X + template.Width, maxLoc.Y + template.Height), Scalar.Red, 3);
            ShowImagesWhenDebugMode(view, template);
        }
        public void FastHoughTransform()
        {
            using (var image = Image("building.jpg", ImreadModes.Grayscale))
                using (var fht = new Mat())
                {
                    CvXImgProc.FastHoughTransform(image, fht, MatType.CV_32SC1);

                    Cv2.MinMaxLoc(fht, out var minv, out double maxv);

                    Mat ucharFht = new Mat();
                    fht.ConvertTo(ucharFht, MatType.CV_8UC1,
                                  255.0 / (maxv + minv), minv / (maxv + minv));
                    Rescale(ucharFht, ucharFht);

                    //Cv2.ImShow("fast hough transform", ucharFht);
                    //Cv2.WaitKey();
                }
        public void Run()
        {
            using var src     = Cv2.ImRead(FilePath.Image.Binarization, ImreadModes.Grayscale);
            using var niblack = new Mat();
            using var sauvola = new Mat();
            using var bernsen = new Mat();
            using var nick    = new Mat();
            int kernelSize = 51;

            var sw = new Stopwatch();

            sw.Start();
            CvXImgProc.NiblackThreshold(src, niblack, 255, ThresholdTypes.Binary, kernelSize, -0.2, LocalBinarizationMethods.Niblack);
            sw.Stop();
            Console.WriteLine($"Niblack {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            CvXImgProc.NiblackThreshold(src, sauvola, 255, ThresholdTypes.Binary, kernelSize, 0.1, LocalBinarizationMethods.Sauvola);
            sw.Stop();
            Console.WriteLine($"Sauvola {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            Binarizer.Bernsen(src, bernsen, kernelSize, 50, 200);
            sw.Stop();
            Console.WriteLine($"Bernsen {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            CvXImgProc.NiblackThreshold(src, nick, 255, ThresholdTypes.Binary, kernelSize, -0.14, LocalBinarizationMethods.Nick);
            sw.Stop();
            Console.WriteLine($"Nick {sw.ElapsedMilliseconds} ms");

            using (new Window("src", WindowMode.AutoSize, src))
                using (new Window("Niblack", WindowMode.AutoSize, niblack))
                    using (new Window("Sauvola", WindowMode.AutoSize, sauvola))
                        using (new Window("Bernsen", WindowMode.AutoSize, bernsen))
                            using (new Window("Nick", WindowMode.AutoSize, nick))
                            {
                                Cv2.WaitKey();
                            }
        }
예제 #19
0
        public static SoftwareBitmap AutoColorEnhancement(SoftwareBitmap Input)
        {
            using (Mat inputMat = Input.SoftwareBitmapToMat())
                using (Mat outputMat = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_8UC4))
                    using (Mat Temp = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                        using (Mat Gray = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                        {
                            inputMat.ConvertTo(Temp, MatType.CV_32FC3);

                            Cv2.CvtColor(Temp, Gray, ColorConversionCodes.BGR2GRAY);

                            Cv2.MinMaxLoc(Gray, out _, out double LwMax);

                            float LwAver;
                            int   Num;

                            using (Mat Lw_ = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                            {
                                Num = inputMat.Rows * inputMat.Cols;

                                //计算每个数组元素绝对值的自然对数
                                Cv2.Log(Gray + 1e-3f, Lw_);

                                //矩阵自然指数
                                LwAver = Convert.ToSingle(Math.Exp(Cv2.Sum(Lw_)[0] / Num));
                            }

                            using (Mat Lg = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                            {
                                Cv2.Log(Gray / LwAver + 1f, Lg);
                                Cv2.Divide(Lg, Math.Log(LwMax / LwAver + 1f), Lg);

                                //局部自适应
                                using (Mat Lout = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                                {
                                    int kernelSize = Convert.ToInt32(Math.Floor(Convert.ToDouble(Math.Max(3, Math.Max(inputMat.Rows / 100, inputMat.Cols / 100)))));

                                    using (Mat Lp = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                                    {
                                        using (Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(kernelSize, kernelSize)))
                                        {
                                            Cv2.Dilate(Lg, Lp, kernel);
                                        }

                                        using (Mat Hg = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                                        {
                                            CvXImgProc.GuidedFilter(Lg, Lp, Hg, 10, 0.01);

                                            Cv2.MinMaxLoc(Lg, out _, out double LgMax);

                                            using (Mat alpha = 1.0f + Lg * (36 / LgMax))
                                                using (Mat Lg_ = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32FC3))
                                                {
                                                    Cv2.Log(Lg + 1e-3f, Lg_);

                                                    float beta = 10 * Convert.ToSingle(Math.Exp(Cv2.Sum(Lg_)[0] / Num));

                                                    Cv2.Log(Lg / Hg + beta, Lout);
                                                    Cv2.Multiply(alpha, Lout, Lout);
                                                    Cv2.Normalize(Lout, Lout, 0, 255, NormTypes.MinMax);
                                                }
                                        }
                                    }

                                    using (Mat gain = new Mat(inputMat.Rows, inputMat.Cols, MatType.CV_32F))
                                    {
                                        for (int i = 0; i < inputMat.Rows; i++)
                                        {
                                            for (int j = 0; j < inputMat.Cols; j++)
                                            {
                                                float x = Gray.At <float>(i, j);
                                                float y = Lout.At <float>(i, j);
                                                gain.At <float>(i, j) = 0 == x ? y : y / x;
                                            }
                                        }

                                        Mat[] BGRChannel = Cv2.Split(Temp);

                                        try
                                        {
                                            BGRChannel[0] = (gain.Mul(BGRChannel[0] + Gray) + BGRChannel[0] - Gray) * 0.5f;
                                            BGRChannel[1] = (gain.Mul(BGRChannel[1] + Gray) + BGRChannel[1] - Gray) * 0.5f;
                                            BGRChannel[2] = (gain.Mul(BGRChannel[2] + Gray) + BGRChannel[2] - Gray) * 0.5f;

                                            Cv2.Merge(BGRChannel, Temp);
                                            Temp.ConvertTo(outputMat, MatType.CV_8UC4);
                                        }
                                        finally
                                        {
                                            Array.ForEach(BGRChannel, (Channel) => Channel.Dispose());
                                        }
                                    }
                                }
                            }

                            return(outputMat.MatToSoftwareBitmap());
                        }
        }
        public void FHTSample()
        {
            const string imPath = @"_data\image\building.jpg";

            using (var image = new Mat(imPath, ImreadModes.GrayScale))
                using (var hough = new Mat())
                    using (var canny = new Mat())
                    {
                        Cv2.Canny(image, canny, 50, 200, 3);

                        CvXImgProc.FastHoughTransform(canny, hough, MatType.CV_32S /*C1*/, AngleRangeOption.ARO_315_135, HoughOP.FHT_ADD, HoughDeskewOption.DESKEW);

                        var lines = new List <Vec4i>();
                        GetLocalExtr(lines, canny, hough, 255f * 0.3f * Math.Min(canny.Rows, canny.Cols), 50);

                        var cannyColor = new Mat();
                        Cv2.CvtColor(canny, cannyColor, ColorConversionCodes.GRAY2BGR);
                        for (var i = 0; i < lines.Count; i++)
                        {
                            var line = lines[i];
                            Cv2.Line(cannyColor, new Point(line.Item0, line.Item1), new Point(line.Item2, line.Item3), Scalar.Red);
                        }
                        //cannyColor.SaveImage("cannycolor.png");

                        ShowImagesWhenDebugMode(image, canny, cannyColor);
                    }

            bool GetLocalExtr(List <Vec4i> lines, Mat src, Mat fht, float minWeight, int maxCount)
            {
                const int MAX_LEN        = 10_000;
                var       weightedPoints = new List <KeyValuePair <int, Point> >();

                for (var y = 0; y < fht.Rows; ++y)
                {
                    if (weightedPoints.Count > MAX_LEN)
                    {
                        break;
                    }

                    var fhtMat     = new MatOfInt(fht);
                    var fhtIndexer = fhtMat.GetIndexer();

                    var pLineY = Math.Max(y - 1, 0);
                    var cLineY = y;
                    var nLineY = Math.Min(y + 1, fht.Rows - 1);

                    for (var x = 0; x < fht.Cols; ++x)
                    {
                        if (weightedPoints.Count > MAX_LEN)
                        {
                            break;
                        }

                        var value = fhtIndexer[cLineY, x];
                        if (value >= minWeight)
                        {
                            var isLocalMax = 0;
                            var start      = Math.Max(x - 1, 0);
                            var end        = Math.Min(x + 1, fht.Cols - 1);
                            for (var xx = start; xx < end; ++xx)
                            {
                                var pLine = fhtIndexer[pLineY, xx];
                                var cLine = fhtIndexer[cLineY, xx];
                                var nLine = fhtIndexer[nLineY, xx];
                                if (!incIfGreater(value, pLine, ref isLocalMax) ||
                                    !incIfGreater(value, cLine, ref isLocalMax) ||
                                    !incIfGreater(value, nLine, ref isLocalMax))
                                {
                                    isLocalMax = 0;
                                    break;
                                }
                            }
                            if (isLocalMax > 0)
                            {
                                weightedPoints.Add(new KeyValuePair <int, Point>(value, new Point(x, y)));
                            }
                        }
                    }
                }

                if (weightedPoints.Count == 0)
                {
                    return(true);
                }

                // Sort WeightedPoints
                weightedPoints = weightedPoints.OrderByDescending(x => x.Key).ToList();
                weightedPoints = weightedPoints.Take(maxCount).ToList();

                for (var i = 0; i < weightedPoints.Count; i++)
                {
                    lines.Add(CvXImgProc.HoughPoint2Line(weightedPoints[i].Value, src));
                }

                return(true);
            }

            bool incIfGreater(int a, int b, ref int value)
            {
                if (/*value == 0 || */ a < b)
                {
                    return(false);
                }
                if (a > b)
                {
                    ++(value);
                }
                return(true);
            }
        }
예제 #21
0
        public void New2()
        {
            var fld = CvXImgProc.CreateFastLineDetector();

            fld.Dispose();
        }