Exemple #1
0
        static void Main(string[] args)
        {
            Mat src1 = Cv2.ImRead("gerbera.jpg");
            Mat dst  = new Mat(src1.Size(), MatType.CV_8UC3);

            Cv2.Compare(src1, new Scalar(200, 127, 100), dst, CmpTypes.GT);

            Cv2.ImShow("dst", dst);
            Cv2.WaitKey(0);
            Cv2.DestroyAllWindows();
        }
Exemple #2
0
    void GrabCut()
    {
        Debug.Log("GrabCut");
        Mat result  = faceDetectionImage.VideoSourceImage;
        Mat bgModel = new Mat(); //background model
        Mat fgModel = new Mat(); //foreground model

        //draw a rectangle
        OpenCvSharp.Rect rectangle = new OpenCvSharp.Rect(1, 1, faceDetectionImage.VideoSourceImage.Cols - 1, faceDetectionImage.VideoSourceImage.Rows - 1);
        Cv2.GrabCut(faceDetectionImage.VideoSourceImage, result, rectangle, bgModel, fgModel, 10, GrabCutModes.InitWithRect);
        Cv2.Compare(result, new Scalar(3, 3, 3), result, CmpTypes.EQ);
        matrix2_grabcut = new Mat(faceDetectionImage.VideoSourceImage.Size(), MatType.CV_8UC3, new Scalar(255, 255, 255));
        faceDetectionImage.VideoSourceImage.CopyTo(matrix2_grabcut, result);
    }
Exemple #3
0
        public void Compare()
        {
            var bytes = new byte[] { 1, 2, 3, 4, 5, 6 };

            using var src = new Mat(bytes.Length, 1, MatType.CV_8UC1, bytes);
            using var dst = new Mat();

            Cv2.Compare(src, 3, dst, CmpTypes.LE);
            Assert.Equal(255, dst.Get <byte>(0));
            Assert.Equal(255, dst.Get <byte>(1));
            Assert.Equal(255, dst.Get <byte>(2));
            Assert.Equal(0, dst.Get <byte>(3));
            Assert.Equal(0, dst.Get <byte>(4));
            Assert.Equal(0, dst.Get <byte>(5));
        }
        public void cuda_compare()
        {
            Mat  mat1 = Image("lenna.png", ImreadModes.Grayscale);
            Size size = mat1.Size();
            Mat  mat2 = new Mat(size, mat1.Type(), new Scalar(2));

            using (GpuMat g_mat1 = new GpuMat(size, mat1.Type()))
                using (GpuMat dst = new GpuMat()) {
                    GpuMat g_mat2 = new GpuMat(size, mat2.Type());
                    g_mat2.Upload(mat2);
                    g_mat1.Upload(mat1);

                    Cuda.cuda.compare(g_mat1, g_mat2, dst, CmpTypes.EQ);

                    Mat dst_gold = new Mat(size, mat1.Type(), Scalar.Black);
                    Cv2.Compare(mat1, mat2, dst_gold, CmpTypes.EQ);
                    ImageEquals(dst_gold, dst);
                    ShowImagesWhenDebugMode(g_mat1, dst);
                }
        }
Exemple #5
0
        protected static void ImageEquals(Mat img1, Mat img2)
        {
            if (img1 == null && img2 == null)
            {
                return;
            }
            Assert.NotNull(img1);
            Assert.NotNull(img2);
#pragma warning disable CS8602
#pragma warning disable CA1062
            Assert.Equal(img1.Type(), img2.Type());
#pragma warning restore CS8602
#pragma warning restore CA1062

            using (var comparison = new Mat())
            {
                Cv2.Compare(img1, img2, comparison, CmpTypes.NE);
                if (img1.Channels() == 1)
                {
                    Assert.Equal(0, Cv2.CountNonZero(comparison));
                }
                else
                {
                    var channels = Cv2.Split(comparison);
                    try
                    {
                        foreach (var channel in channels)
                        {
                            Assert.Equal(0, Cv2.CountNonZero(channel));
                        }
                    }
                    finally
                    {
                        foreach (var channel in channels)
                        {
                            channel.Dispose();
                        }
                    }
                }
            }
        }
        //Grabcut分割函数封装
        private Mat grabCut(Mat image, int ITER_COUNT, GrabCutModes GRABCUTMODE, int x, int y, int w, int h)
        {
            var       bgModel   = new Mat();
            var       fgdModel  = new Mat();
            var       bgModel1  = new Mat();
            var       fgdModel1 = new Mat();
            var       mask      = new Mat();
            const int GC_PR_FGD = 3;
            Rect      rect      = new Rect();

            rect.X      = x;
            rect.Y      = y;
            rect.Width  = w;
            rect.Height = h;
            Cv2.GrabCut(image, mask, rect, bgModel, fgdModel, ITER_COUNT, GRABCUTMODE);
            Cv2.Compare(mask, GC_PR_FGD, mask, CmpTypes.EQ);
            Mat foreground = new Mat(image.Size(), MatType.CV_8UC3, new Scalar(0, 0, 0));

            image.CopyTo(foreground, mask);
            showRect(rect.X, rect.Y, rect.Width, rect.Height);
            showImagePara(image);
            return(foreground);
        }
        protected void ImageEquals(Mat img1, Mat img2)
        {
            if (img1 == null && img2 == null)
            {
                return;
            }
            Assert.NotNull(img1);
            Assert.NotNull(img2);
            Assert.AreEqual(img1.Type(), img2.Type());

            using (var comparison = new Mat())
            {
                Cv2.Compare(img1, img2, comparison, CmpTypes.NE);
                if (img1.Channels() == 1)
                {
                    Assert.Zero(Cv2.CountNonZero(comparison));
                }
                else
                {
                    var channels = Cv2.Split(comparison);
                    try
                    {
                        foreach (var channel in channels)
                        {
                            Assert.Zero(Cv2.CountNonZero(channel));
                        }
                    }
                    finally
                    {
                        foreach (var channel in channels)
                        {
                            channel.Dispose();
                        }
                    }
                }
            }
        }