访问颜色直方图openCV中的尺寸

时间:2014-09-29 16:02:27

标签: c++ opencv multidimensional-array histogram channel

我是openCV的新手,这是我对矩阵维度主题的第一个怀疑。

我通过函数 cv :: calcHist(..)计算彩色图像的直方图。

正如我所料,得到的矩阵是一个3D矩阵。我想第三个维度意味着每个RGB颜色通道的颜色,但我不知道如何访问它们。在计算之后,我有一个3维和1个通道的矩阵,我希望能够访问每个维度。

我认为分割函数在这里可以提供帮助,因为它只将矩阵分割成它的通道。

调试我从3Dhistrogram矩阵中获得以下相关信息:

尺寸:3, 行:-1, 列:-1, 大小:256

我知道我可以通过先将图像分成3个通道然后计算每个通道的1D直方图来获得单独的颜色直方图,但我很想知道openCV中维度的工作方式。

提前致谢!

1 个答案:

答案 0 :(得分:0)

这是来自Opencv关于MatND类的参考:

   // return pointer to the element (versions for 1D, 2D, 3D and generic nD cases)
    uchar* ptr(int i0);
    const uchar* ptr(int i0) const;
    uchar* ptr(int i0, int i1);
    const uchar* ptr(int i0, int i1) const;
    uchar* ptr(int i0, int i1, int i2);
    const uchar* ptr(int i0, int i1, int i2) const;
    uchar* ptr(const int* idx);
    const uchar* ptr(const int* idx) const;

    // convenient template methods for element access.
    // note that _Tp must match the actual matrix type -
    // the functions do not do any on-fly type conversion
    template<typename _Tp> _Tp& at(int i0);
    template<typename _Tp> const _Tp& at(int i0) const;
    template<typename _Tp> _Tp& at(int i0, int i1);
    template<typename _Tp> const _Tp& at(int i0, int i1) const;
    template<typename _Tp> _Tp& at(int i0, int i1, int i2);
    template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
    template<typename _Tp> _Tp& at(const int* idx);
    template<typename _Tp> const _Tp& at(const int* idx) const; 

因此,您可以使用3个元素的数组作为.at方法的参数来设置所需的元素位置。