You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently working on converting Mat to Microsoft.ML.OnnxRuntime.Tensors.Tensor<T>. To be efficient, I used mat.AsSpan() to manually access the data in a CV_8UC3 image. However, it always comes OutOfRange. Referring to the source code below:
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.
staticvoidReproduceIndexOutOfRange(){// Read a jpeg image of format 3-channel 10x20 pixelsMattestImage8u=new(10,20,MatType.CV_8UC3);Span<byte>testImageData=testImage8u.AsSpan<byte>();for(inti=0;i<testImage8u.Channels()*testImage8u.Rows*testImage8u.Cols;i++)testImageData[i]=255;// IndexOutOfRangeException at i = 200Cv2.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:
staticvoidReproduceIndexOutOfRange(){// Read a jpeg image of format 3-channel 10x20 pixelsMattestImage8u=new(10,20,MatType.CV_8UC3);Span<byte>testImageData=testImage8u.AsSpan<byte>();for(inti=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
Activity
GuaiGuaiOnMyComputer commentedon Sep 12, 2024
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.
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.Colsinstead of three times of that to avoid IndexOutOfRangeException like so:The resulting image looks like this

OpenCvSharp version: 4.9.0.20240103
Os: Linux
.NET version: 8.0.108
Merge pull request #1703 from truebigsand/main