Exemple #1
0
        //opencv_point -> W_Point로 바꾸기
        private W_Point OpenCVPoint2W_Point(OpenCvSharp.Point pt)
        {
            W_Point result = new W_Point();

            result = new W_Point(pt.X, pt.Y);

            return(result);
        }
        public static double distance(OpenCvSharp.Point pt1, OpenCvSharp.Point pt2)
        {
            double x      = Math.Pow(pt2.X - pt1.X, 2);
            double y      = Math.Pow(pt2.Y - pt1.Y, 2);
            double length = Math.Sqrt(x + y);

            //Console.WriteLine(length);
            return(length);
        }
        private RelativePoint GetRelativePoint(MouseEventArgs e)
        {
            var viewPoint  = e.GetPosition(frameHolder);
            var imagePoint = new OpenCvSharp.Point(
                frameHolder.Source.Width * (viewPoint.X / frameHolder.ActualWidth),
                frameHolder.Source.Height * (viewPoint.Y / frameHolder.ActualHeight));

            return(RelativePoint.FromFrame(imagePoint, frameHolder.Source.Width, frameHolder.Source.Height));
        }
Exemple #4
0
        internal bool IsHealthPixel(Point p)
        {
            Color color = IntToColor(au3.PixelGetColor(p.X, p.Y));

            if (color.R > 160 && color.G < 80 && color.B < 80)
            {
                return(true);
            }
            return(false);
        }
Exemple #5
0
        internal bool FindPixelColorPos(int v, int x1, int y1, int x2, int y2, out Point p)
        {
            object search = au3.PixelSearch(window.X + x1, window.Y + y1, window.X + x2, window.Y + y2, v);

            if (search.ToString() != "0")
            {
                object[] pixelCoord = (object[])search;
                // au3.MouseMove((int)pixelCoord[0], (int)pixelCoord[1], 500);
                p = new Point((int)pixelCoord[0] - window.X, (int)pixelCoord[1] - window.Y);
                return(true);
            }
            p = new Point(0, 0);
            return(false);
        }
