Efficiently convert System.Single[,] to numpy array
@denfromufa - that is a very useful link.
The suggestion there is to do a direct memory copy, either using Marshal.Copy or np.frombuffer. I couldn't manage to get the Marshal.Copy version working - some shenanigans are required to use a 2D array with Marshal and that changed the contents of of the array somehow - but the np.frombuffer version seems to work for me and reduced the time to complete by a factor of ~16000 for a 3296*2471 array (~25s -> ~1.50ms). This is good enough for my purposes
The method requires a couple more imports, so I've included those in the code snippet below
import ctypes
from System.Runtime.InteropServices import GCHandle, GCHandleType
def SingleToNumpyFromBuffer(TwoDArray):
src_hndl = GCHandle.Alloc(TwoDArray, GCHandleType.Pinned)
try:
src_ptr = src_hndl.AddrOfPinnedObject().ToInt32()
bufType = ctypes.c_float*len(TwoDArray)
cbuf = bufType.from_address(src_ptr)
resultArray = np.frombuffer(cbuf, dtype=cbuf._type_)
finally:
if src_hndl.IsAllocated: src_hndl.Free()
return resultArray