【发布时间】:2014-05-27 08:35:26
【问题描述】:
我有需要检测形状的图片。为此,我使用了模板,我从中对点进行了炮击,然后尝试将此点与我的图像相匹配以实现最佳匹配。当我找到最佳匹配时,我需要围绕这些点绘制曲线。
问题是得分不按顺序排列。 (你可以在图片上看到)。 如何排序曲线不会“跳跃”但平滑的点。
我尝试使用stable_sort(),但没有成功。
stable_sort(points.begin(), points.end(), Ordering_Functor_y());
stable_sort(points.begin(), points.end(), Ordering_Functor_x());
使用stable_sort()
我使用了这个函数来绘图:
polylines(result_image, &pts, &npts, 1, true, Scalar(0, 255, 0), 3, CV_AA);
知道如何解决这个问题吗?谢谢。
编辑: 这是从模板中获取积分的代码
for (int model_row = 0; (model_row < model.rows); model_row++)
{
uchar *curr_point = model.ptr<uchar>(model_row);
for (int model_column = 0; (model_column < model.cols); model_column++)
{
if (*curr_point > 0)
{
Point& new_point = Point(model_column, model_row);
model_points.push_back(new_point);
}
curr_point += image_channels;
}
}
在这部分代码中,您可以看到点顺序的问题在哪里。有没有更好的选择如何以正确的顺序保存点,我不会在绘制轮廓时遇到问题?
【问题讨论】:
-
什么不正常?您的模板点或检测到的点?最简单的方法是按顺序“掏出”您的模板点。否则,总的来说,我认为您需要使用比排序更高的方法来提取轮廓。您可以尝试从“凸包”开始,并使用一些启发式方法来处理凹面。
-
另一种启发式方法:从某一点开始。找到最近的邻居。画一条线到那个邻居并删除/忽略第一个点。找到离第二个点最近的邻居,画线并删除第二个点。等等。
-
@djpiky 查看答案here 可能会有所帮助...
-
你知道 OpenCV 有一个drawContours 函数,对吧?