rogersce/cnpy

Convert to opencv Mat

Opened this issue · 3 comments

Thanks for the great library. While I have been able to load a numpy file, how do I convert it to opencv Mat format.
Thanks!

Add cnpy.cpp and cnpy.h to visual stuido, and remove the relevant code of zlib

Here's an example of how to load a 3D matrix from .npy to OpenCV.

void cnpy2opencv_3d(std::string const& data_fname, cv::Mat& out)
{
	// Load the data from file
	cnpy::NpyArray npy_data = cnpy::npy_load(data_fname);

	// Get pointer to data
	float* ptr = npy_data.data<float>();

	// Get the shape of data
	int dim_1 = npy_data.shape[0];
	int dim_2 = npy_data.shape[1];
	int dim_3 = npy_data.shape[2];

	int size[3] = { dim_1, dim_2, dim_3 };
	out = cv::Mat(3, size, CV_32F, ptr).clone();
}

int main()
{
	cv::Mat test_data;
	cnpy2opencv_2d("test_data.npy", test_data);
}

Want to load a image in opencv Mat with c++, and save a .npy file. how to do it?