首页 > 解决方案 > 为什么 VectorOfKeyPoint 和 Mat 数组会被新的 SIFT 检测器结果覆盖?

问题描述

在执行期间PictureBox1_Click(object sender, EventArgs e)

using Emgu.CV;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        static VectorOfKeyPoint _templateKeyPoints = new VectorOfKeyPoint();
        static Mat _templateDescriptor = new Mat();
        VectorOfKeyPoint[] templateKeyPoints = new VectorOfKeyPoint[2];
        Mat[] templateDescriptor = new Mat[2];
        Bitmap[] croppedImage = new Bitmap[2];
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Load("https://cdn.pixabay.com/photo/2015/12/01/20/28/road-1072823_960_720.jpg");
            pictureBox2.Load("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg");
            croppedImage[0] = (Bitmap)pictureBox1.Image.Clone();
            croppedImage[1] = (Bitmap)pictureBox2.Image.Clone();
        }

        private void PictureBox1_Click(object sender, EventArgs e)
        { 
            for (int i = 0; i < 2; i++)
            {
                try
                {
                    var processTemplate = ProcessTemplate(croppedImage[i].ToImage<Gray, byte>());
                    templateKeyPoints[i] = processTemplate.tempKeyPoints;
                    templateDescriptor[i] = processTemplate.tempDescriptor;
                }
                catch(Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
               
            }
        }        

        public static (VectorOfKeyPoint tempKeyPoints, Mat tempDescriptor)
            ProcessTemplate(Image<Gray, byte> template)
        {
            try
            {
                SIFT detector = new SIFT();
                detector.DetectAndCompute(template, null, _templateKeyPoints, _templateDescriptor, false);

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            return (_templateKeyPoints, _templateDescriptor);
        }
    }
}

标签: c#arraysformsvisual-studioemgucv

解决方案


我解决了这个问题。VectorOfKeyPoint这是因为每当我调用ProcessTemplate()方法时,我都没有创建一个新的“Mat”实例。所以我移动VectorOfKeyPoint _templateKeyPoints = new VectorOfKeyPoint();Mat _templateDescriptor = new Mat();方法如下:

        public static (VectorOfKeyPoint tempKeyPoints, Mat tempDescriptor)
            ProcessTemplate(Image<Gray, byte> template)
        {
            VectorOfKeyPoint _templateKeyPoints = new VectorOfKeyPoint();
            Mat _templateDescriptor = new Mat();
            try
            {
                SIFT detector = new SIFT();
                detector.DetectAndCompute(template, null, _templateKeyPoints, _templateDescriptor, false);

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            return (_templateKeyPoints, _templateDescriptor);
        }

推荐阅读