Exemple #1
0
    public void drawTrace(Vector3 currPosition)
    {
        int x = (int)currPosition.x - 25;
        int y = 400 - (int)currPosition.z;

        Cv2.Circle(checkedArea, x, y, 2, Scalar.LightGreen, -4, LineTypes.Link8);
        StartCoroutine("waitForOneSecondsAndThanDiscard", currPosition);
    }
        static void VideoRun()
        {
            var cap    = new VideoCapture(@"rabit3V.mp4");
            Mat MARKER = new Mat("rabits.jpg");

            Cv2.Resize(MARKER, MARKER, new OpenCvSharp.Size(500, 500));
            Pattern             pattern             = new Pattern();
            PatternTrackingInfo patternTrackinginfo = new PatternTrackingInfo();
            PatternDetector     detector            = new PatternDetector(true);

            detector.buildPatternFromImage(MARKER, pattern);
            detector.train();



            int sleepTime = 1;

            using (Window window = new Window("capture"))
                using (Mat image = new Mat())
                {
                    while (true)
                    {
                        cap.Read(image);
                        if (image.Empty())
                        {
                            break;
                        }
                        var img = image.Clone();

                        if (detector.findPattern(img, patternTrackinginfo))
                        {
                            patternTrackinginfo.computePose(pattern);

                            var temp = patternTrackinginfo.campos.Get <Point3d>(0);

                            string camposInfo = "x:" + Math.Round(temp.X, 5) + "\ny:" + Math.Round(temp.Y, 5) + "\nz:" + Math.Round(temp.Z, 5);
                            Cv2.PutText(img,
                                        camposInfo,
                                        new OpenCvSharp.Point(0, 80),
                                        HersheyFonts.HersheyComplex,
                                        0.5,
                                        Scalar.White);

                            for (int i = 0; i < patternTrackinginfo.points2d.Rows; i++)
                            {
                                //Console.WriteLine(" x"+(int)patternTrackinginfo.points2d.Get<Point2d>(i).X+" "+ (int)patternTrackinginfo.points2d.Get<Point2d>(i).Y);
                                Cv2.Circle(img, (int)patternTrackinginfo.points2d.Get <Point2d>(i).X, (int)patternTrackinginfo.points2d.Get <Point2d>(i).Y, 5, Scalar.Black, 3);
                            }
                        }


                        window.ShowImage(img);
                        Cv2.WaitKey(sleepTime);
                        img.Release();
                    }
                }
            cap.Release();
        }
Exemple #3
0
        private Mat Process(ref Mat buffer)
        {
            Mat   img   = new Mat();
            AKAZE akaze = AKAZE.Create();

            akaze.Threshold = 0.0001;
            KeyPoint[]        keyPoints;
            DMatch[]          matches;
            List <DMatch>     goodMatches = new List <DMatch>();
            Mat               descriptor  = new Mat();
            DescriptorMatcher matcher     = DescriptorMatcher.Create("BruteForce");

            Cv2.CvtColor(buffer, buffer, ColorConversionCodes.BGR2GRAY);
            akaze.DetectAndCompute(buffer, null, out keyPoints, descriptor);
            Cv2.DrawKeypoints(buffer, keyPoints, img, Scalar.Black);
            Cv2.ImShow("keyps", img);
            if (islastSeted)
            {
                matches = matcher.Match(descriptor, lastDescriptor);
                for (int i = 0; i < matches.Length; i++)
                {
                    if (matches[i].Distance < distanceStandard)
                    {
                        goodMatches.Add(matches[i]);
                    }
                }
                //Cv2.DrawMatches(buffer, keyPoints, lastBuffer, lastkeyPoints, goodMatches, img);
                img = buffer;
                if (goodMatches.Count > 3)
                {
                    float[] average = new float[2];
                    average[0] = 0; average[1] = 0;
                    for (int i = 0; i < goodMatches.Count; i++)
                    {
                        average[0] += keyPoints[goodMatches[0].QueryIdx].Pt.X -
                                      lastkeyPoints[goodMatches[0].TrainIdx].Pt.X;
                        average[1] += keyPoints[goodMatches[0].QueryIdx].Pt.Y -
                                      lastkeyPoints[goodMatches[0].TrainIdx].Pt.Y;
                    }
                    lastPoint      = new Point(lastPoint.X + average[0] / goodMatches.Count, lastPoint.Y + average[1] / goodMatches.Count);
                    lastBuffer     = buffer;
                    lastDescriptor = descriptor;
                    lastkeyPoints  = keyPoints;
                }
                Cv2.Circle(img, lastPoint, 15, Scalar.Red, 3);
            }
            else
            {
                islastSeted    = true;
                img            = buffer;
                lastPoint      = new Point(buffer.Cols / 2, buffer.Rows / 2);
                lastBuffer     = buffer;
                lastDescriptor = descriptor;
                lastkeyPoints  = keyPoints;
            }

            return(img);
        }
        public void cuda_HoughCircles()
        {
            Mat  src  = Mat.Zeros(128, 128, MatType.CV_8UC1);
            Size size = src.Size();

            const float dp             = 2.0f;
            const float minDist        = 0.0f;
            const int   minRadius      = 10;
            const int   maxRadius      = 20;
            const int   cannyThreshold = 100;
            const int   votesThreshold = 20;

            List <Vec3f> circles_gold = new List <Vec3f>();

            circles_gold.Add(new Vec3f(20, 20, minRadius));
            circles_gold.Add(new Vec3f(90, 87, minRadius + 3));
            circles_gold.Add(new Vec3f(30, 70, minRadius + 8));
            circles_gold.Add(new Vec3f(80, 10, maxRadius));


            for (int i = 0; i < circles_gold.Count; ++i)
            {
                Cv2.Circle(src, new Point(circles_gold[i][0], circles_gold[i][1]), (int)circles_gold[i][2], Scalar.White, -1);
            }
            using (GpuMat g_src = new GpuMat(size, src.Type()))
                using (GpuMat d_circles = new GpuMat()) {
                    g_src.Upload(src);

                    HoughCirclesDetector houghCircles = HoughCirclesDetector.create(dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);

                    houghCircles.detect(g_src, d_circles);



                    CircleSegment[] circles;

                    houghCircles.downloadResults(d_circles, out circles);

                    for (int i = 0; i < circles.Length; ++i)
                    {
                        bool found = false;

                        for (int j = 0; j < circles_gold.Count; ++j)
                        {
                            Vec3f gold = circles_gold[j];

                            if (System.Math.Abs(circles[i].Center.X - gold[0]) < 5 && System.Math.Abs(circles[i].Center.Y - gold[1]) < 5 && System.Math.Abs(circles[i].Radius - gold[2]) < 5)
                            {
                                found = true;
                                break;
                            }
                        }

                        Assert.True(found);
                    }
                    ShowImagesWhenDebugMode(src, g_src);
                }
        }
        public static void DetectHand()
        {
            var cap = new VideoCapture();

            cap.Open(0);
            while (cap.IsOpened())
            {
                try
                {
                    var image = new Mat();
                    cap.Read(image);
                    var mask_img = Skinmask(image);
                    OpenCvSharp.Point[] contour;
                    var hull = GetCNTHull(mask_img, out contour);
                    image.DrawContours(new List <OpenCvSharp.Point[]>()
                    {
                        contour
                    }, -1, new Scalar(255, 255, 0), 2);
                    image.DrawContours(new List <OpenCvSharp.Point[]>()
                    {
                        hull
                    }, -1, new Scalar(0, 255, 255), 2);
                    var defects = GetDefects(contour);
                    if (defects.Length != 0)
                    {
                        var cnt = 0;
                        for (int i = 0; i < defects.Length; i++)
                        {
                            var start = contour[defects[i][0]];
                            var end   = contour[defects[i][1]];
                            var far   = contour[defects[i][2]];
                            var a     = Math.Sqrt(Math.Pow(end.X - start.X, 2) + Math.Pow(end.Y - start.Y, 2));
                            var b     = Math.Sqrt(Math.Pow(far.X - start.X, 2) + Math.Pow(far.Y - start.Y, 2));
                            var c     = Math.Sqrt(Math.Pow(end.X - far.X, 2) + Math.Pow(end.Y - far.Y, 2));
                            var angle = Math.Acos((b * b + c * c - a * a) / (2 * b * c));
                            if (angle <= Math.PI / 2.0)
                            {
                                cnt++;
                                Cv2.Circle(image, far, 4, new Scalar(0, 0, 255), -1);
                            }
                        }
                        if (cnt > 0)
                        {
                            cnt++;
                        }
                        image.PutText(cnt.ToString(), new OpenCvSharp.Point(0, 50), HersheyFonts.HersheySimplex, 1, new Scalar(255, 0, 0), 2, LineTypes.AntiAlias);
                    }
                    Cv2.ImShow("Detection", image);
                }
                catch (Exception) { }
                if (Cv2.WaitKey(1) == 113)
                {
                    break;
                }
            }
            cap.Release();
            Cv2.DestroyAllWindows();
        }
        public static int RecognizeFaces(string inputImagePath)
        {
            var srcImage = new Mat(inputImagePath);

            var grayImage = new Mat();

            Cv2.CvtColor(srcImage, grayImage, ColorConversionCodes.BGRA2GRAY);
            Cv2.EqualizeHist(grayImage, grayImage);
            var cascade       = new CascadeClassifier(@"..\..\..\..\OpenCVHavrylov\Data\haarcascade_frontalface_alt.xml");
            var nestedCascade = new CascadeClassifier(@"..\..\..\..\OpenCVHavrylov\Data\haarcascade_eye_tree_eyeglasses.xml");
            var faces         = cascade.DetectMultiScale(
                image: grayImage,
                scaleFactor: 1.1,
                minNeighbors: 2,
                flags: HaarDetectionType.DoRoughSearch | HaarDetectionType.ScaleImage,
                minSize: new OpenCvSharp.Size(30, 30)
                );

            var count = 1;

            foreach (var faceRect in faces)
            {
                var detectedFaceImage = new Mat(srcImage, faceRect);

                var color = Scalar.FromRgb(255, 0, 0);
                Cv2.Rectangle(srcImage, faceRect, color, 3);


                var detectedFaceGrayImage = new Mat();
                Cv2.CvtColor(detectedFaceImage, detectedFaceGrayImage, ColorConversionCodes.BGRA2GRAY);
                var nestedObjects = nestedCascade.DetectMultiScale(
                    image: detectedFaceGrayImage,
                    scaleFactor: 1.1,
                    minNeighbors: 2,
                    flags: HaarDetectionType.DoRoughSearch | HaarDetectionType.ScaleImage,
                    minSize: new OpenCvSharp.Size(30, 30)
                    );


                foreach (var nestedObject in nestedObjects)
                {
                    var center = new OpenCvSharp.Point
                    {
                        X = (int)(Math.Round(nestedObject.X + nestedObject.Width * 0.5, MidpointRounding.ToEven) + faceRect.Left),
                        Y = (int)(Math.Round(nestedObject.Y + nestedObject.Height * 0.5, MidpointRounding.ToEven) + faceRect.Top)
                    };
                    var radius = Math.Round((nestedObject.Width + nestedObject.Height) * 0.25, MidpointRounding.ToEven);
                    Cv2.Circle(srcImage, center, (int)radius, color, thickness: 3);
                }

                count++;
            }
            var resultName = inputImagePath == null ? $"{Directory.GetCurrentDirectory()}/result.png" : $"{inputImagePath}result.{inputImagePath.Split('.').Last()}";

            srcImage.ImWrite(resultName);
            srcImage.Dispose();
            return(faces.Length);
        }
