numpy npy to C++ Eigen

If you are dumping data into numpy npy and later on want to use them in c++, here’s my advice: use cnpy

Build Instruction

Since this cnpy library is pretty lightweighted, you can easily do like this:

git clone https://github.com/rogersce/cnpy.git
cd cnpy
cp cnpy* /your/project/src/

and apply the following lines in your CMakeList.txt file:

create the library:

add_library(cnpy SHARED "cnpy.cpp")

link to target:

target_link_libraries(your_program cnpy)

Load Data

Then include the cnpy head in your cpp file:

#include "cnpy.h"

load your npy file:

cnpy::NpyArray npydata = cnpy::npy_load(npy_fname);

define the data pointer according to your npy data saved in python end:

double* data;
data = npydata.data<double>();

Now the pointer data is pointing to your npy array, feel free to convert into CV::Mat or Eigen::MatrixXd using the following function:

void cnpy2eigen(string data_fname, Eigen::MatrixXd& mat_out){
    cnpy::NpyArray npy_data = cnpy::npy_load(data_fname);
    // double* ptr = npy_data.data<double>();
    int data_row = npy_data.shape[0];
    int data_col = npy_data.shape[1];
    double* ptr = static_cast<double *>(malloc(data_row * data_col * sizeof(double)));
    memcpy(ptr, npy_data.data<double>(), data_row * data_col * sizeof(double));
    cv::Mat dmat = cv::Mat(cv::Size(data_col, data_row), CV_64F, ptr); // CV_64F is equivalent double
    new (&mat_out) Eigen::Map<Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>>(reinterpret_cast<double *>(dmat.data), data_col, data_row);
}

Here we allocated a memory in heap for npy_data to keep our referenced data from corruption when function was pop out of stack later.

Written on November 13, 2017