【发布时间】:2020-02-20 17:25:56
【问题描述】:
我想知道是否有人可以阐明一种能够基于每帧对轮廓进行排序的策略。
我正在尝试检测“事件” - 在这种情况下,事件被定义为 4 帧的运动增长。
如果轮廓在连续 4 帧中“生长”/具有较大的轮廓区域,则会记录一个事件,并且我必须存储并输出第一帧生长的轮廓的中心位置。
如果只有一个事件要检测,我可以通过对轮廓区域列表执行成对检查来粗略地检测事件的起源,如果这是真的,通过取 (currentFrameNo - 4) 位置元素轮廓位置列表。
但是,尝试检测多个事件似乎完全不同。
在任何给定的帧上,可能会找到 (n) 个轮廓。每个轮廓都被传递到一个候选对象中,具有表征轮廓的属性,例如帧数、位置和轮廓大小。
最终,我需要一种按帧对这些轮廓进行排序的方法,这样我就可以根据它们的相对位置来组织它们,然后对轮廓的“正确列表”执行成对检查。
我不确定我是否需要多个 (4+) 列表,一个用于每个可能的事件,然后在每一帧上根据最近的中心位置将候选人传递到一个单独的列表中,或者我是否应该继续将它们添加到单个列表,然后查询该列表。
我希望在使用 linq/排序集合方面有更多经验的人可以帮助确定合适的方法。
感谢您抽出宝贵时间阅读这篇文章。
public class CandidateList
{
public List<Candidate> candidates;
public CandidateList()
{
candidates = new List<Candidate> candidates;
}
public void Add(Candidate candidate)
{
candidates.Add(candidate)
}
}
public class Candidate
{
//Attributes shown in constructor.
public Candidate(VectorOfPoint contour, int frameNumber, double contourSize, Point location)
{
Contour = contour;
FrameNumber = frameNumber;
ContourSize = contourSize;
Location = location;
Location_x = Location.X;
Location_y = Location.Y;
}
}
_vc = new VideoCapture(someURLorFilePath);
_candidates = new CandidateList();
_vc.ImageGrabbed += ProcessFrame;
public void ProcessFrame(object sender, EventArgs e)
{
Mat _frame = new Mat();
// read frame.. + other operations to get desired data.
Mat _contourOutput = _frame.Clone();
VectorOfVectorOfPoint _contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(_contourOutput, _contours, new Mat(), RetrType.External, ChainApproxMethod.ChainApproxSimple);
// If there are any contours
if (_contours.Size > 0)
{
// Iterate through contours
for (var i = 0; i < _contours.Size; i++)
{
// Find contour area of each contour (VectorOfPoint)
double _contourArea = CvInvoke.ContourArea(_contours[i]);
// Find centre of contour
Moments M = CvInvoke.Moments(_contours[i]);
Point _contourCentre = new Point(Convert.ToInt16(M.M10 / M.M00), Convert.ToInt16(M.M01 / M.M00));
// Create a candidate based on frame number, contourSize and location
Candidate _candidate = new Candidate(_contours[i], _currentFrameNo, _contourArea, _contourCentre);
_candidates.Add(_candidate)
}
}
_currentFrameNo ++
}
以下图片描述了我必须处理的一种非常可能的情况:
第 1 帧 - 四个候选人。
第 2 帧 - 四个候选者,位置略有偏移
第 3 帧 - 四个候选者,位置略有偏移
第 4 帧 - 四个候选者,位置偏移 检测到两个事件。 从第 1 帧检索中心位置。
【问题讨论】:
标签: c# .net linq sorting opencv