[BUG]: cv::Mat to numpy
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.11.1
Problem description
Hi everyone I'm a newbie learning C++ and I am trying to convert c++ cv::Mat into python numpy, when I runing code, An exception was encountered. return code: code -1073741819 (0xC0000005) and no more detail
I also tried this solution: https://github.com/pthom/cvnp and https://github.com/pybind/pybind11/issues/538 but got same exception
How can I solve this problem?
env info:
windows10 msvc 2022 opencv4.5.5 pybind11 2.11.1 python 3.8.10
Reproducible example code
#include <iostream>
// #include "cvnp/cvnp.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
using namespace std;
namespace py = pybind11;
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat img = imread("E:/code/cpp_cv/1.jpg",0);
cout << "into read...." << endl;
if (img.empty()) {
cout << "cannot read image...." << endl;
return 0;
}
if (img.isContinuous() != 1) {
cout << "cannot read image.... is not continuous()" << endl;
return 0;
}
cout << "transform image...." << endl;
Mat clone_img = img.clone();
// cvnp::mat_to_nparray(img);
py::array_t<unsigned char> dst = py::array_t<unsigned char>({ img.rows, img.cols }, clone_img.data);
return 0;
}
Is this a regression? Put the last known working version here if it is.
Not a regression
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the standalone main() reproducer, especially the py::array_t construction from clone_img.data, and verify it against the stated Windows, MSVC, OpenCV, pybind11, and Python versions. No project files or tests are named; a useful outcome would be a minimal verified reproduction with a clear diagnosis or expected behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- api, computer-vision
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100