Exemple #7
0
        private static void DrawContours(IReadOnlyList <Point[]> contours, IList <Point[]> contoursPoly, Mat drawing)
        {
            var center = new Point2f[contours.Count];
            var radius = new float[contours.Count];

            for (var i = 0; i < contours.Count; i++)
            {
                if (Cv2.ContourArea(contours[i]) >= 5000)
                {
                    contoursPoly[i] = Cv2.ApproxPolyDP(contours[i], 3, true);
                    Cv2.MinEnclosingCircle(contoursPoly[i], out center[i], out radius[i]);
                    var tempContour = contours[i];

                    var hulls  = new Point[1][];
                    var hullsI = new int[1][];
                    hulls[0]  = Cv2.ConvexHull(tempContour);
                    hullsI[0] = Cv2.ConvexHullIndices(tempContour);

                    Cv2.DrawContours(drawing, hulls, -1, Scalar.Gold, 2);
                    if (hullsI[0].Length > 0)
                    {
                        var defects = Cv2.ConvexityDefects(tempContour, hullsI[0]);
                        if (defects.Length > 0)
                        {
                            for (var j = 1; j < defects.Length; j++)
                            {
                                var startIdx = defects[j][0];
                                var ptStart  = tempContour[startIdx];
                                var farIdx   = defects[j][2];
                                var ptFar    = tempContour[farIdx];

                                if (GetDistance(ptStart, ptFar) > 1000 && ptStart.Y < center[i].Y && radius[i] >= 70)
                                {
                                    Cv2.Circle(drawing, ptStart, 10, Scalar.Yellow, 2);
                                    Cv2.Line(drawing, ptStart, ptFar, Scalar.Pink, 2);

                                    _numberOfFingers++;
                                }
                            }

                            if (radius[i] > 50)
                            {
                                Cv2.DrawContours(drawing, contoursPoly, i, Scalar.Red);
                                Cv2.Circle(drawing, center[i], (int)radius[i], Scalar.White, 2);
                                Cv2.Circle(drawing, center[i], 5, Scalar.Red, 2);

                                if (Program.ControlMode)
                                {
                                    _posX = (int)(4 * (center[i].X - 100));
                                    _posY = (int)(4 * (center[i].Y - 100));
                                    WinApiUtils.SetCursorPos(_posX, _posY);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #8
0
    private IEnumerator waitForOneSecondsAndThanDiscard(Vector3 currPosition) // Coroutine
    {
        yield return(new WaitForSeconds(1.0f));

        int x = (int)currPosition.x - 25;
        int y = 400 - (int)currPosition.z;

        Cv2.Circle(checkedArea, x, y, 2, Scalar.White, -4, LineTypes.Link8);
    }
        private OpenCvSharp.Point DetectCenterOfEye(Mat frame, Rect boundingBox)
        {
            // Gets location relative to frame
            // https://pysource.com/2019/01/04/eye-motion-tracking-opencv-with-python/
            //extract the eye region by coordinates.
            Mat Roi = frame.Clone(boundingBox);
            // convert to grayscale
            Mat grayRoi = new Mat();

            Cv2.CvtColor(Roi, grayRoi, ColorConversionCodes.BGR2GRAY);
            // get rid of surrounding noise to isolate pupil
            Mat grayRoi2 = new Mat();

            Cv2.GaussianBlur(grayRoi, grayRoi2, new OpenCvSharp.Size(7, 7), 0);

            // try get rid of more noise
            Mat threshold = new Mat();

            Cv2.Threshold(grayRoi2, threshold, 5, 255, ThresholdTypes.BinaryInv);    //Cv2.Thresh_Binary

            OpenCvSharp.Point[][] contours;
            HierarchyIndex[]      hi;
            Cv2.FindContours(threshold, out contours, out hi, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);

            // get the contour with the largest area, the pupil of eye.
            OpenCvSharp.Point CenterOfEye = new OpenCvSharp.Point();
            int EyeRadius = 0;

            for (int i = 0; i < contours.Length; i++)
            {
                OpenCvSharp.Point thisEye;
                int thisEyeRadius = 0;
                PupilCenter(contours, i, out thisEye, out thisEyeRadius);
                if (i == 0 || thisEyeRadius > EyeRadius)
                {
                    CenterOfEye = thisEye;
                    EyeRadius   = thisEyeRadius;
                }
            }
            if (EyeRadius > 0)
            {
                Scalar color = new Scalar(0, 0, 255);
                // locate center relative to the frame
                int FrameX            = CenterOfEye.X + EyeRadius + boundingBox.X;
                int FrameY            = CenterOfEye.Y + EyeRadius + boundingBox.Y;
                OpenCvSharp.Point ctr = new OpenCvSharp.Point(FrameX, FrameY);
                Cv2.Circle(frame, ctr, EyeRadius, color: color, thickness: 2);

                // draw vert line thru center of pupil
                Cv2.Line(img: frame, pt1: new OpenCvSharp.Point(FrameX, boundingBox.Y),
                         pt2: new OpenCvSharp.Point(FrameX, boundingBox.Y + boundingBox.Height), color, 1);
                //// draw horiz line thru center of pupil
                Cv2.Line(img: frame, pt1: new OpenCvSharp.Point(boundingBox.X, FrameY),
                         pt2: new OpenCvSharp.Point(boundingBox.X + boundingBox.Width, FrameY), color, 1);
            }
            return(CenterOfEye);
        }
Exemple #10
0
        //--------------------------------------------------------------------------------------------------------
        /// <summary>
        /// デバック時の処理       Process at debugging
        /// </summary>
        private void DetectDebug(int threadNo, Mat image, OpenCvSharp.Rect[] faces = null, DlibDotNet.Point[] points = null)
        {
            if (!image.IsDisposed)
            {
                int i = 0;
                switch (mode)
                {
                case DetectMode.OpenCV:
                    if (faces.Length != 0)
                    {
                        Cv2.Rectangle(image, new OpenCvSharp.Point(faces.First().X, faces.First().Y),
                                      new OpenCvSharp.Point(faces.First().X + faces.First().Width, faces.First().Y + faces.First().Height),
                                      new Scalar(0, 255, 255), lineType: LineTypes.AntiAlias);
                    }
                    break;

                case DetectMode.Dlib5:
                    foreach (var point in points)
                    {
                        Cv2.Circle(image, point.X, point.Y, 10, new Scalar(255, 255, 0), 5, LineTypes.AntiAlias);

                        Cv2.PutText(image, i.ToString(), new OpenCvSharp.Point(point.X, point.Y), HersheyFonts.HersheyPlain,
                                    0.6, new Scalar(0, 255, 0));
                        i++;
                    }
                    break;

                case DetectMode.Dlib68:
                    foreach (var point in points)
                    {
                        Cv2.Circle(image, point.X, point.Y, 3, new Scalar(255, 255, 0), 1, LineTypes.AntiAlias);

                        Cv2.PutText(image, i.ToString(), new OpenCvSharp.Point(point.X, point.Y), HersheyFonts.HersheyPlain,
                                    0.6, new Scalar(0, 255, 0));
                        i++;
                    }
                    break;
                }

                lock (lock_out_mat)
                {
                    if (out_mat[threadNo].IsEnabledDispose)
                    {
                        out_mat[threadNo].Dispose();
                    }

                    out_mat[threadNo] = new Mat();

                    lock (lock_imagebytes[threadNo])
                    {
                        out_mat[threadNo] = image.Clone();
                    }
                }

                last_mat = threadNo;
            }
        }
Exemple #11
0
        private static void DrawPose(NcnnDotNet.OpenCV.Mat bgr, List <KeyPoint> keyPoints)
        {
            using var image = bgr.Clone();

            // draw bone
            var jointParts = new[]
            {
                new[] { 0, 1 },
                new[] { 1, 3 },
                new[] { 0, 2 },
                new[] { 2, 4 },
                new[] { 5, 6 },
                new[] { 5, 7 },
                new[] { 7, 9 },
                new[] { 6, 8 },
                new[] { 8, 10 },
                new[] { 5, 11 },
                new[] { 6, 12 },
                new[] { 11, 12 },
                new[] { 11, 13 },
                new[] { 12, 14 },
                new[] { 13, 15 },
                new[] { 14, 16 }
            };

            for (var i = 0; i < 16; i++)
            {
                var p1 = keyPoints[jointParts[i][0]];
                var p2 = keyPoints[jointParts[i][1]];

                if (p1.Prob < 0.2f || p2.Prob < 0.2f)
                {
                    continue;
                }

                Cv2.Line(image, p1.P, p2.P, new Scalar <double>(255, 0, 0), 2);
            }

            // draw joint
            for (var i = 0; i < keyPoints.Count; i++)
            {
                var keyPoint = keyPoints[i];

                Console.WriteLine($"{keyPoint.P.X:f2} {keyPoint.P.Y:f2} = {keyPoint.Prob:f5}");

                if (keyPoint.Prob < 0.2f)
                {
                    continue;
                }

                Cv2.Circle(image, keyPoint.P, 3, new Scalar <double>(0, 255, 0), -1);
            }

            Cv2.ImShow("image", image);
            Cv2.WaitKey(0);
        }
Exemple #12
0
        public void 点数計算_debug(CvBlobs blobs, int[,] 正解座標, ref Mat color)
        {
            if (正解座標 != null)
            {
                int[,] 正解座標2 = (int[, ])正解座標.Clone();
                int 正解数   = 0;
                int 正解数   = 0;
                int 許容回数  = 5;
                int 未検出数  = 0;
                int score = 0;

                foreach (CvBlob item in blobs.Values)
                {
                    CvContourPolygon polygon = item.Contour.ConvertToPolygon();
                    Point2f          circleCenter;
                    float            circleRadius;

                    GetEnclosingCircle(polygon, out circleCenter, out circleRadius);
                    for (int j = 0; j < 正解座標2.Length / 2; j++)
                    {
                        if (正解座標2[j, 0] != 0 && (Math.Pow(circleCenter.X - 正解座標2[j, 0], 2) + Math.Pow(circleCenter.Y - 正解座標2[j, 1], 2) < circleRadius * circleRadius))
                        {//外接円内にあったら
                            Cv2.Circle(color, item.Centroid, (int)circleRadius, new Scalar(0, 0, 255), 2);
                            正解数++;
                            正解座標2[j, 0] = 正解座標2[j, 1] = 0;
                            j           = 正解座標2.Length;//ひとつ照合確定したら,このfor文を抜けて次のラベルの検査に移動
                        }
                    }
                }

                System.Diagnostics.Debug.WriteLine("未検出座標");
                for (int i = 0; i < 正解座標2.Length / 2; i++)
                {//検出されなかった座標が残る
                    if (正解座標2[i, 0] != 0)
                    {
                        System.Diagnostics.Debug.WriteLine(i + ":" + 正解座標2[i, 0] + "," + 正解座標2[i, 1]);
                        未検出数++;
                    }
                }

                正解数 = blobs.Count - 正解数;
                if (正解数 <= 許容回数)
                {
                    score = (int)((float)(正解数) * (10000.0f / (正解座標.Length / 2)));
                }
                else
                {
                    score = (int)((float)(正解数 - (正解数 - 許容回数)) * (10000.0f / (正解座標.Length / 2)));
                }

                Cv2.PutText(color, "score= " + score.ToString(), new Point(10, 120), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 0));
                Cv2.PutText(color, "unCorrect= " + 正解数.ToString(), new Point(10, 140), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 0));
                Cv2.PutText(color, "unFind= " + 未検出数.ToString(), new Point(10, 160), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 0));
            }
        }
Exemple #13
0
        //  Mat _dsp;
        //   Rect roi;
        void show()
        {
            Scalar      fg_color      = new Scalar(255, 0, 0);
            Scalar      bg_color      = new Scalar(0, 255, 0);
            Mat         scribbled_src = _src.Clone();
            const float alpha         = 0.7f;

            for (int y = 0; y < _gcut.Rows; y++)
            {
                for (int x = 0; x < _gcut.Cols; x++)
                {
                    if (_gcut.At <byte>(y, x) == (byte)GrabCutClasses.FGD)
                    {
                        Cv2.Circle(scribbled_src, new OpenCvSharp.Point(x, y), 2, fg_color, -1);
                    }
                    else if (_gcut.At <byte>(y, x) == (byte)GrabCutClasses.BGD)
                    {
                        Cv2.Circle(scribbled_src, new OpenCvSharp.Point(x, y), 2, bg_color, -1);
                    }
                    else if (_gcut.At <byte>(y, x) == (byte)GrabCutClasses.PR_BGD)
                    {
                        Vec3b pix = scribbled_src.At <Vec3b>(y, x);
                        pix[0] = (byte)(pix[0] * alpha + bg_color[0] * (1 - alpha));
                        pix[1] = (byte)(pix[1] * alpha + bg_color[1] * (1 - alpha));
                        pix[2] = (byte)(pix[2] * alpha + bg_color[2] * (1 - alpha));
                        scribbled_src.Set <Vec3b>(y, x, pix);
                    }
                    else if (_gcut.At <byte>(y, x) == (byte)GrabCutClasses.PR_FGD)
                    {
                        Vec3b pix = scribbled_src.At <Vec3b>(y, x);
                        pix[0] = (byte)(pix[0] * alpha + fg_color[0] * (1 - alpha));
                        pix[1] = (byte)(pix[1] * alpha + fg_color[1] * (1 - alpha));
                        pix[2] = (byte)(pix[2] * alpha + fg_color[2] * (1 - alpha));
                        scribbled_src.Set <Vec3b>(y, x, pix);
                    }
                }
            }



            pictureBox2.Image = scribbled_src.ToBitmap();

            Mat fg = getFG();


            pictureBox3.Image = fg.ToBitmap();


            Mat msk = getBinMask();

            Cv2.CvtColor(msk, msk, ColorConversionCodes.GRAY2BGR);


            pictureBox4.Image = msk.ToBitmap();
        }
Exemple #14
0
        /// <summary>
        /// この関数を実行すると,カメラから画像を取得し,グリッドマークを検出しその座標を返します.
        /// 実行時のレンズはx50対物であることを前提とします.
        /// </summary>
        /// <returns>グリッドマークを検出したピクセル座標。検出できなかった時は(-1,-1)が返される</returns>
        public Vector2 SearchGridMarkx50()
        {
            // レンズが50倍に設定されていない場合は例外を返すようにしたいがやり方が分からん(20140724)
            //if (parameterManager.Magnification != ParameterManager.) {
            //    throw new LensTypeException(ParameterManager.LensMagnificationOfGridMarkSearch);
            //}
            Camera c = Camera.GetInstance();

            byte[] b   = c.ArrayImage;
            Mat    mat = new Mat(440, 512, MatType.CV_8U, b);

            //mat.ImWrite(String.Format(@"c:\img\{0}_g.bmp", System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff")));
            Cv2.GaussianBlur(mat, mat, Cv.Size(5, 5), -1);
            Cv2.Threshold(mat, mat, 60, 255, ThresholdType.BinaryInv);
            //mat.ImWrite(String.Format(@"c:\img\{0}_t.bmp", System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff")));

            Moments mom = new Moments(mat);

            if (mom.M00 < 1000 * 255)
            {
                return(new Vector2(-1.0, -1.0));
            }

            double cx       = mom.M10 / mom.M00;
            double cy       = mom.M01 / mom.M00;
            Mat    innercir = Mat.Zeros(440, 512, MatType.CV_8UC1);

            Cv2.Circle(innercir, new Point(cx, cy), 30, new Scalar(255, 255, 255), 3);
            int innerpath = Cv2.CountNonZero(innercir);

            Cv2.BitwiseAnd(innercir, mat, innercir);
            int innersum = Cv2.CountNonZero(innercir);

            Mat outercir = Mat.Zeros(440, 512, MatType.CV_8UC1);

            Cv2.Circle(outercir, new Point(cx, cy), 200, new Scalar(255, 255, 255), 3);
            int outerpath = Cv2.CountNonZero(outercir);

            Cv2.BitwiseAnd(outercir, mat, outercir);
            int outersum = Cv2.CountNonZero(outercir);

            double innerratio = innersum * 1.0 / innerpath * 1.0;
            double outerratio = outersum * 1.0 / outerpath * 1.0;

            if (innerratio < 0.8)
            {
                return(new Vector2(-1.0, -1.0));
            }
            if (outerratio > 0.2)
            {
                return(new Vector2(-1.0, -1.0));
            }

            return(new Vector2(cx, cy));
        }
Exemple #15
0
    public void addNode(RRTNode node)
    {
        int x = (int)node.X() - 25;
        int y = 400 - (int)node.Z();

        Cv2.Circle(pic, x, y, 2, Scalar.Blue, -1, LineTypes.Link8);
        int fatherX = (int)node.Father().X() - 25;
        int fatherZ = 400 - (int)node.Father().Z();

        Cv2.Line(pic, x, y, fatherX, fatherZ, Scalar.Green);
    }
Exemple #16
0
 void drawVectorPoints(Mat image, List <Point> points, Scalar color, bool with_numbers)
 {
     for (int i = 0; i < points.Count; i++)
     {
         Cv2.Circle(image, points[i].X, points[i].Y, 15, color, 5, LineTypes.Link8);
         if (with_numbers)
         {
             Cv2.PutText(image, i.ToString(), points[i], HersheyFonts.HersheyTriplex, 2, color);
         }
     }
 }
Exemple #17
0
 public void DrawPoints(Mat frame)
 {
     if (Points == null)
     {
         return;
     }
     foreach (OpenCvSharp.Point pt in Points)
     {
         Cv2.Circle(img: frame, centerX: pt.X, centerY: pt.Y, radius: 2, color: color, thickness: 1);
     }
 }
        /// <summary>
        /// Camera calibration main function.
        /// </summary>
        /// <param name="cam_test">Camera beeing used.</param>
        /// <param name="calWindow">Display window.</param>
        public void CropLoop(VideoCapture cam_test, Window calWindow)
        {
            src = cam_test.RetrieveMat(); //Real image of the camera
            Cv2.Resize(src, src, new Size(frameWidth, frameHeight), 1, 1, InterpolationFlags.Cubic);

            if (point1.X != 0 && point2.X != 0)
            {
                Cv2.Line(src, point1, point2, new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);
            }
            if (point2.X != 0 && point3.X != 0)
            {
                Cv2.Line(src, point2, point3, new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);
            }
            if (point3.X != 0 && point4.X != 0)
            {
                Cv2.Line(src, point3, point4, new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);
            }
            if (point4.X != 0 && point1.X != 0)
            {
                Cv2.Line(src, point4, point1, new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);
            }

            if (point1.X != 0)
            {
                Cv2.Circle(src, point1, 2, new Scalar(250, 0, 0), 3, LineTypes.AntiAlias);
                Cv2.Circle(src, point1, 10, new Scalar(0, 0, 255), 1, LineTypes.AntiAlias);
            }
            if (point2.X != 0)
            {
                Cv2.Circle(src, point2, 2, new Scalar(250, 0, 0), 3, LineTypes.AntiAlias);
                Cv2.Circle(src, point2, 10, new Scalar(0, 255, 0), 1, LineTypes.AntiAlias);
            }
            if (point3.X != 0)
            {
                Cv2.Circle(src, point3, 2, new Scalar(250, 0, 0), 3, LineTypes.AntiAlias);
                Cv2.Circle(src, point3, 10, new Scalar(255, 255, 0), 1, LineTypes.AntiAlias);
            }
            if (point4.X != 0)
            {
                Cv2.Circle(src, point4, 2, new Scalar(250, 0, 0), 3, LineTypes.AntiAlias);
                Cv2.Circle(src, point4, 10, new Scalar(0, 255, 255), 1, LineTypes.AntiAlias);
                rectangleCreated = true;
            }

            if (point1.X != 0 && point2.X != 0 && point3.X != 0 && point4.X != 0)
            {
                rectangleCreated = true;
            }

            Cv2.ImShow(calWindow.Name, src);

            Cv2.WaitKey(1);
            src.Dispose();
        }
Exemple #19
0
        public Mat CircleImage(out int minX, out int minY, out int maxX, out int maxY)
        {
            minX = 18;
            maxX = 42;
            minY = 28;
            maxY = 52;
            Mat    result = new Mat(100, 100, MatType.CV_8UC1, new Scalar(0));
            Scalar color  = new Scalar(255);

            Cv2.Circle(result, new Point(30, 40), 12, color, 1);
            return(result);
        }
Exemple #20
0
        private void DetectHulls()
        {
            Point[] hulls_points       = Cv2.ConvexHull(HandArea, true);
            Rect    bounding_rectangle = Cv2.BoundingRect(hulls_points);
            Point   centerHand         = new Point(
                (bounding_rectangle.TopLeft.X + bounding_rectangle.BottomRight.X) / 2
                , (bounding_rectangle.TopLeft.X + bounding_rectangle.BottomRight.X) / 2
                );

            Cv2.Rectangle(TrackerVisualisation, bounding_rectangle, new Scalar(0, 255, 255), 2);
            Cv2.Circle(TrackerVisualisation, centerHand, 5, new Scalar(255, 0, 0));
        }
 private static void drawOptFlowMap(ref Mat flow, ref Mat cflowmap, double scale, int step, Scalar color)
 {
     for (int y = 0; y < cflowmap.Rows; y += step)
     {
         for (int x = 0; x < cflowmap.Cols; x += step)
         {
             Point2f fxy = flow.At <Point2f>(y, x) * scale;
             Cv2.Line(cflowmap, new Point(x, y), new Point(Math.Round(x + fxy.X), Math.Round(y + fxy.Y)), color);
             Cv2.Circle(cflowmap, new Point(x, y), 2, color, -1);
         }
     }
 }
Exemple #22
0
        public static void synthesizeFilterH(ref Mat inputOutput_H, Point center, int radius)
        {
            Point c2 = center, c3 = center, c4 = center;

            c2.Y = inputOutput_H.Rows - center.Y;
            c3.X = inputOutput_H.Cols - center.X;
            c4   = new Point(c3.X, c2.Y);
            Cv2.Circle(inputOutput_H, center, radius, new Scalar(0), -1, LineTypes.Link8);
            Cv2.Circle(inputOutput_H, c2, radius, new Scalar(0), -1, LineTypes.Link8);
            Cv2.Circle(inputOutput_H, c3, radius, new Scalar(0), -1, LineTypes.Link8);
            Cv2.Circle(inputOutput_H, c4, radius, new Scalar(0), -1, LineTypes.Link8);
        }
Exemple #23
0
        public Mat MinEnclosing(Mat img)
        {
            Mat    binary = new Mat();
            Mat    morp = new Mat();
            Mat    image = new Mat();
            Mat    dst = img.Clone();
            int    pointX = 0, pointY = 0;
            string text;

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

            OpenCvSharp.Point[][] contours;
            HierarchyIndex[]      hierarchy;

            Cv2.Threshold(img, binary, 230, 255, ThresholdTypes.Binary);
            Cv2.MorphologyEx(binary, morp, MorphTypes.Close, kernel, new OpenCvSharp.Point(-1, -1), 2);
            Cv2.BitwiseNot(morp, image);

            Cv2.FindContours(image, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxTC89KCOS);

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

                OpenCvSharp.Point[]   approx      = Cv2.ApproxPolyDP(contours[i], epsilon, true);
                OpenCvSharp.Point[][] draw_approx = new OpenCvSharp.Point[][] { approx };
                Cv2.DrawContours(dst, draw_approx, -1, new Scalar(255, 0, 0), 2, LineTypes.AntiAlias);

                Cv2.MinEnclosingCircle(contours[i], out Point2f center, out float radius);
                Cv2.Circle(dst, new OpenCvSharp.Point(center.X, center.Y), (int)radius, Scalar.Red, 2, LineTypes.AntiAlias);

                pointX += (int)center.X;
                pointY += (int)center.Y;

                for (int j = 0; j < approx.Length; j++)
                {
                    Cv2.Circle(dst, approx[j], 1, new Scalar(0, 0, 255), 3);
                }
            }

            if (contours.Length > 0)
            {
                pointX = pointX / contours.Length;
                pointY = pointY / contours.Length;

                text = pointX.ToString();
                text = text + ":" + pointY.ToString();
                Cv2.PutText(dst, text, new OpenCvSharp.Point(3300, 2700), HersheyFonts.HersheyPlain, 5, Scalar.White, 5);
            }

            return(dst);
        }
Exemple #24
0
        public static void cv_03()  // 图像翻转 绘字 圆 线 矩形
        {
            // 1.图像翻转
            Mat dst = new Mat();
            Mat src = new Mat(@"G:\\pics\\2.jpg", ImreadModes.Color);

            Cv2.Flip(src, dst, FlipMode.X);  // X 垂直翻转 Y 水平翻转 XY 水平垂直翻转
            Cv2.ImShow("src", src);
            Cv2.ImShow("dst", dst);
            Cv2.WaitKey();

            // 2.绘字
            Cv2.PutText(src,                        // 绘字的图像
                        "CuiRu",                    // 内容
                        new Point(30, 220),         //  字的左下角位置
                        HersheyFonts.HersheyDuplex, //  字体
                        8,                          //  字的大小
                        Scalar.Red);                //  字色
            Cv2.ImShow("put_text", src);
            Cv2.WaitKey();
            Cv2.ImWrite("G:\\pics\\save_pic\\s4.jpg", src);

            // 3.圆
            Cv2.Circle(src,                 // 目标图片
                       new Point(400, 350), // 圆心
                       180,                 // 半径
                       Scalar.PowderBlue,   //  线色
                       -1);                 // -1为实心 数值为线宽
            Cv2.ImShow("circle", src);
            Cv2.WaitKey();

            // 4.线
            Cv2.Line(src,                 // 目标图像
                     new Point(250, 150), // 线段起始点
                     new Point(250, 550), // 终点
                     Scalar.Peru,         // 线色
                     6);                  // 线宽
            Cv2.ImShow("src", src);
            Cv2.WaitKey();

            // 5.矩形
            Rect rect = new Rect(200, 200, 300, 180);

            Cv2.Rectangle(src,          // 目标图像
                          rect,         // 矩形位置
                          Scalar.Beige, // 线色
                          5);           // 线宽
            Cv2.ImShow("rect", src);
            Cv2.WaitKey();

            Cv2.ImWrite("G:\\pics\\save_pic\\s5.jpg", src);
        }
        public void Drawing_method()
        {
            using Mat OriginalImage = new Mat("../../opencv.png", ImreadModes.AnyColor);

            Cv2.Line(OriginalImage, new Point(10, 10), new Point(630, 10), Scalar.Blue);
            Cv2.Circle(OriginalImage, new Point(100, 100), 40, Scalar.Blue);
            Cv2.Rectangle(OriginalImage, new Rect(20, 20, 40, 40), Scalar.Red);
            Cv2.Ellipse(OriginalImage, new RotatedRect(new Point2f(120, 120), new Size2f(200, 100), 10), Scalar.Yellow);
            Cv2.PutText(OriginalImage, "OpenCVSharp", new Point(600, 600), HersheyFonts.Italic, 5, Scalar.Black);

            Cv2.ImShow("draw", OriginalImage);
            Cv2.WaitKey();
        }
Exemple #26
0
        public void DrawDLInVedio(String videoFile, String outputFile, String targetFile, String indexFile, int fileoffset, Scalar color)
        {
            VideoCapture capture = new VideoCapture(videoFile);
            Mat          image   = new Mat();

            OpenCvSharp.CPlusPlus.Size dsize = new OpenCvSharp.CPlusPlus.Size(capture.FrameWidth, capture.FrameHeight);
            VideoWriter   writer             = new VideoWriter(outputFile, FourCC.MJPG, fps, dsize, true);
            int           k          = 0;
            List <string> targetList = IOTools.ReadListFromTxt(targetFile);
            List <string> indexList  = IOTools.ReadListFromTxt(indexFile);
            int           t          = 0;
            String        status     = " ";

            while (capture.Read(image))
            {
                String[] ss = targetList[t + fileoffset].Split(' ');

                if (t < indexList.Count - 2 && k == int.Parse(indexList[t + 1]))
                {
                    t++;
                }
                Cv2.Circle(image, (int)double.Parse(ss[0]), (int)double.Parse(ss[1]), 10, color,
                           2
                           );
                // Cv2.PutText(image, status,
                //      new Point((int)double.Parse(ss[0]) - 5, (int)double.Parse(ss[1])), FontFace.Italic, 1, color, 3);
                if (t > 5)
                {
                    String[] ssPre = targetList[t + fileoffset - 5].Split(' ');
                    if (double.Parse(ss[1]) - double.Parse(ssPre[1]) < -25)
                    {
                        status = "Pick Up";
                    }

                    else if (double.Parse(ss[1]) - double.Parse(ssPre[1]) > 30)
                    {
                        status = "Put Down";
                    }
                    else if (Math.Abs(double.Parse(ss[1]) - double.Parse(targetList[fileoffset].Split(' ')[1])) < 10)
                    {
                        status = " ";
                    }
                }
                //if (double.Parse(ss[1]) < 420)

                writer.Write(image);
                k++;
            }
            ;
            writer.Release();
        }
Exemple #27
0
        public MatchImageResult MatchImage(Mat source, Mat search, double threshold)
        {
            if (source.Width < 10 || source.Height < 10)
            {
                return(new MatchImageResult()
                {
                    Success = false,
                });
            }
            if (source.Width < search.Width || source.Height < search.Height)
            {
                return(new MatchImageResult()
                {
                    Success = false,
                });
            }
            var res = new Mat();

            //var c1 = source.Channels();
            //var c2 = search.Channels();
            DisplayImage("source", source);
            DisplayImage("search", search);
            Cv2.MatchTemplate(source, search, res, TemplateMatchModes.CCoeffNormed);
            double  minVal, maxVal;
            CvPoint minLoc, maxLoc;

            Cv2.MinMaxLoc(res, out minVal, out maxVal, out minLoc, out maxLoc);
            if (maxVal < threshold)
            {
                //LogTools.GetInstance().Info($"maxVal = {maxVal}, threshold = {threshold}");
                return(new MatchImageResult()
                {
                    Success = false,
                    Maxval = maxVal,
                });
            }
            Cv2.Circle(source, maxLoc.X + search.Width / 2, maxLoc.Y + search.Height / 2, 25, Scalar.Red);
            DisplayImage("ImageMatch", source);
            return(new MatchImageResult()
            {
                Success = true,
                MatchedRect = new RECT()
                {
                    x1 = maxLoc.X,
                    y1 = maxLoc.Y,
                    x2 = maxLoc.X + search.Width,
                    y2 = maxLoc.Y + search.Height,
                },
                Maxval = maxVal,
            });
        }
Exemple #28
0
        public static void Draw(Mat img, Point center)
        {
            Point centerImg = new Point(img.Cols / 2, img.Rows / 2);

            Point h_start = centerImg + new Point(center.X - 50, center.Y);
            Point h_stop  = centerImg + new Point(center.X + 50, center.Y);
            Point v_start = centerImg + new Point(center.X, center.Y - 50);
            Point v_stop  = centerImg + new Point(center.X, center.Y + 50);
            Point c_start = centerImg + center;

            Cv2.Line(img, h_start, h_stop, new Scalar(0, 0, 255));
            Cv2.Line(img, v_start, v_stop, new Scalar(0, 0, 255));
            Cv2.Circle(img, c_start, 32, new Scalar(0, 255, 0));
        }
Exemple #29
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Mat src = new Mat(@"./1.jpg");

            src = src.Resize(new Size(src.Width / 2, src.Height / 2));
            Cv2.ImShow("src", src);

            var binary = BinarizationMat(src);

            Cv2.ImShow("bin", binary);
            var fScreenMat = FindContoursMat(binary, src);

            fScreenMat = new Mat(fScreenMat,
                                 new Rect((int)(fScreenMat.Width * 0.025), (int)(fScreenMat.Height * 0.05),
                                          fScreenMat.Width - (int)(fScreenMat.Width * 0.05), fScreenMat.Height - (int)(fScreenMat.Height * 0.1)));
            Cv2.ImShow("Screen", fScreenMat);

            var m2 = SaturationGain(fScreenMat, 255);

            Cv2.ImShow("SaturationGain", m2);
            Cv2.CvtColor(m2, m2, ColorConversionCodes.BGR2GRAY);
            Mat b2 = m2.Threshold(100, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);

            Cv2.FindContours(b2, out var contours, out var hierarchy, RetrievalModes.Tree,
                             ContourApproximationModes.ApproxSimple);
            Cv2.ImShow("b2", b2);
            var dst = fScreenMat;

            foreach (var itemPoint in contours)
            {
                Console.WriteLine("_________________");
                var epsilon = 0.01 * Cv2.ArcLength(itemPoint, true);
                var approx  = Cv2.ApproxPolyDP(itemPoint, epsilon, true);
                Console.WriteLine("Approx Angle:" + approx.Length);
                foreach (var item in approx)
                {
                    Console.WriteLine(item.X + " " + item.Y);
                    Cv2.Circle(dst, item.X, item.Y, 5, new Scalar(255, 0, 0), 2, LineTypes.AntiAlias);
                    Cv2.ImShow("dst", dst);
                    //Cv2.WaitKey();
                }
                //foreach (var item in itemPoint) Console.WriteLine(item.X + " " + item.Y);
                //Console.WriteLine("_________________");
            }
            Cv2.DrawContours(dst, contours, -1, Scalar.Green, 3);
            Cv2.ImShow("dst", dst);
            Cv2.WaitKey();
        }
Exemple #30
0
        protected override Mat MatChanged(Mat Mat)
        {
            Mat cvt = new Mat();

            Cv2.CvtColor(Mat, cvt, ColorConversionCodes.BGR2GRAY);
            Cv2.Threshold(cvt, cvt, Threshold, 255, ThresholdTypes.Tozero);
            Cv2.Threshold(cvt, cvt, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
            Point2f[] corners = Cv2.GoodFeaturesToTrack(cvt, 400, QualityLevel, 20, null, BlockSize, true, 10);
            cvt.Dispose();
            foreach (Point2f p in corners)
            {
                Cv2.Circle(Mat, p, 5, Scalar.Red);
            }
            return(Mat);
        }