Mat.GetArray<T> doesn't include channel count or show a consistent error
See original GitHub issueSummary of your issue
The GetArray<T>(out T[] data) overload doesn’t allocate a valid array for a multi-channel image.
Environment
Windows, Visual Studio 2017, .NET Core 2.1
What did you do when you faced the problem?
I need to convert a Mat array to a NDarray, so I’m converting to an intermediate C# array first. Performance isn’t a concern for me.
Example code:
var image = new Mat(filename, ImreadModes.Color);
byte[] imageArray;
image.GetArray(out imageArray);
Output:
Provided data element number (65536) should be multiple of the Mat channels count (3)
What did you intend to be?
It should allocate an array large enough to include all the channels of the image. Alternatively, throw an error earlier indicating this operation can only be performed on a single channel image.
I saw issue #952 but I do not agree with the resolution. This is a bug, as at the very least it’s inconsistent: Take a 256x256 image. If it’s 3 channels (such as CV_8UC3), you get the error above. However if you reshape it to just 2 channels (so it’s 256x384x2), you get
Mat data type is not compatible: CV_8UC2
At the very least, the error should be fixed so it matches, or provide a clear reason why it won’t work.
Additionally, would it be possible to include channel size when allocating, or would that “data type is not compatible” error prevent that from working as intended?
Such as adding a * Channels
to this line: https://github.com/shimat/opencvsharp/blob/fe727537ada32417af1651c3a9620b46c72dfe55/src/OpenCvSharp/Modules/core/Mat/Mat.cs#L4229
I just mention it because while CheckArgumentsForConvert checks rows, columns, and channel count, GetArray only uses Rows and Cols. This makes what it allocates, and what it checks for, inconsistent.
I was worried about a possible buffer overflow, but I could not reproduce one in the tests I ran.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Thank you for your comment! It helped me a lot. I just have more thing to add, I had to specify the data type of array to uint8, otherwise it’s output shows negative values.
I understand if the behavior is otherwise intended, but the error message isn’t clear, or indicative of the actual problem. That’s the real issue I have.
Anyway, I changed my code to the following (I still need to test it, but looks ok at first glance):