public Tracker() { InitializeComponent(); //UI label_RedBG.SendToBack(); label_BlueBG.SendToBack(); label_RedBG.Controls.Add(label_CarA); label_RedBG.Controls.Add(labelAScore); label_BlueBG.Controls.Add(label_CarB); int newX = label_CarB.Location.X - label_BlueBG.Location.X; int newY = label_CarB.Location.Y - label_BlueBG.Location.Y; label_CarB.Location = new System.Drawing.Point(newX, newY); label_BlueBG.Controls.Add(labelBScore); newX = labelBScore.Location.X - label_BlueBG.Location.X; newY = labelBScore.Location.Y - label_BlueBG.Location.Y; labelBScore.Location = new System.Drawing.Point(newX, newY); label_GameCount.Text = "上半场"; InitialCaiServer(); MessageBox.Show("TCP IP is " + server.getUsedIP().ToString() + " port is " + server.getPort().ToString()); udp = new CaiNetwork.CaiUDP(); MessageBox.Show("UDP IP is " + udp.broadcastIpEndPoint.Address.ToString() + " port is " + udp.broadcastIpEndPoint.Port.ToString()); // Init flags = new MyFlags(); flags.Init(); flags.Start(); ptsShowCorners = new Point2f[4]; cc = new CoordinateConverter(flags); localiser = new Localiser(); capture = new VideoCapture(); // threadCamera = new Thread(CameraReading); capture.Open(0); timeCamNow = DateTime.Now; timeCamPrev = timeCamNow; car1 = new Point2i(); car2 = new Point2i(); buttonStart.Enabled = true; buttonPause.Enabled = false; button_AReset.Enabled = false; button_BReset.Enabled = false; Game.LoadMap(); game = new Game(); if (capture.IsOpened()) { capture.FrameWidth = flags.cameraSize.Width; capture.FrameHeight = flags.cameraSize.Height; capture.ConvertRgb = true; timer100ms.Interval = 75; timer100ms.Start(); //Cv2.NamedWindow("binary"); } }
public void PassengersFilter(MyFlags flags) { if (!flags.calibrated) { return; } for (int i = 0; i < flags.currPassengerNum; ++i) { Point2f[] res = LogicToCamera(new Point2f[] { flags.posPassengerStart[i], flags.posPassengerEnd[i] }); flags.posPassengerStart[i] = res[0]; flags.posPassengerEnd[i] = res[1]; } }
public SetWindow(ref MyFlags flags, ref Game game) { InitializeComponent(); _flags = flags; _game = game; nudHue1L.Value = flags.configs.hue1Lower; nudHue1H.Value = flags.configs.hue1Upper; nudHue2L.Value = flags.configs.hue2Lower; nudHue2H.Value = flags.configs.hue2Upper; nudSat1L.Value = flags.configs.saturation1Lower; nudSat2L.Value = flags.configs.saturation2Lower; nudValueL.Value = flags.configs.valueLower; nudAreaL.Value = flags.configs.areaLower; checkBox_DebugMode.Checked = game.DebugMode; }
public Tracker() { InitializeComponent(); InitialCaiServer(); MessageBox.Show("TCP IP is " + server.getUsedIP().ToString() + " port is " + server.getPort().ToString()); udp = new CaiNetwork.CaiUDP(); MessageBox.Show("UDP IP is " + udp.broadcastIpEndPoint.Address.ToString() + " port is " + udp.broadcastIpEndPoint.Port.ToString()); tbsPoint = new TextBox[] { tbPoint1, tbPoint2, tbPoint3, tbPoint4 }; // Init flags = new MyFlags(); flags.Init(); flags.Start(); ptsShowCorners = new Point2f[4]; cc = new CoordinateConverter(flags); localiser = new Localiser(); capture = new VideoCapture(); // threadCamera = new Thread(CameraReading); /****************************************** 修改了此处,调用电脑自带摄像头 ******************************************/ capture.Open(0); //capture.Open(1); timeCamNow = DateTime.Now; timeCamPrev = timeCamNow; car1 = new Point2i(); car2 = new Point2i(); buttonStart.Enabled = true; buttonPause.Enabled = false; button_reset.Enabled = false; Game.LoadMap(); game = new Game(); if (capture.IsOpened()) { capture.FrameWidth = flags.cameraSize.Width; capture.FrameHeight = flags.cameraSize.Height; capture.ConvertRgb = true; timer100ms.Interval = 75; timer100ms.Start(); //Cv2.NamedWindow("binary"); } portCB.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames()); myID = 'A'; }
public CoordinateConverter(MyFlags myFlags) { camCorners = new Point2f[4]; logicCorners = new Point2f[4]; showCorners = new Point2f[4]; cam2logic = new Mat(); show2cam = new Mat(); logic2show = new Mat(); show2logic = new Mat(); cam2show = new Mat(); logic2cam = new Mat(); logicCorners[0].X = 0; logicCorners[0].Y = 0; logicCorners[1].X = myFlags.logicSize.Width; logicCorners[1].Y = 0; logicCorners[2].X = 0; logicCorners[2].Y = myFlags.logicSize.Height; logicCorners[3].X = myFlags.logicSize.Width; logicCorners[3].Y = myFlags.logicSize.Height; showCorners[0].X = 0; showCorners[0].Y = 0; showCorners[1].X = myFlags.showSize.Width; showCorners[1].Y = 0; showCorners[2].X = 0; showCorners[2].Y = myFlags.showSize.Height; showCorners[3].X = myFlags.showSize.Width; showCorners[3].Y = myFlags.showSize.Height; camCorners[0].X = 0; camCorners[0].Y = 0; camCorners[1].X = myFlags.cameraSize.Width; camCorners[1].Y = 0; camCorners[2].X = 0; camCorners[2].Y = myFlags.cameraSize.Height; camCorners[3].X = myFlags.cameraSize.Width; camCorners[3].Y = myFlags.cameraSize.Height; show2cam = Cv2.GetPerspectiveTransform(showCorners, camCorners); cam2show = Cv2.GetPerspectiveTransform(camCorners, showCorners); }
public void UpdateCorners(Point2f[] corners, MyFlags myFlags) { if (corners == null) { return; } if (corners.Length != 4) { return; } else { showCorners = corners; } logic2show = Cv2.GetPerspectiveTransform(logicCorners, showCorners); show2logic = Cv2.GetPerspectiveTransform(showCorners, logicCorners); camCorners = Cv2.PerspectiveTransform(showCorners, show2cam); cam2logic = Cv2.GetPerspectiveTransform(camCorners, logicCorners); logic2cam = Cv2.GetPerspectiveTransform(logicCorners, camCorners); myFlags.calibrated = true; }
public void Locate(Mat mat, MyFlags localiseFlags) { if (mat == null || mat.Empty()) { return; } if (localiseFlags == null) { return; } using (Mat hsv = new Mat()) using (Mat car1 = new Mat()) using (Mat car2 = new Mat()) using (Mat merged = new Mat()) using (Mat black = new Mat(mat.Size(), MatType.CV_8UC1)) { Cv2.CvtColor(mat, hsv, ColorConversionCodes.RGB2HSV); MyFlags.LocConfigs configs = localiseFlags.configs; Cv2.InRange(hsv, new Scalar(configs.hue1Lower, configs.saturation1Lower, configs.valueLower), new Scalar(configs.hue1Upper, 255, 255), car1); Cv2.InRange(hsv, new Scalar(configs.hue2Lower, configs.saturation2Lower, configs.valueLower), new Scalar(configs.hue2Upper, 255, 255), car2); Point2i[][] contours1, contours2; contours1 = Cv2.FindContoursAsArray(car1, RetrievalModes.External, ContourApproximationModes.ApproxSimple); contours2 = Cv2.FindContoursAsArray(car2, RetrievalModes.External, ContourApproximationModes.ApproxSimple); foreach (Point2i[] c1 in contours1) { Point2i centre = new Point2i(); Moments moments = Cv2.Moments(c1); centre.X = (int)(moments.M10 / moments.M00); centre.Y = (int)(moments.M01 / moments.M00); double area = moments.M00; if (area < configs.areaLower) { continue; } centres1.Add(centre); } foreach (Point2i[] c2 in contours2) { Point2i centre = new Point2f(); Moments moments = Cv2.Moments(c2); centre.X = (int)(moments.M10 / moments.M00); centre.Y = (int)(moments.M01 / moments.M00); double area = moments.M00; if (area < configs.areaLower) { continue; } centres2.Add(centre); } foreach (Point2i c1 in centres1) { Cv2.Circle(mat, c1, 10, new Scalar(0x1b, 0xff, 0xa7), -1); } foreach (Point2i c2 in centres2) { Cv2.Circle(mat, c2, 10, new Scalar(0x00, 0x98, 0xff), -1); } if (localiseFlags.gameState != GameState.Unstart) { for (int i = 0; i < localiseFlags.currPassengerNum; ++i) { int x1 = localiseFlags.posPassengerEnd[i].X; int y1 = localiseFlags.posPassengerEnd[i].Y; int x2 = localiseFlags.posPassengerStart[i].X; int y2 = localiseFlags.posPassengerStart[i].Y; int x10 = x1 - 8; int y10 = y1 - 8; int x20 = x2 - 8; int y20 = y2 - 8; Rect rectDest = new Rect(x10, y10, 16, 16); Rect rectSrc = new Rect(x20, y20, 16, 16); switch (localiseFlags.passengerState[i]) { case Camp.CampA: Cv2.Rectangle(mat, rectDest, new Scalar(0x1b, 0xff, 0xa7), -1); break; case Camp.CampB: Cv2.Rectangle(mat, rectDest, new Scalar(0x00, 0x98, 0xff), -1); break; case Camp.None: // Cv2.Circle(mat, localiseFlags.posPassengerStart[i], 5, new Scalar(0xd8, 0x93, 0xce), -1); if (i != 4) { Cv2.Rectangle(mat, rectSrc, new Scalar(0xf3, 0x96, 0x21), -1); } else { Cv2.Rectangle(mat, rectSrc, new Scalar(0x58, 0xee, 0xff), -1); } break; default: break; } } } Cv2.Merge(new Mat[] { car1, car2, black }, merged); Cv2.ImShow("binary", merged); } }
public void Locate(Mat mat, MyFlags localiseFlags) { if (mat == null || mat.Empty()) { return; } if (localiseFlags == null) { return; } { /* * using (Mat hsv = new Mat()) * using (Mat car1 = new Mat()) * using (Mat car2 = new Mat()) * //using (Mat merged = new Mat()) * using (Mat black = new Mat(mat.Size(), MatType.CV_8UC1)) * { * Cv2.CvtColor(mat, hsv, ColorConversionCodes.RGB2HSV); * MyFlags.LocConfigs configs = localiseFlags.configs; * Cv2.InRange(hsv, * new Scalar(configs.hue1Lower, configs.saturation1Lower, configs.valueLower), * new Scalar(configs.hue1Upper, 255, 255), * car1); * Cv2.InRange(hsv, * new Scalar(configs.hue2Lower, configs.saturation2Lower, configs.valueLower), * new Scalar(configs.hue2Upper, 255, 255), * car2); * * Point2i[][] contours1, contours2; * * contours1 = Cv2.FindContoursAsArray(car1, RetrievalModes.External, ContourApproximationModes.ApproxSimple); * contours2 = Cv2.FindContoursAsArray(car2, RetrievalModes.External, ContourApproximationModes.ApproxSimple); * * foreach (Point2i[] c1 in contours1) * { * Point2i centre = new Point2i(); * Moments moments = Cv2.Moments(c1); * centre.X = (int)(moments.M10 / moments.M00); * centre.Y = (int)(moments.M01 / moments.M00); * double area = moments.M00; * if (area < configs.areaLower) continue; * centres1.Add(centre); * } * foreach (Point2i[] c2 in contours2) * { * Point2i centre = new Point2f(); * Moments moments = Cv2.Moments(c2); * centre.X = (int)(moments.M10 / moments.M00); * centre.Y = (int)(moments.M01 / moments.M00); * double area = moments.M00; * if (area < configs.areaLower) continue; * centres2.Add(centre); * } * * foreach (Point2i c1 in centres1) Cv2.Circle(mat, c1, 10, new Scalar(0x1b, 0xff, 0xa7), -1); * foreach (Point2i c2 in centres2) Cv2.Circle(mat, c2, 10, new Scalar(0x00, 0x98, 0xff), -1); */ Cv2.Circle(mat, localiseFlags.posCarA, 4, new Scalar(0x1b, 0xff, 0xa7), -1); // 画车 Cv2.Circle(mat, localiseFlags.posCarB, 4, new Scalar(0x00, 0x98, 0xff), -1); int x_destination = localiseFlags.nextDestination.X - 4; int y_destination = localiseFlags.nextDestination.Y - 4; Rect rect_destination = new Rect(x_destination, y_destination, 8, 8); Cv2.Rectangle(mat, rect_destination, new Scalar(0x58, 0xee, 0xff), -1); // next target point int x_predCurPosition = localiseFlags.predCurPosition.X - 4; int y_predCurPosition = localiseFlags.predCurPosition.Y - 4; Rect rect_pred = new Rect(x_predCurPosition, y_predCurPosition, 8, 8); Cv2.Rectangle(mat, rect_pred, new Scalar(0x58, 0xee, 0xff), -1); // pred current point Cv2.Line(mat, localiseFlags.predCurPosition, localiseFlags.nextDestination, new Scalar(100, 105, 200), 2); if (localiseFlags.gameState != GameState.Unstart) { for (int i = 0; i < localiseFlags.currPassengerNum; ++i) { int x1 = localiseFlags.posPassengerEnd[i].X; int y1 = localiseFlags.posPassengerEnd[i].Y; int x2 = localiseFlags.posPassengerStart[i].X; int y2 = localiseFlags.posPassengerStart[i].Y; int x10 = x1 - 4; int y10 = y1 - 4; int x20 = x2 - 4; int y20 = y2 - 4; Rect rectDest = new Rect(x10, y10, 8, 8); Rect rectSrc = new Rect(x20, y20, 8, 8); switch (localiseFlags.passengerState[i]) { case Camp.CampA: Cv2.Rectangle(mat, rectDest, new Scalar(0x1b, 0xff, 0xa7), -1); Cv2.Line(mat, localiseFlags.posCarA, localiseFlags.posPassengerEnd[i], new Scalar(0x1b, 0xff, 0xa7), 1); break; case Camp.CampB: Cv2.Rectangle(mat, rectDest, new Scalar(0x00, 0x98, 0xff), -1); Cv2.Line(mat, localiseFlags.posCarB, localiseFlags.posPassengerEnd[i], new Scalar(0x00, 0x98, 0xff), 1); break; case Camp.None: // Cv2.Circle(mat, localiseFlags.posPassengerStart[i], 5, new Scalar(0xd8, 0x93, 0xce), -1); Cv2.Rectangle(mat, rectSrc, new Scalar(0xf3, 0x96, 0x21), -1); Cv2.Rectangle(mat, rectDest, new Scalar(0x58, 0xee, 0xff), -1); Cv2.Line(mat, localiseFlags.posPassengerStart[i], localiseFlags.posPassengerEnd[i], new Scalar(0xf3, 0x96, 0x21), 1); break; default: break; } } } //Cv2.Merge(new Mat[] { car1, car2, black }, merged); //Cv2.ImShow("binary", merged); } }