Mat.AsSpan() doesn't get correct length
Summary of your issue
I'm currently working on converting Mat to Microsoft.ML.OnnxRuntime.Tensors.Tensor<T>. To be efficient, I used mat.AsSpan
public unsafe Span<T> AsSpan<T>() where T : unmanaged
=> IsContinuous() ? new Span<T>(DataPointer, (int)Total() * ElemSize() / sizeof(T)) : [];
Environment
- .NET 8.0.302
- OpenCvSharp4 version 4.10.0.20240616
What did you do when you faced the problem?
Accessing from mat.DataPointer directly just works.
Example code:
Output:
What did you intend to be?
Having the same issue. Here's a minimal reproducible example using 3-channel Mats. The length of the returned Span is only Width x Height instead of 3 x Width x Height.
static void ReproduceIndexOutOfRange()
{
// Read a jpeg image of format 3-channel 10x20 pixels
Mat testImage8u = new(10, 20, MatType.CV_8UC3);
Span<byte> testImageData = testImage8u.AsSpan<byte>();
for (int i = 0; i < testImage8u.Channels() * testImage8u.Rows * testImage8u.Cols; i++)
testImageData[i] = 255; // IndexOutOfRangeException at i = 200
Cv2.ImShow("testImage8u", testImage8u);
Cv2.WaitKey(0);
}
But it's not like the span points to a single channel of the image. If I set the bound of the loop to testImage8u.Rows * testImage8u.Cols instead of three times of that to avoid IndexOutOfRangeException like so:
static void ReproduceIndexOutOfRange()
{
// Read a jpeg image of format 3-channel 10x20 pixels
Mat testImage8u = new(10, 20, MatType.CV_8UC3);
Span<byte> testImageData = testImage8u.AsSpan<byte>();
for (int i = 0; i < testImage8u.Rows * testImage8u.Cols; i++)
testImageData[i] = 255;
Cv2.ImShow("testImage8u", testImage8u);
Cv2.WaitKey(0);
}
The resulting image looks like this
OpenCvSharp version: 4.9.0.20240103 Os: Linux .NET version: 8.0.108