private OpenCvSharp.Point[] findMostOuterBoxCorner(Mat canny) { OpenCvSharp.Point[][] contours; HierarchyIndex[] hierarchy; Cv2.FindContours(canny, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxTC89KCOS); int maxIndex = 0; double maxArea = double.MinValue; for (int i = 0; i < contours.Length; i++) { OpenCvSharp.Point[] p = contours[i]; double length = Cv2.ArcLength(p, true); double area = Cv2.ContourArea(p, true); //if (Math.Abs(area) > maxArea) if (area > maxArea) { maxArea = area;//maxArea = Math.Abs(area); maxIndex = i; } } OpenCvSharp.Point[] p1 = contours[maxIndex]; double length1 = Cv2.ArcLength(p1, true); OpenCvSharp.Point[] pp = Cv2.ApproxPolyDP(p1, 0.02 * length1, true); return(pp); }
private void find_max_area(Mat src_gray, ref OpenCvSharp.Point center, ref Mat dst_color)//カラーで返す { src_gray.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); Mat[] contours; Mat hie = new Mat(); Cv2.FindContours(src_gray, out contours, hie, RetrievalModes.External, ContourApproximationModes.ApproxSimple); if (contours.Length > 0) { double max_size = 0; int max_index = 0; for (int i = 0; i < contours.Length; i++) { double size = Cv2.ContourArea(contours[i]); if (max_size < size) { max_size = size; max_index = i; } } Cv2.DrawContours(dst_color, contours, max_index, new Scalar(255, 255, 255), -1); RotatedRect box = Cv2.MinAreaRect(contours[max_index]); center = box.Center; } contours = null; hie.Dispose(); src_gray.Dispose(); }
public void processFrame(ref Mat binary, ref Rect rect) { Point[][] contours = null; //vector<Vec4i> hierarcy; HierarchyIndex[] hierarcy = null; //画椭圆及中心 Cv2.FindContours(binary, out contours, out hierarcy, RetrievalModes.External, ContourApproximationModes.ApproxNone); if (contours.GetLength(0) > 0) { double maxArea = 0.0; for (int t = 0; t < contours.GetLength(0); t++) { double area = Cv2.ContourArea(contours[t]); if (area > maxArea) { maxArea = area; rect = Cv2.BoundingRect(contours[t]); } } } else { rect.X = rect.Y = rect.Width = rect.Height = 0; } }
private void SegmentPuzzle(Mat img, out Mat roi, out Rect roiRect) { try { Point[][] contours; HierarchyIndex[] hierarchyIndexes; Cv2.FindContours(img, out contours, out hierarchyIndexes, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple); if (contours.Length == 0) { throw new Exception("No contour found in image"); } double[] area = new double[contours.Length]; for (int k = 0; k != contours.Length; ++k) { Point[] appx = Cv2.ApproxPolyDP(contours[k], 4, true); if (appx.Length != 4 || !Cv2.IsContourConvex(appx)) { area[k] = 0; } else { area[k] = Cv2.ContourArea(appx); } } int kmax = area.Select((value, index) => new { value, index }).OrderByDescending(x => x.value).First().index; roiRect = Cv2.BoundingRect(contours[kmax]); roi = img[roiRect]; } catch (Exception e) { throw new Exception(e.Message); } }
public override void OnInspectorGUI() { DrawDefaultInspector(); if (GUILayout.Button("Actualizar")) { foreach (FindContoursCableCanal coso in targets) { coso.Actualizar(); } } var dispMat = target as FindContoursCableCanal; var mat = dispMat.dibujo; if (mat == null) { GUILayout.Label("Mat es NULL"); } else { GUILayout.Label(string.Format("Mat es {0}", mat.CvPtr)); GUILayout.Label(string.Format("tam : ({0},{1})", mat.Width, mat.Height)); } if (dispMat.puntos != null) { GUILayout.Label(string.Format("contornos : {0}", dispMat.puntos.Length)); for (int i = 0; i < dispMat.puntos.Length && i < 5; i++) { GUILayout.Label(string.Format("area de {0}: {1}", i, Cv2.ContourArea(dispMat.puntos[i]))); } } }
public List <BallProperty> GetBallPropties(Mat frame) { //色でマスク Mat masked = GetColorMasked(frame); //輪郭膨張 masked = GetExpansion(masked); //水平線マスク masked = GetHorizonMasked(masked); //円でマスク //masked = GetCircleMasked(masked); //輪郭 Mat[] contours = GetContours(masked); var moList = new List <BallProperty>(); for (int i = 0; i < contours.Length; i++) { BallProperty m = new BallProperty(); m.index = i; m.contour = contours[i]; m.area = Cv2.ContourArea(contours[i]); moList.Add(m); } moList.Sort((a, b) => (int)(b.area - a.area)); return(moList); }
private bool findContourOfPuzzle() { Point[] simple; double maxArea = 0; double area; bool found = false; foreach (Point[] contour in contours) { var curve = new List <Point>(contour); double eps = Cv2.ArcLength(curve, true) * .08; simple = Cv2.ApproxPolyDP(curve, eps, true); if (simple.Length == 4 && Cv2.IsContourConvex(simple)) { area = Cv2.ContourArea(contour); if (area > maxArea) { likelyCandidate = simple; maxArea = area; found = true; } } } return(found); }
public static OpenCvSharp.Point[] FindBiggestContour(ref OpenCvSharp.Point[][] contours) { int biggest_solid_contourID; if (contours.Length > 1) { biggest_solid_contourID = -1; double biggest_contour = 0.0; for (int i = 0; i < contours.Length; i++) { double contour = Cv2.ContourArea(contours[i], false); if (contour > biggest_contour) { biggest_contour = contour; biggest_solid_contourID = i; } } } else { biggest_solid_contourID = 0; } return(contours[biggest_solid_contourID]); }
public void ContourArea() { var contour = new[] { new Point2f(0, 0), new Point2f(10, 0), new Point2f(10, 10), new Point2f(0, 10), }; var area = Cv2.ContourArea(contour); Assert.Equal(100, area); }
public static OpenCvSharp.Point[] Square(Mat src) { Mat[] split = Cv2.Split(src); Mat blur = new Mat(); Mat binary = new Mat(); OpenCvSharp.Point[] squares = new OpenCvSharp.Point[4]; int N = 10; double max = src.Size().Width *src.Size().Height * 0.9; double min = src.Size().Width *src.Size().Height * 0.1; for (int channel = 0; channel < 3; channel++) { Cv2.GaussianBlur(split[channel], blur, new OpenCvSharp.Size(5, 5), 1); for (int i = 0; i < N; i++) { Cv2.Threshold(blur, binary, i * 255 / N, 255, ThresholdTypes.Binary); OpenCvSharp.Point[][] contours; HierarchyIndex[] hierarchy; Cv2.FindContours(binary, out contours, out hierarchy, RetrievalModes.List, ContourApproximationModes.ApproxTC89KCOS); for (int j = 0; j < contours.Length; j++) { double perimeter = Cv2.ArcLength(contours[j], true); OpenCvSharp.Point[] result = Cv2.ApproxPolyDP(contours[j], perimeter * 0.02, true); double area = Cv2.ContourArea(result); bool convex = Cv2.IsContourConvex(result); } } } }
// // Update // 프레임과 마스크를 업데이트하여 검출상태를 갱신합니다. // // Parameters // source 영역 검출을 위한 행렬입니다. // 이 변수의 엣지를 검출한 뒤에 영역을 구하기 때문에 잡음이 없는 마스크 형식이 가장 이상적입니다. // public void Update(Mat source, isDetectedDelegate callback) { if (source.Type() != MatType.CV_8UC1) { source.ConvertTo(source, MatType.CV_8UC1); } this._detectedRects.Clear(); var edged = source.Canny(this._threshold.Start, this._threshold.End).Dilate(null).Erode(null); var cnts = null as Point[][]; var hierarchy = null as HierarchyIndex[]; edged.FindContours(out cnts, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple); foreach (var c in cnts) { if (Cv2.ContourArea(c) < 100) { continue; } var detectedRect = Cv2.MinAreaRect(c); if (callback(detectedRect) == false) { continue; } this._detectedRects.Add(detectedRect); } }
public List <RotatedRect> GetBarcode(Mat src) { Mat imgthresh = BarcodeRegion(src); Point[][] contours; HierarchyIndex[] hierarchy; Cv2.FindContours(imgthresh, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone, new Point(0, 0)); //Cv2.ImShow("X", imgthresh); //Cv2.WaitKey(); List <RotatedRect> barcode = new List <RotatedRect>(); for (int i = 0; i < contours.Length; i++) { double area = Cv2.ContourArea(contours[i]); RotatedRect rect = Cv2.MinAreaRect(contours[i]); double ratio = area / (rect.Size.Width * rect.Size.Height); if (1 - ratio < 0.2 && area > 200) { Cv2.DrawContours(src, contours, i, 255, -1); barcode.Add(rect); } } //RotatedRect p; barcode = barcode.OrderBy(p => p.Center.X).ThenBy(p => p.Center.Y).ToList(); Cv2.ImShow("X", src); Cv2.WaitKey(); return(barcode); }
private void FindContoursBy(ColorObjectCV colorObject) { List <Point[]> corners = new List <Point[]>(); HierarchyIndex[] h; bin.FindContours(out var contours, out h, RetrievalModes.External, ContourApproximationModes.ApproxSimple); double maxArea = 0; for (int i = 0; i < contours.Length; i++) { double area = Cv2.ContourArea(contours[i]); if (area > maxArea) { maxArea = area; corners.Add(contours[i]); } } if (corners.Count > 0) { for (int i = 0; i < corners.Count; i++) { bgr.DrawContours(corners, i, colorObject.ContourColor, 5); } } }
public static Point[] GetContours(Mat edgesImg) { Point[] screenCut = null; Point[][] contours; Mat heirchy = Mat.Zeros(edgesImg.Size(), edgesImg.Type()); HierarchyIndex[] hIndex; Cv2.FindContours(edgesImg, out contours, out hIndex, RetrievalModes.List, ContourApproximationModes.ApproxSimple); var query = contours .OrderBy(n => Cv2.ContourArea(n)) .Reverse(); foreach (var contour in query) { double peri = Cv2.ArcLength(contour, true); Point[] approx = Cv2.ApproxPolyDP(contour, 0.02 * peri, true); if (approx.Length == 4) { screenCut = approx; break; } } return(screenCut); }
/// <summary> /// Takes candidate shape and combined hull and returns best match /// </summary> /// <param name="areaSize">Area size</param> /// <param name="candidates">Candidates</param> /// <param name="hull">Hull</param> /// <returns></returns> private Point[] GetBestMatchingContour(double areaSize, List <Point[]> candidates, Point[] hull) { Point[] result = hull; if (candidates.Count == 1) { result = candidates[0]; } else if (candidates.Count > 1) { List <Point> keys = new List <Point>(); foreach (var c in candidates) { keys.AddRange(c); } Point[] joinedCandidates = Cv2.ConvexHull(keys); Point[] joinedHull = Cv2.ApproxPolyDP(joinedCandidates, Cv2.ArcLength(joinedCandidates, true) * 0.01, true); result = joinedHull; } // check further if (Settings.DropBadGuess) { double area = Cv2.ContourArea(result); if (area / areaSize < Settings.ExpectedArea * 0.75) { result = null; } } return(result); }
// radimo konturu i uzimamo silazni poredak prema veličini konture // te uzimamo prvu konturu u toj kolekciji (najveća kontura) private Point[] CreateHandContour(Mat mask) { mask.FindContours(out var contours, out var hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxSimple); return(contours.OrderByDescending(x => Cv2.ContourArea(x)).FirstOrDefault()); }
public void Apply(Mat src) { using (ResourceTracker t = new ResourceTracker()) { Stopwatch sw = new Stopwatch(); sw.Start(); Size srcSize = src.Size(); //todo: 只对脸部进行亮肤https://blog.csdn.net/cnbloger/article/details/77949949 //https://blog.csdn.net/skyqsdyy/article/details/89467143 //todo: 对src进行美颜、瘦脸、亮肤等。只要控制在一帧50ms之内即可。 Mat matMask = t.NewMat(srcSize, MatType.CV_8UC1, new Scalar(0)); RenderGreenScreenMask(src, matMask); //the area is by integer instead of double, so that it can improve the performance of comparision of areas int minBlockArea = (int)(srcSize.Width * srcSize.Height * this.MinBlockPercent); var contoursExternalForeground = Cv2.FindContoursAsArray(matMask, RetrievalModes.External, ContourApproximationModes.ApproxNone) .Select(c => new { contour = c, Area = (int)Cv2.ContourArea(c) }) .Where(c => c.Area >= minBlockArea) .OrderByDescending(c => c.Area).Take(5).Select(c => c.contour); //contoursExternalForeground = contoursExternalForeground.Select(c=>Cv2.ApproxPolyDP(c,0.5,true)); //a new Mat used for rendering the selected Contours var matMaskForeground = t.NewMat(srcSize, MatType.CV_8UC1, new Scalar(0)); //thickness: -1 means filling the inner space matMaskForeground.DrawContours(contoursExternalForeground, -1, new Scalar(255), thickness: -1); //matInternalHollow is the inner Hollow parts of body part. var matInternalHollow = t.NewMat(srcSize, MatType.CV_8UC1, new Scalar(0)); Cv2.BitwiseXor(matMaskForeground, matMask, matInternalHollow); int minHollowArea = (int)(minBlockArea * 0.5);//the lower size limitation of InternalHollow is less than minBlockArea, because InternalHollows are smaller //find the Contours of Internal Hollow var contoursInternalHollow = Cv2.FindContoursAsArray(matInternalHollow, RetrievalModes.External, ContourApproximationModes.ApproxNone) .Select(c => new { contour = c, Area = Cv2.ContourArea(c) }) .Where(c => c.Area >= minHollowArea) .OrderByDescending(c => c.Area).Take(10).Select(c => c.contour); //draw hollows foreach (var c in contoursInternalHollow) { matMaskForeground.FillConvexPoly(c, new Scalar(0)); } var element = t.T(Cv2.GetStructuringElement(MorphShapes.Cross, new Size(3, 3))); //smooth the edge of matMaskForeground Cv2.MorphologyEx(matMaskForeground, matMaskForeground, MorphTypes.Close, element, iterations: 6); Cv2.GaussianBlur(matMaskForeground, matMaskForeground, new Size(3, 3), 0, 0); var foreground = t.NewMat(src.Size(), MatType.CV_8UC4, new Scalar(0)); ZackCVHelper.AddAlphaChannel(src, foreground, matMaskForeground); //resize the _backgroundImage to the same size of src Cv2.Resize(_backgroundImage, src, src.Size()); //draw foreground(people) on the backgroundimage ZackCVHelper.DrawOverlay(src, foreground); Debug.WriteLine($"5:{sw.ElapsedMilliseconds}"); } }
private static void DrawContours(IReadOnlyList <Point[]> contours, IList <Point[]> contoursPoly, Mat drawing) { var center = new Point2f[contours.Count]; var radius = new float[contours.Count]; for (var i = 0; i < contours.Count; i++) { if (Cv2.ContourArea(contours[i]) >= 5000) { contoursPoly[i] = Cv2.ApproxPolyDP(contours[i], 3, true); Cv2.MinEnclosingCircle(contoursPoly[i], out center[i], out radius[i]); var tempContour = contours[i]; var hulls = new Point[1][]; var hullsI = new int[1][]; hulls[0] = Cv2.ConvexHull(tempContour); hullsI[0] = Cv2.ConvexHullIndices(tempContour); Cv2.DrawContours(drawing, hulls, -1, Scalar.Gold, 2); if (hullsI[0].Length > 0) { var defects = Cv2.ConvexityDefects(tempContour, hullsI[0]); if (defects.Length > 0) { for (var j = 1; j < defects.Length; j++) { var startIdx = defects[j][0]; var ptStart = tempContour[startIdx]; var farIdx = defects[j][2]; var ptFar = tempContour[farIdx]; if (GetDistance(ptStart, ptFar) > 1000 && ptStart.Y < center[i].Y && radius[i] >= 70) { Cv2.Circle(drawing, ptStart, 10, Scalar.Yellow, 2); Cv2.Line(drawing, ptStart, ptFar, Scalar.Pink, 2); _numberOfFingers++; } } if (radius[i] > 50) { Cv2.DrawContours(drawing, contoursPoly, i, Scalar.Red); Cv2.Circle(drawing, center[i], (int)radius[i], Scalar.White, 2); Cv2.Circle(drawing, center[i], 5, Scalar.Red, 2); if (Program.ControlMode) { _posX = (int)(4 * (center[i].X - 100)); _posY = (int)(4 * (center[i].Y - 100)); WinApiUtils.SetCursorPos(_posX, _posY); } } } } } } }
/// <summary> /// Crops and Transforms Image to return an image of a card /// </summary> /// <param name="imageInput"></param> /// <returns>Mat[]</returns> public Mat GetCardImage(Mat imageInput) { Mat image = imageInput; image = new Mat(image, roi); Mat transformedImage = GetTransformedImage(image); Mat returnCardImage = new Mat(); Point[][] contours; HierarchyIndex[] outputArray; //Only external contours... we don't want the textbox or the art of the card Cv2.FindContours(transformedImage, out contours, out outputArray, RetrievalModes.External, ContourApproximationModes.ApproxSimple); //FindContours will give array of contours detected in image. Here we filter to get the contour of a card by... foreach (Point[] contour in contours) { //shaping out the contour Point[] contourPoints = Cv2.ApproxPolyDP(contour, .01 * Cv2.ArcLength(contour, true), true); //and then checking if it's a rectangle and is big enough to be our card... if (contourPoints.Length == 4 && Cv2.ContourArea(contour) > 600) { //if it is, apply a perspective transform to get a flat image and return it Point2f[] arrayContourPoints = new Point2f[4]; for (int i = 0; i < arrayContourPoints.Length; i++) { arrayContourPoints[i] = contourPoints[i]; } var corners = GetCorners(arrayContourPoints); if (corners == null) { returnCardImage = null; break; } int rotation = GetCardRotation(corners); ///The order of the corners matter for the perspective transform. The order differs depending on rotation. if (rotation < 0) { arrayContourPoints[0] = corners["leftCorner"]; arrayContourPoints[1] = corners["topCorner"]; arrayContourPoints[2] = corners["bottomCorner"]; arrayContourPoints[3] = corners["rightCorner"]; } else { arrayContourPoints[0] = corners["topCorner"]; arrayContourPoints[1] = corners["rightCorner"]; arrayContourPoints[2] = corners["leftCorner"]; arrayContourPoints[3] = corners["bottomCorner"]; } Point2f[] destinationPoints = { new Point(0, 0), new Point(672, 0), new Point(0, 936), new Point(672, 936) }; Mat perspective = Cv2.GetPerspectiveTransform(arrayContourPoints, destinationPoints); Cv2.WarpPerspective(image, returnCardImage, perspective, new Size(672, 936)); break; } } return(returnCardImage); }
static List <Point[][]> AdaptiveThreshold_Based_Extract_Defect(Mat Src, List <OpenCvSharp.Point[]> contours_final) { //=========prepare adaptive threshold input Mat Adaptive_Src = Mat.Zeros(Src.Size(), MatType.CV_8UC1); //用adaptive threshold 濾出瑕疵 Cv2.GaussianBlur(Src, Adaptive_Src, new OpenCvSharp.Size(3, 3), 0, 0); /*//=====================================1008test== mask inner and outer to black=================================== * OpenCvSharp.Point[][] test = new Point[1][]; * test[0] = contours_final[1]; * Cv2.DrawContours(Adaptive_Src, test, -1, 0, -1); * Cv2.DrawContours(Adaptive_Src, test, -1, 0, 10); * Adaptive_Src.SaveImage("adaptive_prepocessing.jpg"); * //================================================================================== */ Cv2.AdaptiveThreshold(Adaptive_Src, Adaptive_Src, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 45, 105 / 10); Adaptive_Src.SaveImage("adaptive.jpg"); //讓黑白相反(not opetation) Mat Src_255 = new Mat(Adaptive_Src.Size(), MatType.CV_8UC1, new Scalar(255)); Cv2.Subtract(Src_255, Adaptive_Src, Adaptive_Src); // denoise OpenCvSharp.Point[][] temp = new Point[1][]; Point[][] contours; HierarchyIndex[] hierarchly; Cv2.FindContours(Adaptive_Src, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple); foreach (OpenCvSharp.Point[] contour_now in contours) { if (Cv2.ContourArea(contour_now) < 100) { //Console.WriteLine("Arc Length: " + (Cv2.ArcLength(contour_now, true) + " Area: " + Cv2.ContourArea(contour_now))+" Length/Area:" +(Cv2.ArcLength(contour_now, true) / Cv2.ContourArea(contour_now))); OpenCvSharp.Point[] approx = Cv2.ApproxPolyDP(contour_now, 0.000, true); temp[0] = approx; Cv2.DrawContours(Adaptive_Src, temp, -1, 0, -1); } } //Mat kernel = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(13, 7)); //Adaptive_Src = Adaptive_Src.MorphologyEx(MorphTypes.Close, kernel); //=========================吃掉邊界======================================= temp[0] = contours_final[0]; Cv2.DrawContours(Adaptive_Src, temp, -1, 0, 30); temp[0] = contours_final[1]; Cv2.DrawContours(Adaptive_Src, temp, -1, 0, 45); Adaptive_Src.SaveImage("a.jpg"); //上面已經得到defect圖,用Find_Defect_Contour_and_Extract萃取出來 return(Find_Defect_Contour_and_Extract(Src, Adaptive_Src, contours_final)); }
private static OpenCvSharp.Point[] GetCNTHull(Mat image, out OpenCvSharp.Point[] contour) { HierarchyIndex[] hierarchies; OpenCvSharp.Point[][] contours; image.FindContours(out contours, out hierarchies, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple); contour = contours.MaxBy(x => Cv2.ContourArea(x)).FirstOrDefault(); OpenCvSharp.Point[] hull = Cv2.ConvexHull(contour); return(hull); }
private Mat findPoly(Mat frame) { foreach (OpenCvSharp.Point[] contour in contours) { //var lines = Cv2.HoughLines(frame, 0.4, 1, 120); OpenCvSharp.Point[] walls = Cv2.ApproxPolyDP(contour, 5.2, true); if (!walls.Any(w => w.X < FrameWidth || w.X > CaptureWidth - FrameWidth || w.Y < FrameWidth || w.Y > CaptureHeight - FrameWidth)) { if (Cv2.ContourArea(walls, false) > 10000 && walls.Length < 25) { drawShape(frame, walls, Scalar.Green); wallObjects.AddRange(Converter.ContourToWall(walls)); } } OpenCvSharp.Point[] shapes = Cv2.ApproxPolyDP(contour, 6.2, true); if (!shapes.Any(w => w.X < FrameWidth || w.X > CaptureWidth - FrameWidth || w.Y < FrameWidth || w.Y > CaptureHeight - FrameWidth)) { if (Cv2.ContourArea(shapes, false) < 10000 && Cv2.ContourArea(shapes, false) > 500 && shapes.Length <= 5 && shapes.Length > 2) { List <double> cos = new List <double>(); for (int j = 2; j < shapes.Length + 1; j++) { cos.Add(Angle(shapes[j % shapes.Length], shapes[j - 2], shapes[j - 1])); } // Sort ascending the cosine values cos.Sort(); // Get the lowest and the highest cosine double mincos = cos.First(); double maxcos = cos.Last(); if (maxcos > 0.8 || -mincos > 0.8) { drawShape(frame, shapes, Scalar.GreenYellow); wallObjects.AddRange(Converter.ContourToWall(shapes)); } else { Gem gemObject = Converter.ContourToGem(shapes); if (!gemObjects.Any(g => Math.Abs(g.Position.X - gemObject.Position.X) < 50 && Math.Abs(g.Position.Y - gemObject.Position.Y) < 50)) { gemObjects.Add(gemObject); drawShape(frame, shapes, Scalar.Blue); } } //if(Cv2.ContourArea(shapes, false) < 3000) } } } //Cv2.DrawContours(frame, contours, -1,Scalar.Red); return(frame); }
public double CalculateArea() { var area = Cv2.ContourArea(ContourPoints); var grandchildren = Children.SelectMany(c => c.Children); var outermostChildren = Children.Where(c => !grandchildren.Any(gc => gc == c)); foreach (var child in outermostChildren) { area -= child.CalculateArea(); } return(area); }
public void DetectHandFrom(Pipeline pipeline) { FrameCount++; ElapsedMillisecondsOfLastFrames = (int)(pipeline.FrameDate - _lastFrameDate).TotalMilliseconds; CheckAllFramesLifeTime(pipeline.FrameDate); CheckTrackQueueLifeTime(); _allFrames.Enqueue(pipeline.FrameDate); OpenCvSharp.Point[][] contours; HierarchyIndex[] hierarchyIndices; Cv2.FindContours(pipeline.OutputMat, out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxSimple); OpenCvSharp.Point[] largestArea = null; double largestAreaSize = 0; foreach (var contour in contours) { var area = Cv2.ContourArea(contour, false); if (area > TrackerConfig.MinHandAreaSize) { if (largestArea == null) { largestArea = contour; largestAreaSize = area; } else { if (area > largestAreaSize) { largestAreaSize = area; largestArea = contour; } } } } HandArea = largestArea; if (HandArea != null) { TrackerVisualisation = new Mat(); pipeline.InputMat.CopyTo(TrackerVisualisation); Cv2.DrawContours(TrackerVisualisation, new OpenCvSharp.Point[][] { HandArea }, -1, new Scalar(0, 255, 0), 3); var newSample = HandTrackerSample.From(DateTime.Now, pipeline.InputMat, HandArea); _samples.Enqueue(newSample); DetectHulls(); } else { TrackerVisualisation = null; } _lastFrameDate = pipeline.FrameDate; }
Mat getBinMask() { Mat binmask = new Mat(_gcut.Size(), MatType.CV_8U); binmask = _gcut & (byte)GrabCutClasses.FGD; binmask = binmask * 255; Mat tmp = new Mat(); binmask.CopyTo(tmp); OpenCvSharp.HierarchyIndex[] hi; binmask *= 0; Cv2.FindContours(tmp, out co, out hi, RetrievalModes.CComp, ContourApproximationModes.ApproxNone); //simplify for (int i = 0; i < co.Length; i++) { double tol = 0.5; co[i] = simplifyDouglasPeucker(co[i].Select(z => new Point2f(z.X, z.Y)).ToArray(), tol).Select(z => new OpenCvSharp.Point(z.X, z.Y)).ToArray(); } for (int i = 0; i < co.Length; i++) { if (hi[i].Parent != -1) { continue; } if (Cv2.ContourArea(co[i]) < 50) { continue; } Cv2.DrawContours(binmask, co, i, new Scalar(255, 255, 255), Cv2.FILLED, LineTypes.AntiAlias); } for (int i = 0; i < co.Length; i++) { if (hi[i].Parent == -1) { continue; } if (Cv2.ContourArea(co[i]) < 50) { continue; } Cv2.DrawContours(binmask, co, i, new Scalar(0, 0, 0), Cv2.FILLED, LineTypes.AntiAlias); } binmask.CopyTo(_bin); return(binmask); }
public static List <Rectangle> DetectCarplates(Bitmap image) { var result = new List <Rectangle>(); var original = BitmapConverter.ToMat(image); var gray = image.ToGrayscaleMat(); var src = new Mat(); gray.CopyTo(src); var threshImage = new Mat(); Cv2.AdaptiveThreshold(gray, threshImage, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 9, 9); SaveImage(threshImage, "threshhold"); Point[][] contours; HierarchyIndex[] hierarchyIndexes; Cv2.FindContours(threshImage, out contours, out hierarchyIndexes, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple); if (contours.Length == 0) { return(new List <Rectangle>()); } var sorted = contours .OrderByDescending(x => Cv2.ContourArea(x)) .Take(100) .Where(x => { var rect = Cv2.BoundingRect(x); return(rect.IsHorizontalCarplateBlock() && GetMeanColor(gray, rect)[0] > 135); }) .ToList(); foreach (var contour in sorted) { var boundingRect = Cv2.BoundingRect(contour); result.Add(boundingRect.ToRectangle()); var meanColor = GetMeanColor(gray, boundingRect); Cv2.Rectangle(original, new Point(boundingRect.X, boundingRect.Y), new Point(boundingRect.X + boundingRect.Width, boundingRect.Y + boundingRect.Height), new Scalar(0, 0, 255), 2); Cv2.PutText(original, meanColor[0].ToString(), new Point(boundingRect.X + 10, boundingRect.Y + 10), HersheyFonts.HersheyPlain, 0.75, Scalar.Red); } SaveImage(original, "detected"); return(result); }
public static OpenCvSharp.Point[] Square(Mat src) { Mat[] split = Cv2.Split(src); Mat blur = new Mat(); Mat binary = new Mat(); OpenCvSharp.Point[] squares = new OpenCvSharp.Point[4]; int N = 10; double max = src.Size().Width *src.Size().Height * 0.9; double min = src.Size().Width *src.Size().Height * 0.1; for (int channel = 0; channel < 3; channel++) { Cv2.GaussianBlur(split[channel], blur, new OpenCvSharp.Size(5, 5), 1); for (int i = 0; i < N; i++) { Cv2.Threshold(blur, binary, i * 255 / N, 255, ThresholdTypes.Binary); OpenCvSharp.Point[][] contours; HierarchyIndex[] hierarchy; Cv2.FindContours(binary, out contours, out hierarchy, RetrievalModes.List, ContourApproximationModes.ApproxTC89KCOS); Mat test = src.Clone(); Cv2.DrawContours(test, contours, -1, new Scalar(0, 0, 255), 3); for (int j = 0; j < contours.Length; j++) { double perimeter = Cv2.ArcLength(contours[j], true); OpenCvSharp.Point[] result = Cv2.ApproxPolyDP(contours[j], perimeter * 0.02, true); double area = Cv2.ContourArea(result); bool convex = Cv2.IsContourConvex(result); if (result.Length == 4 && area > min && area < max && convex) { double cos = 0; for (int k = 1; k < 5; k++) { double t = Math.Abs(Angle(result[(k - 1) % 4], result[k % 4], result[(k + 1) % 4])); cos = cos > t ? cos : t; } if (cos < 0.15) { squares = result; } } } } } return(squares); }
public override void Actualizar(IEsCable nodo) { if (nodo == null) { return; } var matEntrante = nodo.MatOut(); if (matEntrante == null) { return; } // if (generarNuevoMat) // { // if (mat == null) mat = new Mat(); // } // else { if (mat != matEntrante) { mat = matEntrante; } } puntos = Cv2.FindContoursAsArray(mat, retrievalMode, approxMode); if (ordenarPorArea) { puntos = puntos.OrderByDescending(e => Cv2.ContourArea(e)).ToArray(); } dibujo = new Mat(matEntrante.Rows, matEntrante.Cols, MatType.CV_8UC3); var loopColorsEvery = this.loopColorsEvery; if (loopColorsEvery < 1) { loopColorsEvery = 1; } for (int i = 0; i < puntos.Length; i++) { var area = Cv2.ContourArea(puntos[i]); if (area < umbralArea) { continue; } var colContornos = this.colContornos.Evaluate((i % loopColorsEvery) / (float)loopColorsEvery); var colScalar = new Scalar(colContornos.b * 255, colContornos.g * 255, colContornos.r * 255); Cv2.DrawContours(dibujo, puntos, i, colScalar, 1, tipoDibujo); } PropagarActualizacion(); }
private List <Mat> getIndividualBoxes(Mat unwarped) { int pixelnum = 3; List <Mat> boxes = new List <Mat>(); Point[][] contours; HierarchyIndex[] hierarchyIndex; int inc = unwarped.Rows / 9; for (int x = 0; x < 9; x += 1) { for (int y = 0; y < 9; y += 1) { int xind = x * inc; int yind = y * inc; var num = unwarped.SubMat(xind + pixelnum, xind - pixelnum + inc, yind + pixelnum, yind - pixelnum + inc); if (Cv2.CountNonZero(num) < 100) { boxes.Add(null); } else { num.FindContours(out contours, out hierarchyIndex, RetrievalModes.External, ContourApproximationModes.ApproxSimple); Point[] LargestContour = new Point[1]; double maxArea = 0; foreach (var c in contours) { double tempArea = Cv2.ContourArea(c); if (tempArea > maxArea) { maxArea = tempArea; LargestContour = c; } } int w = 25; Rect rec = Cv2.BoundingRect(LargestContour); Mat numbox = new Mat(w, w, MatType.CV_8UC1); var dest = new Point2f[] { new Point2f(0, 0), new Point2f(0, w), new Point2f(w, w), new Point2f(w, 0) }; var transform = Cv2.GetPerspectiveTransform(rect2Contour(rec), dest); Cv2.WarpPerspective(num, numbox, transform, new Size(w, w)); boxes.Add(numbox); } } } return(boxes); }
private void button1_Click(object sender, EventArgs e) { //if (openFileDialog1.ShowDialog() == DialogResult.OK) //{ //using (Mat Image原图 = Cv2.ImRead(openFileDialog1.FileName)) using (Mat Image原图 = Cv2.ImRead(@"D:\new1\input2.tif")) using (Mat Image原图角度纠正后 = handle.The_Angle_correct(Image原图)) using (Mat Image表格 = handle.To_extract_form(Image原图角度纠正后)) { OpenCvSharp.Point[][] contours查找表格轮廓 = handle.Find_the_outline(Image表格); Rect[] rect = new Rect[contours查找表格轮廓.Length]; OpenCvSharp.Point[][] contours_poly = new OpenCvSharp.Point[contours查找表格轮廓.Length][]; Point2f center; for (int i = 0; i < contours查找表格轮廓.Length; i++) { //获取区域的面积,如果小于某个值就忽略,代表是杂线不是表格 double area = Cv2.ContourArea(contours查找表格轮廓[i]); if (area < 1000) { continue; } if (area > 4000000) { continue; } //拟近的多边形 ApproxPolyDP contours_poly[i] = Cv2.ApproxPolyDP(contours查找表格轮廓[i], 3, true); //矩形 最小正矩形 包围 BoundingRect rect[i] = Cv2.BoundingRect(contours_poly[i]); //矩形 最小斜矩形 包围 MinAreaRect RotatedRect 斜矩形 = Cv2.MinAreaRect(contours查找表格轮廓[i]); Rect rect2 = new Rect(rect[i].X + 2, rect[i].Y + 2, rect[i].Width - 4, rect[i].Height - 4); Cv2.Rectangle(Image原图, rect2, Scalar.Red); Image原图.PutText(">>" + i.ToString() + "____" + area.ToString(), handle.getCenterPoint(rect2), HersheyFonts.HersheySimplex, 0.5, Scalar.Blue); } //using (new Window("Image原图", WindowMode.AutoSize, Image原图)) ////using (new Window("Image原图角度纠正后", WindowMode.AutoSize, Image原图角度纠正后)) ////using (new Window("Image表格", WindowMode.AutoSize, Image表格)) //{ // Image原图.SaveImage(@"D:\new1\画框框.tif"); // Window.WaitKey(0); //} Image原图.SaveImage(@"D:\new1\画框框.tif"); } }