/// <summary> /// Захватывает кадр с камеры, если она активна, и обрабатывает его. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Camera_ImageGrab(object sender, EventArgs e) { if ((Camera != null) && Camera.Grab() && Camera.Retrieve(Frame.Mat)) { FrameResult fr = Proc.ProcessFrame(Frame.Clone()); //обрабатываем кадр if (fr.QRLocation != null) { lb_qr.Visible = true; countQR += 1; lb_qr.Text = ("QR found: " + countQR + "times"); } if (fr.ArucoLocation != null) { lb_aruco.Visible = true; countAruco += 1; lb_aruco.Text = ("Aruco found: " + countAruco + "times"); } // и просто показываем результат ViewBox.Image = fr.Visual; this.Text = "Data: " + fr.Commentary; TiltAngleLbl.Text = double.IsNaN(fr.TiltAngle) ? "------" : string.Format("Наклон {0:f2} гр", fr.TiltAngle * 180.0 / Math.PI); } }
public void Visualize(FrameResult fr) { if (fr.QRLocation != null) { fr.Visual.DrawPolyline(fr.QRLocation, true, new Bgr(0, 0, 255), 2); fr.Visual.Draw(new CircleF(fr.QRCenter, 6), new Bgr(0, 255, 0), 2); fr.Visual.Draw(new CircleF(fr.QRLocation[0], 6), new Bgr(255, 255, 255), 2); fr.Visual.Draw(new CircleF(fr.QRLocation[2], 6), new Bgr(255, 0, 0), 2); Point TopMiddle = PointOperations.Middle(fr.QRLocation[0], fr.QRLocation[3]); fr.Visual.Draw(new LineSegment2D(fr.QRCenter, TopMiddle), new Bgr(0, 255, 0), 2); } if (fr.ArucoLocation != null) { ArucoInvoke.DrawDetectedMarkers(fr.Visual, corners, ids, new MCvScalar(0, 255, 0)); } }
/// <summary> /// Обрабатывает один поступивший кадр. /// </summary> /// <param name="frame">Кадр для анализа.</param> /// <returns>Сведения об объектах в кадре.</returns> public FrameResult ProcessFrame(Image <Bgr, byte> frame) { if (bwframe == null) { bwframe = new Image <Gray, byte>(frame.Size); } if (grayframe == null) { grayframe = new Image <Gray, byte>(frame.Size); } //if (vid == null) vid = new VideoWriter("video.avi", 24, frame.Size, true); vid?.Write(frame.Mat); grayframe.ConvertFrom(frame); int blocksize = 51; CvInvoke.AdaptiveThreshold(grayframe, bwframe, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.MeanC, Emgu.CV.CvEnum.ThresholdType.Binary, blocksize, 5); FrameResult fr = new FrameResult(frame); ArucoInvoke.DetectMarkers(bwframe, ArucoDictionary, corners, ids, _detectorParameters, rejected); if (ids.Size > 0) { fr.ArucoLocation = corners[0].ToArray(); } // Ищем коды на изображении List <ZBar.Symbol> symbols = Scanner.Scan(bwframe.ToBitmap()); if (symbols.Count == 0) { return(fr); } ZBar.Symbol s = symbols[0]; fr.QRLocation = s.Location.ToArray(); fr.QRCenter = PointOperations.Middle(fr.QRLocation); Point RightMiddle = PointOperations.Middle(fr.QRLocation[3], fr.QRLocation[2]); fr.TiltAngle = Math.Atan2(RightMiddle.Y - fr.QRCenter.Y, RightMiddle.X - fr.QRCenter.X); fr.Commentary = s.Data; Visualize(fr); return(fr); }