I have 3D Mat and would like to convert it to Vector. I tried opencv's reshape() function but it seems to not work with matrices that have dimensions more than 2. How can I convert to 3D Mat to Vector ? I can do it by accessing all elements in the Mat. Is there any efficient way?
相关问题
- How to get the background from multiple images by
- Is GLFW designed to use without LWJGL (in java)?
- Try to load image with Highgui.imread (OpenCV + An
- CV2 Image Error: error: (-215:Assertion failed) !s
- Is it a bug of opencv RotatedRect?
相关文章
- Algorithm for partially filling a polygonal mesh
- How do I get characters common to two vectors in C
- Robust polygon normal calculation
- Keep constant number of visible circles in 3D anim
- How can I unpack (destructure) elements from a vec
- opencv fails to build with ipp support enabled
- Code completion is not working for OpenCV and Pyth
- Face unlock code in Android open source project?
Reshape should work. A sample below.
If we have the following 3D matrix:
This only works for continuous mat objects (i.e. m.isContinuous() == true)
To get a vector of the same type containing the same elements:
Using the overloaded STL vector constructor:
Declaring and initializing a vector of same type as the matrix and copying all mat elements:
Using OpenCV's reshape:
This differs from the first solution in how the elements are laid out in the end. Originally from this post: How to divide 3D matrix into bunches of 2D matrix with opencv C++ I second his suggestion on defining your dimensions as channels if possible. OpenCV handles channels better than dimensions. Of course, that might not be applicable if you intended to have more than 4 dimensions.
With both solutions all elements are accounted for except that they're ordered differently and thus accessed differently. In the first you access elements at row, col, plane via
In the second you're elements are ordered by plane.
Picking which solution probably depends on how you'll be indexing the vector.
Another variant but one that, as per the real problem, starts with 2D arrays and then uses the 3d-given-already-allocated-data form of the Mat constructor to affect a reshape by giving a 3d slice view of the 2d matrices to satisfy copyTo (as both source and arguments will have the same dimensions 3x4x1 (as given by aslicesizes below)