private void FlirCameraStream_OnSourceChanged(FlirCameraStream sender, FlirCamera Source) { if (Source == null) { CameraFeed.Invoke(new Action(() => { CameraFeed.Image = null; })); FPSStatusLabel.Text = ((int)0).ToString("N2").PadLeft(3) + " FPS"; } }
private void FlirCameraStream_OnNewImage(FlirCameraStream sender, Image <Bgr, byte> Image) { FpsCount++; if (FpsCount >= 4) //About 15 fps { FpsCount = 0; CameraFeed.Invoke(new Action(() => { CameraFeed.Image = (Image == null) ? null : Image.Bitmap; })); FPSStatusLabel.Text = sender.FPS.ToString("N2").PadLeft(3) + " FPS"; } }
private void Capture_ImageGrabbed(object sender, EventArgs e) { lock (Capture) { if (Busy) { return; } Busy = true; } try { Capture.Retrieve(CameraFeed); var imageFrame = CameraFeed.ToImage <Bgr, byte>(); #region smooth var imgSmoothed = imageFrame.PyrDown().PyrUp(); imgSmoothed._SmoothGaussian(Smooth); imageBox2.Image = imgSmoothed; #endregion #region Color filter var imgColorFiltered = imgSmoothed.InRange(new Bgr(Bmin, Gmin, Rmin), new Bgr(Bmax, Gmax, Rmax)); imgColorFiltered = imgColorFiltered.PyrDown().PyrUp(); imgColorFiltered._SmoothGaussian(Smooth); // 3 imageBox3.Image = imgColorFiltered; #endregion #region erosion, delete noise //var imageErode = imgColorFiltered.Erode(2); //imageBox4.Image = imageErode; #endregion #region //var imageDilate = imageErode.Dilate(5); //imageBox5.Image = imageDilate; #endregion #region opening // open > erosion and dilate Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(5, 5), new Point(-1, -1)); var opening = imgColorFiltered.MorphologyEx(MorphOp.Open, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(1.0)); imageBox4.Image = opening; #endregion #region closing // open > erosion and dilate //Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(5, 5), new Point(-1, -1)); //var opening = imgColorFiltered.MorphologyEx(MorphOp.Close, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(1.0)); //imageBox4.Image = opening; #endregion #region gradient // open > erosion and dilate //Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(5, 5), new Point(-1, -1)); //var opening = imgColorFiltered.MorphologyEx(MorphOp.Gradient, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(1.0)); //imageBox4.Image = opening; #endregion #region Canny and edge detection //UMat cannyEdges = new UMat(); //CvInvoke.Canny(imageDilate, cannyEdges, CannyThreshold1, CannyThreshold2, CannyApertureSize, CannyL2Gradient); //imageBox6.Image = cannyEdges; #endregion #region objects VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(opening /*cannyEdges*/, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple); int count = contours.Size; for (int i = 0; i < count; i++) { using (VectorOfPoint contour = contours[i]) using (VectorOfPoint approxContour = new VectorOfPoint()) { CvInvoke.ApproxPolyDP(contour, approxContour, CvInvoke.ArcLength(contour, true) * 0.05, true); double area = CvInvoke.ContourArea(approxContour, false); if (area > 300) { CvInvoke.DrawContours(imageFrame, contour, -1, new MCvScalar(255, 0, 0), 2); imageFrame.Draw($"{area}", new Point(50, 50), Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.8, new Bgr(Color.Red)); } } } #endregion imageBox1.Image = imageFrame; //var imgSmoothed = imageFrame.PyrDown().PyrUp(); //imgSmoothed._SmoothGaussian(3); //imageBox2.Image = imgSmoothed; //var imgColorFiltered = imgSmoothed.InRange(new Bgr(Bmin, Gmin, Rmin), new Bgr(Bmax, Gmax, Rmax)); //imgColorFiltered = imgColorFiltered.PyrDown().PyrUp(); //imgColorFiltered._SmoothGaussian(3); //imageBox3.Image = imgColorFiltered; //Gray grayCannyThreshold = new Gray(GrayCannyTh); //Gray grayCircleThreshold = new Gray(GrayCircleTh); //Gray grayLinking = new Gray(80); //var imgCanny = imgColorFiltered.Canny(grayCannyThreshold.Intensity, grayLinking.Intensity); //imgColorFiltered.Canny(1.0, 2.0, 3, false); //var imgCircles = imageFrame.CopyBlank(); //var imgLines = imageFrame.CopyBlank(); //var imgPoly = imageFrame.CopyBlank(); //double dblAccumRes = 1.0; //double dblMinDist = 1.0; //int intMinRadius = 50; //int intMaxRadius = 150; ////var circles = imgColorFiltered.HoughCircles(grayCannyThreshold, grayCircleThreshold, dblAccumRes, dblMinDist, intMinRadius, intMaxRadius)[0]; ////foreach (var circ in circles) ////{ //// imgCircles.Draw(circ, new Bgr(Color.Red), 2); //// imageFrame.Draw(circ, new Bgr(Color.Red), 2); ////} ////Contour<Point> contours = imgCanny.FindContours(); ////List<RotatedRect> lstRectangles = new List<RotatedRect>(); //Double dblRhoRes = 1.0; //Double dblThetaRes = 4.0 * (Math.PI / 180.0); //int intThreshold = 20; //Double dblMinLineWidth = 30.0; //Double dblMinGapBetweenLines = 10.0; //imgColorFiltered.Ho //LineSegment2D[] lines = imgCanny.Clone().HoughLinesBinary(dblRhoRes, dblThetaRes, intThreshold, dblMinLineWidth, dblMinGapBetweenLines)[0]; //foreach (LineSegment2D line in lines) //{ // imgLines.Draw(line, new Bgr(Color.DarkGreen), 2); // imageFrame.Draw(line, new Bgr(Color.DarkGreen), 2); //} //imageBox4.Image = imgLines; //VectorOfVectorOfPointF contours = new VectorOfVectorOfPointF(); //Mat hierarchy = null; //CvInvoke.FindContours(imgCanny, contours, hierarchy, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainCode); ////Contour<Point> contours = imgCanny.FindContours(); //List<MCvBox2D> lstRectangles = new List<MCvBox2D>(); //List<Contour<Point>> lstPoluhons = new List<Contour<Point>>(); //while (contours != null) //{ // Contour<Point> contour = contours.ApproxPoly(10.0); // if (contour.Area > 250.0) // { // if (contour.Total == 4) // { // Point[] ptPoints = contour.ToArray(); // Boolean blnIsRectangle = true; // LineSegment2D[] ls2dEdges = PointCollection.PolyLine(ptPoints, true); // for (int i = 0; i < ls2dEdges.Length - 1; i++) // { // Double dblAngle = Math.Abs(ls2dEdges[(i + 1) % ls2dEdges.Length].GetExteriorAngleDegree(ls2dEdges[i])); // if (dblAngle < 80.0 || dblAngle > 100.0) // { // blnIsRectangle = false; // } // } // if (blnIsRectangle) // lstRectangles.Add(contour.GetMinAreaRect()); // } // } // contours = contours.HNext; //} //foreach (MCvBox2D rect in lstRectangles) //{ // imgTrisRectsPolys.Draw(rect, new Bgr(Color.Blue), 2); // if (chbDrawTrianglesAndPolygansOnOriginalImage.Checked == true) // { // imgOriginal.Draw(rect, new Bgr(Color.Blue), 2); // } //} //CvInvoke.CvtColor(CameraFeed, HSV, Emgu.CV.CvEnum.ColorConversion.Bgr2Hsv); //CvInvoke.InRange(HSV, new MCvScalar(Bmin, Gmin, Rmin), new MCvScalar(Bmax, Gmax, Rmax), Threshold); //CvInvoke.Mor //histogramBoxCapture. = CameraFeed; //var imageFrame = Frame.ToImage<Bgr, byte>(); } catch (Exception ex) { } Busy = false; }