SciSharp / NumSharp

High Performance Computation for N-D Tensors in .NET, similar API to NumPy.
https://github.com/SciSharp
Apache License 2.0
1.36k stars 189 forks source link

Bitmap.ToNDArray problem with odd bitmap width #396

Open herrvonregen opened 4 years ago

herrvonregen commented 4 years ago

Hi everyone. The official micorsoft documenation for Bitmap.Stride says:

The stride is the width of a single row of pixels (a scan line), rounded up to a four-byte boundary.

This could cause an exception here if the loaded bitmap is RGB with an odd width. Buffer.MemoryCopy(src, dst, bmpData.Stride * image.Height, nd.size); var ret = nd.reshape(1, image.Height, image.Width, bmpData.Stride / bmpData.Width);

Example: An RGB image with the size of 227x227 pixels Stride will be 684. 2273 = 681 rounded up to four-byte boundary. Copied data are 155268 bytes. Reshaped into 277 277 * floor(684/227) = 154587 bytes This will cause an IncorrectShapeException

Nucs commented 4 years ago

You shouldn't work hard, we provide extensions to Bitmap, see this wiki article.

herrvonregen commented 4 years ago

Yes, I know but the implementation has the described behavior. Try it with the picture provided below. 00001