const int restRingWidth = 160; // to jest suma czyli 2x80 #endregion pierscienie public static ProcessFrameResult ProcessFrame(AditionaCapturelData acd) { PointF[] srcVertices; ProcessFrameResult result = new ProcessFrameResult(); result.Target = new TargetDetails(); int kwadratWidth; srcVertices = acd.MainTargetDetails.TargetRect; if (srcVertices == null) { return(null); } kwadratWidth = Convert.ToInt32(srcVertices[2].X - srcVertices[3].X); switch (acd.CurrentState) { case BidaSiusState.Start: return(null); break; case BidaSiusState.SetTargetBoundries: SetTargetBoundries(srcVertices, kwadratWidth, result, acd); break; case BidaSiusState.SetTargetSizeNPosition: SetTargetSizeNPosition(srcVertices, kwadratWidth, result, acd); break; case BidaSiusState.Play: FuseThisTarget(srcVertices, kwadratWidth, result, acd); break; default: throw new ArgumentOutOfRangeException(); } return(result); }
private static void FuseThisTarget(PointF[] src_vertices, int kwadratWidth, ProcessFrameResult result, AditionaCapturelData acd) { result.Target.BlackCenter = acd.MainTargetDetails.BlackCenter; result.Target.BlackR = acd.MainTargetDetails.BlackR; #region wyznaczenie prostokatow i kwadratow do transformacji perspektywy PointF[] dstVertices = new PointF[] { new PointF(0, 0), //tl topleft new PointF(kwadratWidth, 0), //tr new PointF(kwadratWidth, kwadratWidth), //br new PointF(0, kwadratWidth) }; //bl //kwadrat z lewym dolnym rogiem w tym samym miejscu co znaleziony - tylko po to żeby zobrazować //PointF[] dst_vertices_kwadrat_lewydol = new PointF[] { // new PointF(bo_ord[3].X , bo_ord[3].Y - kwadratWidth), // new PointF(bo_ord[3].X + kwadratWidth, bo_ord[3].Y - kwadratWidth), // new PointF(bo_ord[3].X + kwadratWidth, bo_ord[3].Y), // bo_ord[3]}; #endregion wyznaczenie prostokatow i kwadratow do transformacji perspektywy using (Mat warped = new Mat()) { #region tranformacja perspektywy Mat warpMatrix = CvInvoke.GetPerspectiveTransform(src_vertices, dstVertices); Size size = new Size(kwadratWidth, kwadratWidth); CvInvoke.WarpPerspective(acd.Frame, warped, warpMatrix, size, Inter.Linear, Warp.Default); #endregion tranformacja perspektywy #region rysowanie pierscieni //Mat circleImage = warped.Clone(); var pix = Pix(acd.MainTargetDetails.BlackR); //DrawCircles(circleImage, pix, useThisTarget.BlackCenter); #endregion rysowanie pierscieni #region blur gray canny samej tarczy Mat canny_output12 = new Mat(); Mat smallGrayFrame12 = new Mat(); Mat smoothedGrayFrame12 = new Mat(); CvInvoke.PyrDown(warped, smallGrayFrame12); CvInvoke.PyrUp(smallGrayFrame12, smoothedGrayFrame12); CvInvoke.CvtColor(smoothedGrayFrame12, smoothedGrayFrame12, ColorConversion.Bgr2Gray); // CvInvoke.GaussianBlur(smoothedGrayFrame12, smoothedGrayFrame12, new Size(9, 9), 1, 1); result.SmoothedOryginal = smoothedGrayFrame12;//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ #region test CvInvoke.Canny(smoothedGrayFrame12, canny_output12, acd.FirstCannyThresh, acd.secondCannyThresh); #endregion test #endregion blur gray canny samej tarczy #region rozpoznawanie strzału int czteryIpolmmRInt = Convert.ToInt32(FourNHalfR(pix)); int zapasSizeMax = 5; int zapasSizeMin = 1; CircleF[] przestrzeliny = CvInvoke.HoughCircles(smoothedGrayFrame12, HoughType.Gradient, 1, 400, acd.firstCannyThresh1, acd.secondCannyThresh1, czteryIpolmmRInt - zapasSizeMin, czteryIpolmmRInt + zapasSizeMax); result.Warped = warped.Clone();//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ foreach (CircleF shot in przestrzeliny) { CvInvoke.Circle(warped, Point.Round(shot.Center), czteryIpolmmRInt, new Bgr(Color.Blue).MCvScalar, 1, LineType.AntiAlias, 0); CvInvoke.Circle(warped, Point.Round(shot.Center), czteryIpolmmRInt - zapasSizeMin, new Bgr(Color.BlueViolet).MCvScalar, 1, LineType.AntiAlias, 0); CvInvoke.Circle(warped, Point.Round(shot.Center), czteryIpolmmRInt + zapasSizeMax, new Bgr(Color.Chartreuse).MCvScalar, 1, LineType.AntiAlias, 0); result.Shot = WyliczWartoscPrzestrzeliny(shot.Center, acd.MainTargetDetails); if (acd.MainTargetDetails.CameraFlipped) { result.TargetScanWithResult = Rotate180(warped.Clone());//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ } else { result.TargetScanWithResult = warped.Clone();//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ } break; } #endregion } }
private static void SetTargetSizeNPosition(PointF[] src_vertices, int kwadratWidth, ProcessFrameResult result, AditionaCapturelData acd) { #region wyznaczenie prostokatow i kwadratow do transformacji perspektywy PointF[] dstVertices = new PointF[] { new PointF(0, 0), //tl topleft new PointF(kwadratWidth, 0), //tr new PointF(kwadratWidth, kwadratWidth), //br new PointF(0, kwadratWidth) }; //bl #endregion wyznaczenie prostokatow i kwadratow do transformacji perspektywy #region tranformacja perspektywy Mat warpMatrix = CvInvoke.GetPerspectiveTransform(src_vertices, dstVertices); Size size = new Size(kwadratWidth, kwadratWidth); using (Mat warped = new Mat()) { CvInvoke.WarpPerspective(acd.Frame, warped, warpMatrix, size, Inter.Linear, Warp.Default); #endregion tranformacja perspektywy #region rysowanie pierscieni var pix = Pix(acd.MainTargetDetails.BlackR); DrawCircles(warped, pix, acd.MainTargetDetails.BlackCenter); result.Warped = warped.Clone();//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ } #endregion rysowanie pierscieni }
private static void SetTargetBoundries(PointF[] src_vertices, int kwadratWidth, ProcessFrameResult result, AditionaCapturelData acd) { //pokaż wszystko na oryginalnym obrazku Mat targetMarked = acd.Frame; CvInvoke.Polylines(targetMarked, Array.ConvertAll(src_vertices, Point.Round), true, new Bgr(Color.DarkOrange).MCvScalar, 2); result.TargetMarked = targetMarked;//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ }