【问题标题】:How to sort 2D Points in a 2D-Raster如何对二维栅格中的二维点进行排序
【发布时间】:2020-06-26 21:40:23
【问题描述】:

我有很多未排序的 2D 点,它们代表图像中随机选取的像素的位置。

在下一步中,我尝试在 2D 数组中对它们进行排序/光栅化,按 x、y 值最小的点

array[0][0] 

和在

处具有最高 x,y 值的点
array[n][k]

条件1:

所有其他 2D 都应在此边界之间并且几乎已排序。

条件2:

数组的所有行都应该填充几乎相同数量的值,列也一样。

任何想法如何解决这个问题?

我计算了 delaunay-triangulation 并考虑了一个 voronoi 图,用于逐步抛出每个单元格,但我不知道我是否走在正确的道路上。

我的随机位置就是这样创建的:

std::vector<Point_d> sample_rand_points(){
  std::cout<<"sampling random points\n";
  std::vector<Point_d> output_pattern;

  //PREPARE:
  std::vector<std::pair<int, int> > not_sampled_yet;
  for(int x=0; x<_X; x++)
  {
    for(int y=0; y<_Y; y++)
    {
      not_sampled_yet.push_back(std::pair<int,int>(x,y));
    }
  }
  //SAMPLING
  Point_d pix;
  for (int i=0; i<_Amount; i++)
  {
    //std::cout<<i<<"\n";
    int n= rand()% not_sampled_yet.size();
    pix.x= (double)not_sampled_yet[n].first;
    pix.y= (double)not_sampled_yet[n].second;
    not_sampled_yet[n]=not_sampled_yet.back();
    not_sampled_yet.pop_back();
    output_pattern.push_back(pix);
  }
  return output_pattern;

}

输出是一个向量,点为 {{x1,y1},{x2,y2},......}

【问题讨论】:

  • 输入数据(2D点)是如何生成的?
  • 我插入了一些随机函数的代码
  • 乍一看,将 x 和 y 坐标存储在两个单独的 std::vector&lt;double&gt; 中是有意义的。这样更容易找到最小值和最大值。
  • 我选择了这种数据格式作为 delaunay 库的输入。一开始我是按照我现在插入的方式编写的。
  • 随机选择的像素数量约为原始 Pixelammount 的 60-70%。最小值始终为 x= 0 或 y= 0 且最大值为高度和高度的点。

标签: c++ algorithm sorting pixel delaunay


【解决方案1】:

使用以下代码,您可以生成具有随机像素的“二维数组”。 在我看来,没有必要使用像sample_rand_points 这样的生成器函数。

#include <iostream>
#include <vector>
#include <cstdlib>

int main()
{
    int NumX =10, NumY =20;
    std::vector<std::vector<bool>> data(NumX, std::vector<bool>(NumY));

    for(int i=0; i<NumX ; ++i)
    {
        for(int m=0; m<NumY ; ++m)
        {
            //Generation
            bool val = false;
            if(rand() % 2 == 0)
                val = true;

            //The Data
            data[i][m] = val;

            //Output
            if(val)
                std::cout << "*";
            else
                std::cout << "_";

        }
        std::cout << std::endl;
    }
}

You can run the above code online 查看以下输出:

_*____**__*_*__*****
_*__***____***___*_*
____*_**_*_*_**_***_
__*_*_*___*_*_*_**_*
_*****__*_****_****_
_***___*_***_**___*_
*__________*__*_*__*
*_*___***_*_*_*_*___
_***_*_*****_*_____*
*__*__*_*_____**__*_

请注意,if(rand() % 2 == 0) 行控制“选定”像素的密度。

【讨论】:

  • 我不明白这与我的问题有什么关系。我有数据类型 Pixel,包含位置和颜色信息,并希望在 2D 数组中对它们进行排序
  • 好吧,我不明白你为什么要实现sample_rand_points这个函数。
  • 您的函数为每个像素随机选择真或假。我有离散数量的随机像素,我想恢复为光栅格式。
  • 是的,你的方法不同。但最终的结果是一样的。我想了解您为什么需要生成点 {{x0,y0},{x1,y1},......}?
猜你喜欢
  • 2013-08-17
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 2013-09-13
  • 2013-07-03
  • 1970-01-01
  • 2017-05-25
  • 2015-09-17
相关资源
最近更新 更多