Exemple #1
0
        public void Bitwise()
        {
            const int count  = 256;
            var       random = new Random(0);
            var       array1 = Enumerable.Range(0, count).Select(i => (byte)i).OrderBy(_ => random.Next()).ToArray();
            var       array2 = Enumerable.Range(0, count).Select(i => (byte)i).OrderBy(_ => random.Next()).ToArray();

            using var mat1 = new Mat(count, 1, MatType.CV_8UC1, array1);
            using var mat2 = new Mat(count, 1, MatType.CV_8UC1, array2);
            using var and  = new Mat();
            using var or   = new Mat();
            using var xor  = new Mat();
            using var not  = new Mat();
            Cv2.BitwiseAnd(mat1, mat2, and);
            Cv2.BitwiseOr(mat1, mat2, or);
            Cv2.BitwiseXor(mat1, mat2, xor);
            Cv2.BitwiseNot(mat1, not);

            for (int i = 0; i < count; i++)
            {
                Assert.Equal((byte)(array1[i] & array2[i]), and.Get <byte>(i));
                Assert.Equal((byte)(array1[i] | array2[i]), or.Get <byte>(i));
                Assert.Equal((byte)(array1[i] ^ array2[i]), xor.Get <byte>(i));
                Assert.Equal((byte)(~array1[i]), not.Get <byte>(i));
            }
        }
        public IplImage BitwiseMat(IplImage src)
        {
            Mat Input1  = new Mat(src);
            Mat Input2  = new Mat(this.Binary(src, 150));
            Mat bitwise = new Mat();

            Window win_src1 = new Window("src1", WindowMode.StretchImage, Input1);
            Window win_src2 = new Window("src2", WindowMode.StretchImage, Input2);

            Cv2.BitwiseNot(Input1, bitwise);
            Window win_Not = new Window("BitwiseNot", WindowMode.StretchImage, bitwise);

            Cv2.BitwiseAnd(Input1, Input2.CvtColor(ColorConversion.GrayToBgr), bitwise);
            Window win_And = new Window("BitwiseAnd", WindowMode.StretchImage, bitwise);

            Cv2.BitwiseOr(Input1, Input2.CvtColor(ColorConversion.GrayToBgr), bitwise);
            Window win_Or = new Window("BitwiseOr", WindowMode.StretchImage, bitwise);

            Cv2.BitwiseXor(Input1, Input2.CvtColor(ColorConversion.GrayToBgr), bitwise);
            Window win_Xor = new Window("BitwiseXor", WindowMode.StretchImage, bitwise);

            //IplImage 형식//

            //Cv.Not();
            //Cv.And();
            //Cv.Or();
            //Cv.Xor();

            //IplImage 형식//

            return(Input2.ToIplImage());
        }
