【问题标题】:AccessViolationException was unhandled in EMGU CVEMGU CV 中未处理 AccessViolationException
【发布时间】:2012-07-06 02:19:26
【问题描述】:

我在运行程序时总是遇到这个错误...

我不确定我的程序出了什么问题,仅供参考,实际上我的程序包含许多图像处理算法,例如

  • viola-jones,用于手部检测的 haarcascade
  • 用于手部跟踪的 camshift + 卡尔曼滤波器
  • 计数手指的凸包和凸包缺陷
  • 和其他预处理 ....

我不知道这些过程是否会触发错误......

但是当我删除凸包/凸面缺陷时,错误消失了...... 有什么解决办法吗?

已编辑:(添加)

这是代码sn-p

 private void ExtractContourAndHull()
        {
            using (MemStorage storage = new MemStorage())
            {
                
                Contour<Point> contours = backproject.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage);
                Contour<Point> biggestContour = null;

                Double Result1 = 0;
                Double Result2 = 0;
                while (contours != null)
                {
                    Result1 = contours.Area;
                    if (Result1 > Result2)
                    {
                        Result2 = Result1;
                        biggestContour = contours;
                    }
                    contours = contours.HNext;
                }

                if (biggestContour != null)
                {
                    //image.Draw(biggestContour, new Bgr(Color.DarkViolet), 2);
                    Contour<Point> currentContour = biggestContour.ApproxPoly(biggestContour.Perimeter * 0.005, storage);
                    image.Draw(currentContour, new Bgr(Color.LimeGreen), 2);
                    biggestContour = currentContour;
                 

                    hull = biggestContour.GetConvexHull(ORIENTATION.CV_CLOCKWISE);
                    box = biggestContour.GetMinAreaRect();
                    PointF[] points = box.GetVertices();
                    //handRect = box.MinAreaRect();
                    //image.Draw(handRect, new Bgr(200, 0, 0), 1);

                    Point[] ps = new Point[points.Length];
                    for (int i = 0; i < points.Length; i++)
                        ps[i] = new Point((int)points[i].X, (int)points[i].Y);

                    image.DrawPolyline(hull.ToArray(), true, new Bgr(200, 125, 75), 2);
                    image.Draw(new CircleF(new PointF(box.center.X, box.center.Y), 3), new Bgr(200, 125, 75), 2);

                    //ellip.MCvBox2D= CvInvoke.cvFitEllipse2(biggestContour.Ptr);
                    //image.Draw(new Ellipse(ellip.MCvBox2D), new Bgr(Color.LavenderBlush), 3);

                    //PointF center;
                    //float radius;
                    //CvInvoke.cvMinEnclosingCircle(biggestContour.Ptr, out  center, out  radius);
                    //image.Draw(new CircleF(center, radius), new Bgr(Color.Gold), 2);

                    //image.Draw(new CircleF(new PointF(ellip.MCvBox2D.center.X, ellip.MCvBox2D.center.Y), 3), new Bgr(100, 25, 55), 2);
                    //image.Draw(ellip, new Bgr(Color.DeepPink), 2);

                    //CvInvoke.cvEllipse(image, new Point((int)ellip.MCvBox2D.center.X, (int)ellip.MCvBox2D.center.Y), new System.Drawing.Size((int)ellip.MCvBox2D.size.Width, (int)ellip.MCvBox2D.size.Height), ellip.MCvBox2D.angle, 0, 360, new MCvScalar(120, 233, 88), 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0);
                    //image.Draw(new Ellipse(new PointF(box.center.X, box.center.Y), new SizeF(box.size.Height, box.size.Width), box.angle), new Bgr(0, 0, 0), 2);


                    filteredHull = new Seq<Point>(storage);
                    for (int i = 0; i < hull.Total; i++)
                    {
                        if (Math.Sqrt(Math.Pow(hull[i].X - hull[i + 1].X, 2) + Math.Pow(hull[i].Y - hull[i + 1].Y, 2)) > box.size.Width / 10)
                        {
                            filteredHull.Push(hull[i]);
                        }
                    }

                    defects = biggestContour.GetConvexityDefacts(storage, Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE);
                    
                    defectArray = defects.ToArray();
                }
            }
        }
        private void DrawAndComputeFingersNum()
        {
            using (MemStorage storage = new MemStorage())
            {
                int fingerNum = 0;
                
                

                #region hull drawing
                //for (int i = 0; i < filteredHull.Total; i++)
                //{
                //    PointF hullPoint = new PointF((float)filteredHull[i].X,
                //                                  (float)filteredHull[i].Y);
                //    CircleF hullCircle = new CircleF(hullPoint, 4);
                //    image.Draw(hullCircle, new Bgr(Color.Aquamarine), 2);
                //}
                #endregion


                #region defects drawing
                ***defects = new Seq<MCvConvexityDefect>(storage);***
                for (int i = 0; i < defects.Total; i++)
                {
                    PointF startPoint = new PointF((float)defectArray[i].StartPoint.X,
                                                    (float)defectArray[i].StartPoint.Y);

                    PointF depthPoint = new PointF((float)defectArray[i].DepthPoint.X,
                                                    (float)defectArray[i].DepthPoint.Y);

                    PointF endPoint = new PointF((float)defectArray[i].EndPoint.X,
                                                    (float)defectArray[i].EndPoint.Y);

                    LineSegment2D startDepthLine = new LineSegment2D(defectArray[i].StartPoint, defectArray[i].DepthPoint);

                    LineSegment2D depthEndLine = new LineSegment2D(defectArray[i].DepthPoint, defectArray[i].EndPoint);

                    CircleF startCircle = new CircleF(startPoint, 5f);

                    
                    CircleF depthCircle = new CircleF(depthPoint, 5f);
                  
                    CircleF endCircle = new CircleF(endPoint, 5f);
                    MCvFont angga = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SCRIPT_COMPLEX, 0.5, 0.5);
                   // image.Draw(defectArray[i].StartPoint.X.ToString() + " , " + defectArray[i].StartPoint.Y.ToString(), ref angga, new Point(defectArray[i].StartPoint.X, defectArray[i].StartPoint.Y), new Bgr(Color.Red));
                    image.Draw(defectArray[i].StartPoint.X.ToString() + " , " + defectArray[i].StartPoint.Y.ToString() + " , " + i.ToString(), ref angga, new Point(defectArray[i].StartPoint.X, defectArray[i].StartPoint.Y), new Bgr(Color.Red));

                    //Custom heuristic based on some experiment, double check it before use
                    if ((startCircle.Center.Y < box.center.Y || depthCircle.Center.Y < box.center.Y) && (startCircle.Center.Y < depthCircle.Center.Y) && (Math.Sqrt(Math.Pow(startCircle.Center.X - depthCircle.Center.X, 2) + Math.Pow(startCircle.Center.Y - depthCircle.Center.Y, 2)) > box.size.Height / 6.5))
                    {
                        fingerNum++;
                        //image.Draw(startDepthLine, new Bgr(Color.Blue), 2);
                        //image.Draw(depthEndLine, new Bgr(Color.Magenta), 2);
                    }


                    image.Draw(startCircle, new Bgr(Color.Red), 2);
                    image.Draw(depthCircle, new Bgr(Color.Yellow), 5);
                    //image.Draw(endCircle, new Bgr(Color.DarkBlue), 4);
                }
                #endregion

                MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_DUPLEX, 5d, 5d);
                image.Draw(fingerNum.ToString(), ref font, new Point(50, 150), new Bgr(Color.White));
                hand.fingerChangedCompute = fingerNum;
            }

        }

       
        
        public MemStorage storage { get; set; } 

