Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to opencv Mat #59

Open
ShrutheeshIR opened this issue Feb 10, 2020 · 3 comments
Open

Convert to opencv Mat #59

ShrutheeshIR opened this issue Feb 10, 2020 · 3 comments

Comments

@ShrutheeshIR
Copy link

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!

@tyongguang
Copy link

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

@doughtmw
Copy link

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);
}

@nistarlwc
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants