VoteforSizeandOrientation和Cam捕获错误EMGUCV C#

时间:2016-01-01 08:54:34

标签: c# .net opencv emgucv

我是 EMGU CV OpenCV 编程的新手,所以我可能犯了一些愚蠢的错误。我正在编写一个程序来检测仓库中具有不同徽标的一组公司的特定徽标。相机将自上而下定向。我已经开始简单地使用 EMGU CV wiki中提供的示例了,我正在尝试使用带有FAST Extractor的Brief描述符,因为SURF太慢了。我收到两个错误。第一个是这个。

  

未处理的类型' System.AccessViolationException'发生在Emgu.CV.dll

     

附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

它出现在这行代码中

        observerframe  = capture.QueryGrayFrame();

下一个错误就是这个

  

未处理的类型' System.Runtime.InteropServices.SEHException'发生在Emgu.CV.dll

     

其他信息:外部组件引发了异常。

它出现在这行代码中

    nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(ObjectKeypoints, SceneKeypoints, indices, mask, 1.5, 20);

我的完整代码在下面提供

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows;


using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.Features2D;
using Emgu.CV.Util;
namespace train
{
    public partial class Detectingwindow : Form
    {
        public Bitmap testobject;
        public Capture capture;
        public Image<Gray, Byte> observerframe;
        bool captureinprogress;
        bool detect = false;

        public Detectingwindow()
        {
            InitializeComponent();

        }

        private void matcher_Load(object sender, EventArgs e)
        {
        }

        private void camcapture(object sender, EventArgs e)
        {
            capture = new Capture(); 
            observerframe  = capture.QueryGrayFrame();

            if (observerframe != null)
            {
                Image<Gray, Byte> objectbnw = new Image<Gray, byte>(testobject);
                Image<Bgr, Byte> output = Draw(objectbnw, observerframe);
                imageBox1.Image = output;
            }
            return;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            #region if capture is not created, create it now
            if (capture == null)
            {
                try
                {
                    capture = new Capture();
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
            #endregion

            if (capture != null)
            {
                if (captureinprogress)
                {  //if camera is getting frames then stop the capture and set button Text
                    // "Start" for resuming capture
                    button1.Text = "Restart"; 
                    Application.Idle -= camcapture;
                }
                else
                {
                    //if camera is NOT getting frames then start the capture and set button
                    // Text to "Stop" for pausing capture
                    button1.Text = "Pause";
                    Application.Idle += camcapture;


                }

                captureinprogress = !captureinprogress;
            }
        }
        public static Image<Bgr, Byte> Draw(Image<Gray, Byte> testobject, Image<Gray, byte> scene)
        {

                Image<Bgr, Byte> Draw;
                HomographyMatrix H = null;
                FastDetector detector = new FastDetector(10, true);
                VectorOfKeyPoint ObjectKeypoints, SceneKeypoints;
                Matrix<int> indices;
               // Freak extractor = new Freak(true, true, 22.0f, 4);
               BriefDescriptorExtractor extractor = new BriefDescriptorExtractor();
                Matrix<byte> mask;


                ObjectKeypoints = detector.DetectKeyPointsRaw(testobject, null);
                Matrix<Byte> ObjectDescriptors = extractor.ComputeDescriptorsRaw(testobject, null, ObjectKeypoints);

                SceneKeypoints = detector.DetectKeyPointsRaw(scene, null);
                Matrix<Byte> SceneDescriptors = extractor.ComputeDescriptorsRaw(scene, null, SceneKeypoints);

                BruteForceMatcher<Byte> matcher = new BruteForceMatcher<byte>(DistanceType.Hamming);
                matcher.Add(SceneDescriptors);

                indices = new Matrix<int>(SceneDescriptors.Rows, 2);
                using (Matrix<float> dist = new Matrix<float>(SceneDescriptors.Rows, 2))
                {
                    matcher.KnnMatch(SceneDescriptors, indices, dist, 2, null);
                    mask = new Matrix<byte>(dist.Rows, 1);
                    mask.SetValue(255);
                    Features2DToolbox.VoteForUniqueness(dist, 0.8, mask);
                }

                int nonZeroCount = CvInvoke.cvCountNonZero(mask);

                if (nonZeroCount >= 4)
                {
                    nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(ObjectKeypoints, SceneKeypoints, indices, mask, 1.5, 20);
                    if (nonZeroCount <= 4)
                    {
                        H = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(ObjectKeypoints, SceneKeypoints, indices, mask, 2);
                    }
                }

                Draw = Features2DToolbox.DrawMatches(testobject, ObjectKeypoints, scene, SceneKeypoints, indices, new Bgr(255, 255, 255), new Bgr(255, 255, 255), mask, Features2DToolbox.KeypointDrawType.NOT_DRAW_SINGLE_POINTS);
                if (H != null)
                {
                    Rectangle rect = testobject.ROI;
                    PointF[] pts = new PointF[] {
         new PointF(rect.Left, rect.Bottom),
         new PointF(rect.Right, rect.Bottom),
         new PointF(rect.Right, rect.Top),
         new PointF(rect.Left, rect.Top)};
                    H.ProjectPoints(pts);
                    Draw.DrawPolyline(Array.ConvertAll<PointF, Point>(pts, Point.Round), true, new Bgr(Color.Red), 5);
                }
                return Draw;


        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

很抱歉没有正确更正缩进等或评论。真的很感激任何帮助。

由于

0 个答案:

没有答案