Exemple #3
0
        public void Apply(Mat src)
        {
            using (ResourceTracker t = new ResourceTracker())
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Size srcSize = src.Size();

                //todo: 只对脸部进行亮肤https://blog.csdn.net/cnbloger/article/details/77949949
                //https://blog.csdn.net/skyqsdyy/article/details/89467143
                //todo: 对src进行美颜、瘦脸、亮肤等。只要控制在一帧50ms之内即可。
                Mat matMask = t.NewMat(srcSize, MatType.CV_8UC1, new Scalar(0));
                RenderGreenScreenMask(src, matMask);
                //the area is by integer instead of double, so that it can improve the performance of comparision of areas
                int minBlockArea = (int)(srcSize.Width * srcSize.Height * this.MinBlockPercent);
                var contoursExternalForeground = Cv2.FindContoursAsArray(matMask, RetrievalModes.External, ContourApproximationModes.ApproxNone)
                                                 .Select(c => new { contour = c, Area = (int)Cv2.ContourArea(c) })
                                                 .Where(c => c.Area >= minBlockArea)
                                                 .OrderByDescending(c => c.Area).Take(5).Select(c => c.contour);
                //contoursExternalForeground = contoursExternalForeground.Select(c=>Cv2.ApproxPolyDP(c,0.5,true));

                //a new Mat used for rendering the selected Contours
                var matMaskForeground = t.NewMat(srcSize, MatType.CV_8UC1, new Scalar(0));
                //thickness: -1 means filling the inner space
                matMaskForeground.DrawContours(contoursExternalForeground, -1, new Scalar(255),
                                               thickness: -1);

                //matInternalHollow is the inner Hollow parts of body part.
                var matInternalHollow = t.NewMat(srcSize, MatType.CV_8UC1, new Scalar(0));
                Cv2.BitwiseXor(matMaskForeground, matMask, matInternalHollow);

                int minHollowArea = (int)(minBlockArea * 0.5);//the lower size limitation of InternalHollow is less than minBlockArea, because InternalHollows are smaller
                //find the Contours of Internal Hollow
                var contoursInternalHollow = Cv2.FindContoursAsArray(matInternalHollow, RetrievalModes.External, ContourApproximationModes.ApproxNone)
                                             .Select(c => new { contour = c, Area = Cv2.ContourArea(c) })
                                             .Where(c => c.Area >= minHollowArea)
                                             .OrderByDescending(c => c.Area).Take(10).Select(c => c.contour);
                //draw hollows
                foreach (var c in contoursInternalHollow)
                {
                    matMaskForeground.FillConvexPoly(c, new Scalar(0));
                }

                var element = t.T(Cv2.GetStructuringElement(MorphShapes.Cross, new Size(3, 3)));
                //smooth the edge of matMaskForeground
                Cv2.MorphologyEx(matMaskForeground, matMaskForeground, MorphTypes.Close,
                                 element, iterations: 6);
                Cv2.GaussianBlur(matMaskForeground, matMaskForeground, new Size(3, 3), 0, 0);

                var foreground = t.NewMat(src.Size(), MatType.CV_8UC4, new Scalar(0));
                ZackCVHelper.AddAlphaChannel(src, foreground, matMaskForeground);
                //resize the _backgroundImage to the same size of src
                Cv2.Resize(_backgroundImage, src, src.Size());

                //draw foreground(people) on the backgroundimage
                ZackCVHelper.DrawOverlay(src, foreground);
                Debug.WriteLine($"5:{sw.ElapsedMilliseconds}");
            }
        }
Exemple #4
0
        public Mat Process(Mat frame)
        {
            mFrame = frame;

            Size blurWinSize = new Size(mBlurWinWidth, mBlurWinHeight);

            mFrame.Blur(blurWinSize);

            var reshaped = mFrame.Reshape(cn: 3, rows: mFrame.Rows * mFrame.Cols);
            var samples  = new Mat();

            reshaped.ConvertTo(samples, MatType.CV_32FC3);

            var bestLabels = new Mat();
            var centers    = new Mat();

            Cv2.Kmeans(samples,
                       mClustNum,
                       bestLabels,
                       new TermCriteria(type: CriteriaType.Eps | CriteriaType.MaxIter, maxCount: 10, epsilon: 1.0),
                       3,
                       KMeansFlags.PpCenters,
                       centers);

            //This is not optimal solution, but it works in this case.
            Mat clusteredImage = new Mat(mFrame.Rows, mFrame.Cols, mFrame.Type());

            for (var size = 0; size < mFrame.Cols * mFrame.Rows; size++)
            {
                var clusterIndex = bestLabels.At <int>(0, size);
                var newPixel     = new Vec3b
                {
                    Item0 = (byte)(centers.At <float>(clusterIndex, 0)), // B
                    Item1 = (byte)(centers.At <float>(clusterIndex, 1)), // G
                    Item2 = (byte)(centers.At <float>(clusterIndex, 2))  // R
                };
                clusteredImage.Set(size / mFrame.Cols, size % mFrame.Cols, newPixel);
            }

            mFrame = clusteredImage;

            Mat gray   = mFrame.CvtColor(ColorConversionCodes.BGR2GRAY);
            Mat thresh = gray.Threshold(140, 255, ThresholdTypes.Otsu);

            Mat erode  = thresh.Erode(new Mat());
            Mat dilate = erode.Dilate(new Mat());

            Mat morph = new Mat();

            Cv2.BitwiseXor(erode, dilate, morph);

            return(morph);
        }
