EMGU CV随机冻结系统,在控制台上抛出奇怪的错误

时间:2018-08-03 13:25:55

标签: c# emgucv

我已从Nuget将EMGU CV(3.1.0.2282)添加到我的项目中。

问题是整个系统有时冻结(在调试中),有时会在线: CvInvoke.Canny(来源,cannyEdges,cannyThreshold,cannyThresholdLinking);

有时下一行,等等……我刚刚已经重启系统4次了……

更多。在控制台上进行首次初始操作时,我将获得如下结束的日志信息:

“错误:前端编译器生成失败”,但是某些opencv方法运行顺利。

我的代码如下:

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenCV.NETTest
{
    public class SquareRecognition
    {
        public SquareRecognition() {}

        private void PyrDownUp(UMat source)
        {
            using (UMat pyrDownGray = new UMat())
            {
                CvInvoke.PyrDown(source, pyrDownGray);
                CvInvoke.PyrUp(pyrDownGray, source);
            }
        }

        private UMat CannyEdges(UMat source)
        {
            double cannyThresholdLinking = 120.0;
            double cannyThreshold = 180.0;//5.0

            UMat cannyEdges = new UMat();
            CvInvoke.Canny(source, cannyEdges, cannyThreshold, cannyThresholdLinking);

            cannyEdges.Save(@"c:\ttt.jpg");

            return cannyEdges;
        }

        private List<RotatedRect> GetRectangles(UMat source)
        {
            //PyrDownUp(source);
            UMat cannyEdges = CannyEdges(source);

            List<RotatedRect> boxList = new List<RotatedRect>(); //a box is a rotated rectangle
            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            {
                CvInvoke.FindContours(cannyEdges, contours, null, RetrType.List, 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);
                        if (CvInvoke.ContourArea(approxContour, false) > 250) //only consider contours with area greater than 250
                        {
                            if (approxContour.Size == 4) //The contour has 4 vertices.
                            {
                                #region determine if all the angles in the contour are within [80, 100] degree
                                bool isRectangle = true;
                                Point[] pts = approxContour.ToArray();
                                LineSegment2D[] edges = PointCollection.PolyLine(pts, true);

                                for (int j = 0; j < edges.Length; j++)
                                {
                                    double angle = Math.Abs(
                                        edges[(j + 1) % edges.Length].GetExteriorAngleDegree(edges[j]));
                                    if (angle < 80 || angle > 100)
                                    {
                                        isRectangle = false;
                                        break;
                                    }
                                }
                                #endregion

                                if (isRectangle) boxList.Add(CvInvoke.MinAreaRect(approxContour));
                            }
                        }
                    }
                }
            }
            return boxList;
        }

        public void FindSquares(string FileName)
        {
            if (!File.Exists(FileName))
                throw new Exception(string.Format("No file: {0}", FileName));

            using (UMat uimageGray = new UMat())
            {
                using (Image<Bgr, Byte> img = new Image<Bgr, byte>(FileName))
                {
                    CvInvoke.CvtColor(img, uimageGray, ColorConversion.Bgr2Gray);
                }

                var RectList = GetRectangles(uimageGray);                                             
            }
        }
    }
}

0 个答案:

没有答案