Exemple #6
0
        public static bool isInRegion(OpenCvSharp.Point checkPoint, OpenCvSharp.Point regionP1, OpenCvSharp.Point regionP2)
        {
            int minX = regionP1.X > regionP2.X ? regionP2.X : regionP1.X,
                maxX = regionP1.X <regionP2.X?regionP2.X : regionP1.X,
                                   minY = regionP1.Y> regionP2.Y ? regionP2.Y : regionP1.Y,
                maxY = regionP1.Y < regionP2.Y ? regionP2.Y : regionP1.Y;

            if (minX <= checkPoint.X && checkPoint.X <= maxX && minY <= checkPoint.Y && checkPoint.Y <= maxY)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static OpenCvSharp.Point[] getCCWList(OpenCvSharp.Point[] pp)
        {
            OpenCvSharp.Point[] alignedList = new OpenCvSharp.Point[4];
            OpenCvSharp.Point   center      = new OpenCvSharp.Point();
            for (int i = 0; i < pp.Length; i++)
            {
                center.X += pp[i].X;
                center.Y += pp[i].Y;
            }
            center.X = center.X / pp.Length;
            center.Y = center.Y / pp.Length;


            for (int i = 0; i < pp.Length; i++)
            {
                // left top
                if (pp[i].X < center.X && pp[i].Y < center.Y)
                {
                    alignedList[0] = pp[i];
                }
                // right top
                if (pp[i].X > center.X && pp[i].Y < center.Y)
                {
                    alignedList[1] = pp[i];
                }
                // right  bottom
                if (pp[i].X > center.X && pp[i].Y > center.Y)
                {
                    alignedList[2] = pp[i];
                }
                // left bottom
                if (pp[i].X <center.X && pp[i].Y> center.Y)
                {
                    alignedList[3] = pp[i];
                }
            }

            return(alignedList);
        }
 public static PythonTuple ToTuple(this OpenCvSharp.Point point)
 {
     return(new PythonTuple(new[] { point.X, point.Y }));
 }
Exemple #9
0
 internal int GetPixelColor(Point p)
 {
     return(au3.PixelGetColor(p.X + window.X, p.Y + window.Y));
 }
Exemple #10
0
        private void LocalizeVideoButton_Click(object sender, EventArgs e)
        {
            var widthHeightInputDialog = new VideoLocalizationDialog();

            widthHeightInputDialog.ShowDialog();

            var mapWidth  = widthHeightInputDialog.MapWidth;
            var mapHeight = widthHeightInputDialog.MapHeight;

            var ofd    = new OpenFileDialog();
            var result = ofd.ShowDialog();

            int pixelsPerGrid = 100;

            float[]         directionX = { 10, 7.07f, 0, -7.07f, -10, -7.07f, 0, 7.07f };
            float[]         directionY = { 0, -7.07f, -10, -7.07f, 0, 7.07f, 10, 7.07f };
            OpenCvSharp.Mat gridMap    = new OpenCvSharp.Mat(mapHeight * pixelsPerGrid, mapWidth * pixelsPerGrid, OpenCvSharp.MatType.CV_8UC3, new OpenCvSharp.Scalar(255, 255, 255));

            // Preprocessing
            for (int rowIndex = 0; rowIndex < mapHeight; ++rowIndex)
            {
                var pt1 = new OpenCvSharp.Point(0, rowIndex * pixelsPerGrid);
                var pt2 = new OpenCvSharp.Point(pixelsPerGrid * mapWidth, rowIndex * pixelsPerGrid);

                gridMap.Line(pt1, pt2, new OpenCvSharp.Scalar(0, 0, 0));
            }
            for (int colIndex = 0; colIndex < mapWidth; ++colIndex)
            {
                var pt1 = new OpenCvSharp.Point(colIndex * pixelsPerGrid, 0);
                var pt2 = new OpenCvSharp.Point(colIndex * pixelsPerGrid, pixelsPerGrid * mapHeight);

                gridMap.Line(pt1, pt2, new OpenCvSharp.Scalar(0, 0, 0));
            }

            Dictionary <Place, int> placeVote = new Dictionary <Place, int>();

            if (result == DialogResult.OK)
            {
                var videoFileName = ofd.FileNames[0];
                if (videoFileName.EndsWith(".mov") || videoFileName.EndsWith("MOV"))
                {
                    var vc = OpenCvSharp.VideoCapture.FromFile(videoFileName);
                    Dictionary <Tuple <int, int, int>, int> poseRecords = new Dictionary <Tuple <int, int, int>, int>();
                    Tuple <int, int, int> mostLikelyPose = null;
                    Place mostLikelyPlace      = null;
                    float mostLikelySimilarity = 0.0f;
                    int   nIterations          = 0;
                    Place localizedPlace       = null;

                    while (vc.IsOpened())
                    {
                        OpenCvSharp.Mat frame = new OpenCvSharp.Mat();
                        vc.Read(frame);

                        if (frame.Width == 0 || frame.Height == 0)
                        {
                            vc.Release();
                            break;
                        }

                        OpenCvSharp.Cv2.CvtColor(frame, frame, OpenCvSharp.ColorConversionCodes.BGR2GRAY);
                        OpenCvSharp.Cv2.Transpose(frame, frame);
                        OpenCvSharp.Cv2.Flip(frame, frame, OpenCvSharp.FlipMode.Y);

                        OpenCvSharp.Cv2.Resize(frame, frame, new OpenCvSharp.Size(frame.Width / 2, frame.Height / 2));
                        OpenCvSharp.Cv2.ImShow("Videos", frame);


                        if (nIterations < 15)
                        {
                            globalDB.GetNearestPlaceAndPose(frame, out mostLikelyPlace, out mostLikelyPose, out mostLikelySimilarity);
                            if (!placeVote.ContainsKey(mostLikelyPlace))
                            {
                                placeVote.Add(mostLikelyPlace, 0);
                            }
                            placeVote[mostLikelyPlace]++;
                        }
                        else if (nIterations == 15)
                        {
                            int currentMaxVote = 0;

                            foreach (var eachPlace in placeVote.Keys)
                            {
                                if (currentMaxVote <= placeVote[eachPlace])
                                {
                                    currentMaxVote = placeVote[eachPlace];
                                    localizedPlace = eachPlace;
                                }
                            }
                        }
                        else
                        {
                            globalDB.GetNearestPoseGivenPlaceName(frame, mostLikelyPlace.PlaceName, out mostLikelyPose, out mostLikelySimilarity);
                        }

                        if (mostLikelySimilarity < 0.20)
                        {
                            Console.WriteLine("Localization failed");
                        }
                        else
                        {
                            Console.WriteLine("(x, y, theta) = ({0}, {1}, {2}), similarity=> {3}", mostLikelyPose.Item1, mostLikelyPose.Item2, mostLikelyPose.Item3, mostLikelySimilarity);
                            if (poseRecords.ContainsKey(mostLikelyPose) == false)
                            {
                                poseRecords[mostLikelyPose] = 0;
                            }
                            poseRecords[mostLikelyPose]++;

                            if (poseRecords[mostLikelyPose] >= 5)
                            {
                                // For visualization code
                                //  => (pixelsPerGrid / 2) + pixelsPerGrid * (mostLikelyPose.Item1)
                                var arrowStartingPoint = new OpenCvSharp.Point((pixelsPerGrid / 2) + pixelsPerGrid * (mostLikelyPose.Item1), (pixelsPerGrid / 2) + pixelsPerGrid * (mostLikelyPose.Item2));
                                var arrowEndPoint      = new OpenCvSharp.Point(arrowStartingPoint.X + directionX[mostLikelyPose.Item3 - 1] * 2, arrowStartingPoint.Y + directionY[mostLikelyPose.Item3 - 1] * 2);

                                OpenCvSharp.Cv2.ArrowedLine(gridMap, arrowStartingPoint, arrowEndPoint, new OpenCvSharp.Scalar(0, 0, 255), 2);
                                OpenCvSharp.Cv2.ImShow("Grid Map", gridMap);
                            }

                            nIterations++;
                        }
                        var key = OpenCvSharp.Cv2.WaitKey(1);
                        if ((char)key == 27) // ESC
                        {
                            break;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Your video file format is invalid");
                }
            }
            MessageBox.Show("Video Localization Done");
            OpenCvSharp.Cv2.DestroyAllWindows();
        }
Exemple #11
0
 public Vector2(OpenCvSharp.Point point) : this(point.X, point.Y)
 {
 }
 private void AddCrossHandler()
 {
     OpenCvSharp.Point center = new OpenCvSharp.Point(1000 * CrossPosX / Cam["X um / px"], 1000 * CrossPosY / Cam["Y um / px"]);
     Crosshair.Draw(chessboardImage.ImageMat, center);
     ImageData = ImageTypeConverter.Convert(chessboardImage.ImageMat.ToBitmap());
 }