【发布时间】:2023-03-10 19:48:02
【问题描述】:
我有一个binary image:
在这张图片中,我可以使用重载的std::sort 轻松地从上到下和从左到右对找到的轮廓进行排序。
我首先通过以下方式从上到下排序:
sort(contours.begin(), contours.end(), top_to_bottom_contour_sorter());
然后我从左到右排序:
for (int i = 0; i < contours.size(); i = i + no_of_contours_horizontally)
{
sort(i, i + no_of_contours_horizontally, left_to_right_contour_sorter);
}
top_to_bottom 和 left_to_right 是我传递给排序函数的单独函数。而no_of_contours_horizontally 相对于第一张图片是三 (3)。
但是,这只有在我知道水平轮廓的数量时才有效。如果我使用的图像将具有不同数量的水平轮廓,就像这张图片一样。 contours_sample。程序失败。我可以蛮力并定义特定索引以更改找到的轮廓数。但是,它会限制程序对特定输入进行操作,而不是灵活。我正在考虑创建可以覆盖在图像顶部的矩形或线条,并计算内部轮廓的数量,以便获得水平轮廓数量的值。如果有更优雅的解决方案,我将不胜感激。
这是我的排序功能
bool top_to_bottom_contour_sorter(const std::vector<Point> &lhs, const std::vector<Point> &rhs)
{
Rect rectLhs = boundingRect(Mat(lhs));
Rect rectRhs = boundingRect(Mat(rhs));
return rectLhs.y < rectRhs.y;
}
bool left_to_right_contour_sorter(const std::vector<Point> &lhs, const std::vector<Point> &rhs)
{
Rect rectLhs = boundingRect(Mat(lhs));
Rect rectRhs = boundingRect(Mat(rhs));
return rectLhs.x < rectRhs.x;
}
编辑 这是我当前的输出和每个图像的期望输出。 使用第一张图片和我当前的工作代码。 Current_Output
第二张图片我想要的输出。 Desired_Output
【问题讨论】:
-
就在我的脑海中。我会做类似线扫描算法的事情。您从上到下开始“扫描”线并找到该线与先前找到的轮廓的交点,然后您从左到右对它们进行排序,因为您现在知道它们中有哪些以及有多少。
-
对于每个轮廓,计算boundingRect,然后按相应矩形的
x和y值排序。我们是否很快完成了您的调查评估程序? ;-) -
@HansHirse,差不多了 ;-)。这就是我目前正在做的事情。
-
好的,那么,您能否在第二个示例中提供所需的轮廓排序顺序?如果您一次考虑所有轮廓,则必须定义 - 例如 - 如果我们先从左到右还是先从上到下。而且,这意味着,我们需要一个分拣机来完成这项任务!或者,您可以单独考虑轮廓组(列、行)。
-
我做的是先从上到下再从左到右排序。尝试同时使用两个坐标但失败了。