question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mat.GetArray<T> doesn't include channel count or show a consistent error

See original GitHub issue

Summary 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
yacaehcommented, Jul 23, 2020

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):

var newimage = image.Reshape(1);
newimage.GetArray(out imageArray);
var array = Numpy.np.array(imageArray).reshape(image.Rows, image.Cols, image.Channels());

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.

                    var newimage = _frame.Reshape(1);
                    newimage.GetArray(out byte[] imageArray);
                    var array = np.array(imageArray, dtype: np.uint8).reshape(_frame.Rows, _frame.Cols, _frame.Channels());
2reactions
Gwindalmircommented, Jul 16, 2020

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):

var newimage = image.Reshape(1);
newimage.GetArray(out imageArray);
var array = Numpy.np.array(imageArray).reshape(image.Rows, image.Cols, image.Channels());
Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found