如何将 dlib 中的矩阵转换为 std::vector

Posted

技术标签:

【中文标题】如何将 dlib 中的矩阵转换为 std::vector【英文标题】:how to convert a matrix in dlib to a std::vector 【发布时间】:2016-12-10 13:22:59 【问题描述】:

我在 dlib 中定义了一个列向量。如何将其转换为 std::vector?

typedef dlib::matrix<double,0,1> column_vector;
column_vector starting_point(4);
starting_point = 1,2,3,4;
std::vector x = ??

谢谢

【问题讨论】:

【参考方案1】:

有很多方法。您可以通过 for 循环复制它。或者使用带有迭代器的 std::vector 构造函数:std::vector&lt;double&gt; x(starting_point.begin(), starting_point.end())

【讨论】:

谢谢。但不应该是 std::vector x(starting_point.begin(), starting_point.end()) 吗?【参考方案2】:

这将是您通常迭代矩阵的方式(如果矩阵只有 1 列则无关紧要):

// loop over all the rows
for (unsigned int r = 0; r < starting_point.nr(); r += 1) 
    // loop over all the columns
    for (unsigned int c = 0; c < starting_point.nc(); c += 1) 
        // do something here
       

那么,为什么不遍历列向量并将每个值引入新的std::vector?这是一个完整的例子:

#include <iostream>
#include <dlib/matrix.h>

typedef dlib::matrix<double,0,1> column_vector;

int main() 
    column_vector starting_point(4);
    starting_point = 1,2,3,4;

    std::vector<double> x;

    // loop over the column vector
    for (unsigned int r = 0; r < starting_point.nr(); r += 1) 
        x.push_back(starting_point(r,0));
    

    for (std::vector<double>::iterator it = x.begin(); it != x.end(); it += 1) 
        std::cout << *it << std::endl;
    

【讨论】:

以上是关于如何将 dlib 中的矩阵转换为 std::vector的主要内容,如果未能解决你的问题,请参考以下文章

了解 Dlib 内核实现

在 DLIB 中将 RGB 图像转换为灰度图像

将 OpenCV Rect 转换为 dlib 矩形?

如何使用opencv将图像转换为Python中的矩阵

如何将矩阵转换为 R 中的列向量列表?

如何将 4 行数据帧转换为 R 中的 2*2 矩阵?