SciSharp / SciSharp/NumSharp

Bitmap.ToNDArray problem with odd bitmap width

Open
#396 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1.5k
Forks
205
Avg merge
7d 7h
Merged PRs (30d)
2

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue points to Bitmap.ToNDArray and the Buffer.MemoryCopy/reshape calls; begin there and reproduce the problem with a 227x227 RGB bitmap. Verify the resulting array shape and copied byte count against the bitmap stride, and consider the issue done when this input no longer raises IncorrectShapeException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
computer-vision
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.