起源 看看 DrawandComputeFingersNUm() 你会看到一行

defects = new Seq<MCvConvexityDefect>(storage);

当我取消注释上面的行时会出现问题..

有人知道如何解决这个问题吗? 任何帮助,将不胜感激.. 谢谢

【问题讨论】:

  • 您不会得到询问仅基于程序使用的算法调试程序的结果。您的问题必须更加具体才能获得任何有用的帮助。
  • 好的@antlersoft我会修改问题..

标签: c# opencv emgucv


【解决方案1】:

如您所知,我是您正在使用的代码的创建者。

我的一位读者有时会发现与您类似的问题,评论了我的帖子并建议删除 using Memstorage 并将其放在全局然后清除 DrawAndComputeFingersNum() 函数中的存储

MemStorage storage = new MemStorage() //Global Declaration


private void DrawAndComputeFingersNum()
    {

            int fingerNum = 0;



            #region hull drawing
            //for (int i = 0; i < filteredHull.Total; i++)
            //{
            //    PointF hullPoint = new PointF((float)filteredHull[i].X,
            //                                  (float)filteredHull[i].Y);
            //    CircleF hullCircle = new CircleF(hullPoint, 4);
            //    image.Draw(hullCircle, new Bgr(Color.Aquamarine), 2);
            //}
            #endregion


            #region defects drawing
            defects = new Seq<MCvConvexityDefect>(storage);
            for (int i = 0; i < defects.Total; i++)
            {
                PointF startPoint = new PointF((float)defectArray[i].StartPoint.X,
                                                (float)defectArray[i].StartPoint.Y);

                PointF depthPoint = new PointF((float)defectArray[i].DepthPoint.X,
                                                (float)defectArray[i].DepthPoint.Y);

                PointF endPoint = new PointF((float)defectArray[i].EndPoint.X,
                                                (float)defectArray[i].EndPoint.Y);

                LineSegment2D startDepthLine = new LineSegment2D(defectArray[i].StartPoint, defectArray[i].DepthPoint);

                LineSegment2D depthEndLine = new LineSegment2D(defectArray[i].DepthPoint, defectArray[i].EndPoint);

                CircleF startCircle = new CircleF(startPoint, 5f);


                CircleF depthCircle = new CircleF(depthPoint, 5f);

                CircleF endCircle = new CircleF(endPoint, 5f);
                MCvFont angga = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SCRIPT_COMPLEX, 0.5, 0.5);
               // image.Draw(defectArray[i].StartPoint.X.ToString() + " , " + defectArray[i].StartPoint.Y.ToString(), ref angga, new Point(defectArray[i].StartPoint.X, defectArray[i].StartPoint.Y), new Bgr(Color.Red));
                image.Draw(defectArray[i].StartPoint.X.ToString() + " , " + defectArray[i].StartPoint.Y.ToString() + " , " + i.ToString(), ref angga, new Point(defectArray[i].StartPoint.X, defectArray[i].StartPoint.Y), new Bgr(Color.Red));

                //Custom heuristic based on some experiment, double check it before use
                if ((startCircle.Center.Y < box.center.Y || depthCircle.Center.Y < box.center.Y) && (startCircle.Center.Y < depthCircle.Center.Y) && (Math.Sqrt(Math.Pow(startCircle.Center.X - depthCircle.Center.X, 2) + Math.Pow(startCircle.Center.Y - depthCircle.Center.Y, 2)) > box.size.Height / 6.5))
                {
                    fingerNum++;
                    //image.Draw(startDepthLine, new Bgr(Color.Blue), 2);
                    //image.Draw(depthEndLine, new Bgr(Color.Magenta), 2);
                }


                image.Draw(startCircle, new Bgr(Color.Red), 2);
                image.Draw(depthCircle, new Bgr(Color.Yellow), 5);
                //image.Draw(endCircle, new Bgr(Color.DarkBlue), 4);
            }
            #endregion

            MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_DUPLEX, 5d, 5d);
            image.Draw(fingerNum.ToString(), ref font, new Point(50, 150), new Bgr(Color.White));
            hand.fingerChangedCompute = fingerNum;
        storage.clear(); // clear storage allocation

    }

【讨论】:

  • 我照你说的做了,是的,它解决了问题,但是 DrawComputerFingersNum 方法不起作用(它不画任何圆/线并且手指计数不起作用)这是快照i.imgur.com/aQozV.jpg
  • he.. 应用程序在调整存储变量后现在正在运行...我不知道为什么代码运行良好..
  • 存储变量范围存在潜在问题。我很高兴它现在可以工作了。
  • 存储变量范围存在潜在问题您能否进一步扩展一下?它似乎确实解决了这个问题,但我不明白为什么。 @LucaDelTongo
  • @NisargShah 基本上问题可能取决于每次调用函数 DrawAndComputeFingersNum 时分配和释放内存存储内存的方式。在全局范围内,有一种预分配可以防止这个问题。为了得到更深入的解释,我应该看看 memstorage 类及其实现 IDisposable 的方式......我原来的并不总是正常工作,但这不是我的错,这是某种不正确的对象处理的副作用...... .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多