cv::Mat::reshape does not work anymore in 2.4.0! :( (Bug #1942)
Description
Hi, yesterday I upgraded from OpenCV 2.3.1 to 2.4.0 and this resulted in breaking up my code at a very basic level, that is cv::Mat::reshape.
It does not work anymore because it rises this very odd error:
OpenCV Error: The function/feature is not implemented () in reshape, file /home/vito/SourceFolders/OpenCV-2.4.0/modules/core/src/matrix.cpp, line 2891 terminate called after throwing an instance of 'cv::Exception' what(): /home/vito/SourceFolders/OpenCV-2.4.0/modules/core/src/matrix.cpp:2891: error: (-213) in function reshape Annullato (core dump creato)
I was just computing (correctly up to last release) a n by 3 matrix centroid!!!
The code that causes this issue is like this:
cv::Mat A = (cv::Mat_<float>(18,3)<< 3.4884074, 1.4159607, 0.78737736, 2.3456569, -0.88010466, 0.3009364, 2.2710831, -0.81703609, -0.035067264, 4.2535844, 1.639971, -0.050906904, 2.3350885, -0.95192128, -0.0438799, 2.1784587, -0.44972348, -0.030304601, 2.0178607, -0.37849408, -0.36985111, 5.4491558, 1.6346338, 1.47364, 2.0937307, -0.55370253, -0.61064941, 2.2241848, -0.77611613, -0.65611577, 2.2629843, -0.73538947, -0.061620831, 6.0119362, 2.1610513, -0.35381731, 2.0254273, -0.39233872, -0.42393345, 2.2760479, -0.71189457, -0.60599393, 2.0163939, -0.39841026, -0.60027134, 2.1354396, -0.40491298, -0.30758607, 2.3370185, -0.62746048, -0.49580219, 2.0175889, -0.41587964, -0.46615571 ); cv::Mat_<float> M= A.reshape(3); //<-- code breaks here in 2.4.0 Scalar s = mean (M); Point3f centroid = Point3f (s[0], s[1], s[2]);
Associated revisions
Added test and workaround for #1942
Merge pull request #1942 from ilya-lavrenov:warn_fix
History
Updated by Andrey Kamaev almost 13 years ago
It seems that you are using wrong headers or have a link problem.
Mat
class defines 2 versions of reshape
method:
1Mat reshape(int _cn, int _rows=0) const;
2Mat reshape(int _cn, int _newndims, const int* _newsz) const;
And you need the first one, but your exception comes from the second overload.
- Status changed from Open to Cancelled
- Target version set to 2.4.1
- Category set to core
- Assignee set to Andrey Kamaev
Updated by Vito Macchia almost 13 years ago
Yes but iven if I try with:
cv::Mat_<float> M= A.reshape(3,A.rows);
code breaks!
And yes I know there is an overload (though, according to the documentation, the second form should be for MatND)
But calling with one single argument should call the first form, not the second.
You mentioned an header problem. Which header should I include then? I was including just this:
#include <opencv2/core/core.hpp>
and up to 2.3.1 was ok. I compiled it from source on Ubuntu 12.04 using the same CMake settings I used for 2.3.1
Updated by Andrey Kamaev almost 13 years ago
Well, I've reproduced your problem.
Test and partial fix are committed to OpenCV trunk.
By the way you are using cv::Mat_<float>
in a wrong way. It can not be 3-channel. For 3-channel Mat you need a cv::Mat_<cv::Vec3f>
.
See the last paragraph for multi-channel Mat_
: http://docs.opencv.org/modules/core/doc/basic_structures.html#Mat_
So the copy constructor internally tries to reshape the Mat back to single channel.
- Status changed from Cancelled to Done
Updated by Vito Macchia almost 13 years ago
Indeed you are right..
It was a code I wrote a long time ago, that I was just trying to run with the new release. I will fix my code using Mat_<Vec3f> for 3 channels matrices, thank you! ;)