【问题标题】:Mapping value to pixel pairs - How to implement the data structure将值映射到像素对 - 如何实现数据结构
【发布时间】:2015-12-10 13:45:54
【问题描述】:

我有一个函数 f(x,y),其中 x 和 y 是像素坐标(就像在 OpenCV cv::Point(i,j) 中一样),它将浮点值映射到每个像素对 (x,y)。我的问题是:表示此映射的正确数据结构是什么?我想要一个类似具有多种类型的数组的东西,这是标准数组不可能的。我想在下面的例子中使用它:

mat[cv::Point(i,j)][cv::Point(k,l)] = 5.0f;

我将不胜感激。

【问题讨论】:

  • 所以你用的不是OpenCV,而是你自己的数组?
  • 我正在使用 OpenCV,但在这种情况下,当涉及到点对索引时,我不知道如何使用矩阵。
  • 所以你想要一个超过 2 维的垫子?或者您认为什么是像素对? x 和 y 是 2 点,所以 x 有 x 和 y 并且 y 有 x 和 y 坐标,还是什么?

标签: c++ arrays opencv mapping


【解决方案1】:

您只需要像素所在屏幕的width。 这样的功能很容易实现,因为width * height等于像素的数量,你可以写x + y * width来表示地图上的任何像素。

【讨论】:

    【解决方案2】:

    可以使用std::map

    std::map< Point, std::map< Point, float > > map;
    map[Point(0,0)][Point(0,0)] = 5.0f;
    

    但要这样做,Point 需要一个 operator&lt;

    struct Point
    {
      Point(const size_t _x, const size_t _y) : x(_x), y(_y) {}
      size_t x, y;
    
      bool operator<(const Point& rhs) const
      {
        // first of all, order regarding x
        if ( x < rhs.x ) return true;
        if ( x > rhs.x ) return false;
        // if x is equal, order regarding y
        if ( y < rhs.y ) return true;
        return false;
      }
    };
    

    您当然可以使用find()来检查地图中是否有元素Point(0,0)

    if( map.find(Point(0,0)) == map.end() ){ /*Element not found*/ }
    

    如果您可以使用 c++11,std::unorderd_map 也是一个选项,因为您不需要 operator&lt; 用于键类。


    您甚至可以使用std::pair&lt;Point,Point&gt;作为地图的键。


    另一种选择是使用这样的 4D 数组:

    mat[i][j][k][l] = 5.0f;
    

    【讨论】:

    • 将 (2,3) 放入地图并发现 (3,2) 也存在可能会令人惊讶。
    • 为什么会这样? (2,3) 和 (3,2) 是完全不同的位置。
    • 根据您的订购,它们是相同的。
    【解决方案3】:

    正如@Gombat 已经提到的,一个简单的方法是使用std::map,并使用std::pair&lt;cv::Point, cv::Point&gt; 作为键。

    您需要。但是要提供自定义比较器,因为 cv::Point 不提供 operator&lt;

    看看这段代码:

    #include <opencv2/opencv.hpp>
    #include <map>
    using namespace cv;
    using namespace std;
    
    bool lessPoints(const Point& lhs, const Point& rhs)
    {
        return (lhs.x == rhs.x) ? lhs.y < rhs.y : lhs.x < rhs.x;
    }
    
    struct lessPairPoints
    {
        bool operator()(const pair<Point, Point>& lhs, const pair<Point, Point>& rhs) const
        {
            return (lhs.first == rhs.first) ? lessPoints(lhs.second, rhs.second) : lessPoints(lhs.first, rhs.first);
        }
    };
    
    typedef map<pair<Point, Point>, float, lessPairPoints> MapPoints;
    
    int main()
    {
        MapPoints map1;
    
        map1[{Point(0, 0), Point(1, 1)}] = 0.3;
        map1[{Point(1, 2), Point(1, 1)}] = 0.1;
    
        for (const auto& el : map1)
        {
            cout << el.first.first << ", " << el.first.second << " -> " << el.second << endl;
        }
    
        cout << map1[{Point(0,0), Point(1,1)}] << endl;
    
        auto pp = make_pair(Point(1,2), Point(1,1)); 
        cout << map1[pp] << endl;
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 2020-09-30
      • 2021-02-11
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多