Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix normalize method when scale image #70

Merged
merged 2 commits into from Jul 10, 2018
Merged

Conversation

mslichao
Copy link
Member

@mslichao mslichao commented Jul 6, 2018

Previous method not change the image size, only draw a small one on the left top.

@shishaochen
Copy link
Contributor

@mslichao Good catch on this problem! Thanks a lot!
However, the default scaling implementation of Bitmap constructor may have problem described at C# Resizing Bitmap without changing the Pixelformat.
How about using function below which is the original/internal implementation of MNIST App?

public static Bitmap ResizeImage(Bitmap imgToResize, Size size)
{
    Bitmap b = new Bitmap(size.Width, size.Height);
    using (Graphics g = Graphics.FromImage(b))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
    }
    return b;
}

public static float[,] ConvertToGrayScaleArray(Bitmap image)
{
    var height = image.Size.Height;
    var width = image.Size.Width;
    var data = new float[height, width];
    for (var x = 0; x < height; x++)
    {
        for (var y = 0; y < width; y++)
        {
            // Be careful about the X/Y direction
            var color = image.GetPixel(y, x);
            // 1. Convert to gray
            // 2. Normalize to [-0.5,0.5]
            // 3. Turn to black background and white digit like MNIST dataset
            data[x, y] = (float)(0.5 - (color.R + color.G + color.B) / (3.0 * 255));
        }
    }
    return data;
}

Moving these 2 utilization functions to another C# file like "ImageUtils.cs" may be helpful for our customers to apply to their applications.

@mslichao
Copy link
Member Author

mslichao commented Jul 9, 2018

@shishaochen Thanks for pointing out the issue of Bitmap constructor, I have fix it in the new commit.
Besides, I have reverted all my changes and only did the required change in the new commit because:

  1. Keep my pull request only fix the image size issue.
  2. Respect the origin author's code

@shishaochen
Copy link
Contributor

shishaochen commented Jul 9, 2018

@mslichao I see.
Actually, I am the author of this App while the code was open sourced by others with some changes.

@linmajia
Copy link
Contributor

linmajia commented Jul 9, 2018

@shishaochen and @mslichao , if you think the code needs improvement, please submit pull requests and don't worry about other things.

@linmajia linmajia merged commit 92522d2 into microsoft:master Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants