c# - 为什么 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);
}
}
}
在第一次迭代期间:
何时填充
templateKeyPoints[0]
,templateDescriptor[0]
我得到正确的结果。在第二次迭代期间:
processTemplate.tempKeyPoints
来自和 的新结果分别processTemplate.tempDescriptor
被覆盖到 和 的第一个元素templateKeyPoints[i]
(templateDescriptor[i]
不是根据需要),并且还被填充到它们各自的第二个元素(根据需要)。请问为什么会发生这种覆盖?
解决方案
我解决了这个问题。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);
}
推荐阅读
- docker - nginx html文件夹内的docker无法创建挂载
- ruby-on-rails - 升级到 Rails 5.2:如何避免非可选关联的“可选:真”?
- visual-studio - 如何修复'无效的资源目录名称
Xamarin.Android 中的错误? - javascript - (Handsontable) - hot.setCellMeta 不起作用
- r - opts_knit$set(unnames.chunk.label = "figs") 不会更改默认标签
- web-applications - 为什么 java.exe 由于“EnhancedQueueExecutor.java”线程而消耗更多 CPU?
- javascript - 如何检测 URL 是否在固定 URL 之后包含单词?
- c# - 如何正确处理此 dbnull 异常?
- r - 将散点图从 PDF 导入 R
- android - 当我从通知切换回应用程序时,为什么媒体播放器会多次播放媒体?