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

[BUG]: cv::Mat to numpy #5347

Open
2 of 3 tasks
pythondever opened this issue Sep 1, 2024 · 1 comment
Open
2 of 3 tasks

[BUG]: cv::Mat to numpy #5347

pythondever opened this issue Sep 1, 2024 · 1 comment
Labels
triage New bug, unverified

Comments

@pythondever
Copy link

Required prerequisites

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 #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

@pythondever pythondever added the triage New bug, unverified label Sep 1, 2024
@pythondever
Copy link
Author

I'm trying to convert vector to python bytes got same runtime exception:

Exception 0xc0000005 encountered at address 0x000000: User-mode data execution prevention (DEP) violation at location 0x00000000

#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;

std::vector<uint8_t> getImageData() {
    const unsigned int width = 100; //
    const unsigned int height = 100; //
    std::vector<uint8_t> imageData(width * height); //
    for (size_t i = 0; i < imageData.size(); ++i) {
        imageData[i] = 0;
    }
    return imageData;
}

py::bytes get_frame() {
    auto image = getImageData();
    return py::bytes(reinterpret_cast<char*>(image.data()), image.size()); //
}

int main() {
    auto frame = get_frame(); // debug
    return 0;
}

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

No branches or pull requests

1 participant