Exemple #5
0
        //static Mat pre_Prosessing()
        //{
        //    Mat fdfdfd= new Mat();

        //    return fdfdfd;  이미지와 배열을 리턴해줘야한다.
        //}//전처리하는부분

        static void Main(string[] args)
        {
            Mat src    = Cv2.ImRead("./samples2/209.bmp");
            Mat gray   = new Mat(src.Size(), MatType.CV_8UC1);
            Mat binary = new Mat(src.Size(), MatType.CV_8UC1);
            Mat dst    = src.Clone();
            Mat rotate = new Mat();
            Mat Roi    = new Mat();

            double angle = default;

            Point[][]        contours;
            HierarchyIndex[] hier;


            Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));

            Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
            Cv2.Threshold(gray, binary, 40, 255, ThresholdTypes.Binary);
            Cv2.ImShow("aa", binary);

            // 존좋탱
            //Point[][] contour;
            //HierarchyIndex[] hieracy;

            //Cv2.FindContours(binary, out contour, out hieracy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
            //Cv2.DrawContours(src, contour, -1, new Scalar(0, 0, 255), 3);
            //Cv2.ImShow("cc", src);

            // 진우탱 : 레이블을 계산 - 2개 초과이면 불량품
            Mat ds    = gray.Clone();
            Mat noise = new Mat();

            Cv2.Threshold(ds, ds, 195, 255, ThresholdTypes.Binary);
            Cv2.BitwiseXor(binary, ds, noise);
            Cv2.BitwiseXor(noise, ds, noise);
            Cv2.ImShow("noise", noise);

            Point[][]        contour;
            HierarchyIndex[] hieracy;

            Cv2.FindContours(ds, out contour, out hieracy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
            Cv2.DrawContours(src, contour, -1, new Scalar(0, 0, 255), 3);
            Cv2.ImShow("linecheck", src);

            Console.WriteLine("개수 : " + contour.Length);
            if (contour.Length > 2)
            {
                Console.WriteLine("불량품");
            }
            else
            {
                Console.WriteLine("양품");
            }


            //Cv2.ImShow("bb", binary - gray);
            //Cv2.MorphologyEx(binary, binary, MorphTypes.Close, kernel, new Point(-1, -1), 5);
            //Cv2.MorphologyEx(binary, binary, MorphTypes.Erode, kernel, new Point(-1, -1), 20);
            //Cv2.MorphologyEx(binary, binary, MorphTypes.Dilate, kernel, new Point(-1, -1), 20);


            //Cv2.FindContours(binary, out contours, out hier, RetrievalModes.External, ContourApproximationModes.ApproxTC89KCOS);


            //Point[] point_rect = new Point[4];

            //for (int i = 0; i < contours.Length; i++)
            //{
            //    double perimeter = Cv2.ArcLength(contours[i], true);
            //    double epsilon = perimeter * 0.04;

            //    Point[] approx = Cv2.ApproxPolyDP(contours[i], epsilon, true);
            //    Point[][] draw_approx = new Point[][] { approx };

            //    // Cv2.DrawContours(dst, contours, -1, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
            //    Cv2.DrawContours(dst, draw_approx, -1, new Scalar(255, 0, 0), 2, LineTypes.AntiAlias);


            //    // 좌표를 표시하는 부분
            //    for (int j = 0; j < approx.Length; j++)
            //    {
            //        Cv2.Circle(dst, approx[j], 1, new Scalar(0, 0, 255), 3);

            //        point_rect[j] = approx[j];
            //        Console.WriteLine(point_rect[j]);
            //    }
            //}

            ////ROI를 검출하는 로직  x의 min, max 과 y의 min,max 찾기
            //int max_x = 0;
            //int min_x = int.MaxValue;
            //int max_y = 0;
            //int min_y = int.MaxValue;

            //angle = calculate_angle(point_rect);
            //Roi = make_ROI(dst, min_y, max_y, min_x, max_x, point_rect);
            //rotate = Rotate_rect(Roi, angle);

            //Console.WriteLine(angle);

            //Cv2.ImShow("src", src);
            //Cv2.ImShow("binary", binary);
            //Cv2.ImShow("dst", dst);
            //Cv2.ImShow("Roi", Roi);
            //Cv2.ImShow("Rotate", rotate);
            Cv2.WaitKey(0);
            Cv2.DestroyAllWindows();
        }