NumSharp icon indicating copy to clipboard operation
NumSharp copied to clipboard

Bitmap.ToNDArray problem with odd bitmap width

Open herrvonregen opened this issue 5 years ago • 2 comments

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. 227*3 = 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

herrvonregen avatar Feb 06 '20 12:02 herrvonregen

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

Nucs avatar Feb 06 '20 15:02 Nucs

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

herrvonregen avatar Feb 06 '20 16:02 herrvonregen