Skip to content

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

Open
Open
@pythondever

Description

@pythondever

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions