Skip to content

Commit

Permalink
Fix issue emgucv#834 Mat.GetData does not respect the ROI
Browse files Browse the repository at this point in the history
  • Loading branch information
emgucv committed May 6, 2023
1 parent 6736020 commit 993edfb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Emgu.CV.Test/AutoTestMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Xml;
using System.Xml.Linq;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;

Expand Down Expand Up @@ -62,6 +63,26 @@ public void TestGetData()
EmguAssert.IsTrue(data2[i, j] == data[i, j]);
}

using (Mat m = new Mat(
data.GetLength(0),
data.GetLength(1),
DepthType.Cv32F,
1,
dataHandle.AddrOfPinnedObject(),
data.GetLength(1) * Marshal.SizeOf<float>()
))
using (Mat m3 = new Mat(m, new Rectangle(1, 1, m.Width-1, m.Height-1)))
{
float[,] data2 = m.GetData(true) as float[,];
for (int i = 0; i < data2.GetLength(0); i++)
for (int j = 0; j < data2.GetLength(1); j++)
EmguAssert.IsTrue(data2[i, j] == data[i, j]);

float[,] data3 = m3.GetData(true) as float[,];
for (int i = 0; i < data3.GetLength(0); i++)
for (int j = 0; j < data3.GetLength(1); j++)
EmguAssert.IsTrue(data3[i, j] == data[i + 1, j + 1]);
}
dataHandle.Free();
}

Expand Down
11 changes: 10 additions & 1 deletion Emgu.CV/Core/Mat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,16 @@ public Array GetData(bool jagged = true)
}

GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
CvInvoke.cveMemcpy(handle.AddrOfPinnedObject(), DataPointer, byteSize);
using (Mat dst = new Mat(
this.Size,
this.Depth,
this.NumberOfChannels,
handle.AddrOfPinnedObject(),
byteSize / Height))
{
this.CopyTo(dst);
}

handle.Free();
return array;
}
Expand Down

0 comments on commit 993edfb

Please sign in to comment.