gpt4 book ai didi

c++ - 对图片中的像素进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:54 24 4
gpt4 key购买 nike

我有一张图片需要检测形状。为此,我使用了从中提取点的模板,然后尝试将这些点与我的图像相匹配以获得最佳匹配。当我找到最佳匹配时,我需要围绕这些点绘制曲线。

问题是所取的分数没有按顺序排列。 (你可以在图片上看到)。如何对曲线没有“跳跃”但平滑的点进行排序。

enter image description here

我尝试使用 stable_sort() 但没有成功。

stable_sort(points.begin(), points.end(), Ordering_Functor_y());
stable_sort(points.begin(), points.end(), Ordering_Functor_x());

使用stable_sort()

using 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;
}
}

在这部分代码中,您可以看到点顺序的问题在哪里。有没有更好的选择如何以正确的顺序保存点,我不会在绘制轮廓时遇到问题?

最佳答案

您当前的方法是对 x 或 y 值进行排序,这不是绘制轮廓的正确顺序。

一种方法可以是以下

  1. 计算被检测物体的质心
  2. 将极坐标系置于质心
  3. 计算所有点的方向
  4. 按方向坐标对点进行排序

它会比您当前的排序更好,但可能并不完美。

一种更复杂的方法是确定通过所有检测到的点的最短路径。如果这些点均匀分布在轮廓周围,则此方法将定位轮廓。这种方法的搜索词是旅行商问题。

关于c++ - 对图片中的像素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23884668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com