-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Ask opencv mat numpy array conversion #538
Comments
What's the logic here? Strides are already expressed in bytes. That's probably the cause of your errors. Besides, note that a lot of your code is not outdated / deprecated, there's new/better ways to do these things. Also, you may not need to do a
Direct constructors from if (!isinstance<array>(src))
return false;
auto arr = reinterpret_borrow<array>(src);
const size_t* shape = arr.shape();
const size_t* strides = arr.strides();
int ndim = arr.ndim();
arr.mutable_data() /* or arr.data() if you only need a const pointer */ |
thx for your answer i'll test it soon. Just to complete my solution finally it work with small modifications (see below).
PS: I'm aware my code use deprecated things but i don't find enough help on the non deprecated solutions. working code :
|
@aldanor I try your modification but i'm not able to find a equivalent of : |
Yes, what I think happens is this: the array is non-contiguous, and as such the data pointer stops making much sense. @wjakob ^ This is quite closely related to flags rework, how do you think cases like this should be solved, ideally/hypothetically? If instead of |
@edmBernard There's Maybe we should properly expose this so that |
When i try this but there is an awfull solution : |
I put my 2 versions on git here : |
I close as my solution finally work even if it not really clean |
I've modified @edmBernard 's sample project and replaced the converter with extracts from https://github.com/yati-sagade/opencv-ndarray-conversion ... which really just copies OpenCV's internal Ultimately, I'd like to add a capsule to OpenCV that exports it's conversion routines so that anyone can use them, instead of just using it internally. Would make this sort of thing a lot easier. Won't be able to finish that for awhile, but WIP for that at virtuald/opencv@6db6d29 Example project is at https://github.com/virtuald/pybind11_opencv_numpy, check it out, it seems to work so far. |
@virtuald Using OpenCV's code is indeed probably the best approach. When I first started hacking the conversion, that was my main idea, but OpenCV doesn't make using just the conversion too easy. In fact its python bindings are such magic I couldn't even start to guess where to look. |
@jampekka As I said above, I've extracted the necessary bits from OpenCV's python bindings and incorporated them into the example project above, you can find them at https://github.com/virtuald/pybind11_opencv_numpy/blob/master/ndarray_converter.cpp . |
@virtuald thanks for your work, That's a really better solution. |
Is it not better to try to merge this solution in the repository? |
@bhack the real solution is adding a capsule to OpenCV for this. It probably doesn't belong in pybind11. |
Capsule? |
See virtuald/opencv@6db6d29 for an initial (crashing) implementation of a capsule, similar to how one imports numpy from a C/C++ project. |
@vpisarev What do you think? Could it be upstreamed in opencv? |
Gently ping @vpisarev |
I try to add an Openv Numpy interface with
type_caster
I start from @jampekka comments here
I ended to this :
The cast part work, i'm able to transfert cv::Mat from C++ to numpy array in python.
but the load part is completely broken there is buffer issues. it seem strides were bad but I don't find how to fixe them.
have you an idea to fixe that ? thx
The text was updated successfully, but these errors were encountered: