public static string PixelValues(DigitImage dImage) { // create a string, with embedded newlines, suitable // for display in a multi-line TextBox control string s = ""; for (int i = 0; i < dImage.height; ++i) { for (int j = 0; j < dImage.width; ++j) { s += dImage.pixels[i][j].ToString("X2") + " "; } s += Environment.NewLine; } return(s); }
private void button2_Click(object sender, EventArgs e) // Display 'next' image { int nextIndex = int.Parse(textBox4.Text); DigitImage currImage = trainImages[nextIndex]; int mag = int.Parse(comboBox1.SelectedItem.ToString()); // magnification Bitmap bitMap = MakeBitmap(currImage, mag); pictureBox1.Image = bitMap; string pixelVals = PixelValues(currImage); textBox5.Text = pixelVals; textBox3.Text = textBox4.Text; // update curr idx from old next idz textBox4.Text = (nextIndex + 1).ToString(); // ++next index listBox1.Items.Add("Curr image index = " + textBox3.Text + " label = " + currImage.label); }
} // LoadData public static Bitmap MakeBitmap(DigitImage dImage, int mag) { // create a C# Bitmap suitable for display in a PictureBox control int width = dImage.width * mag; int height = dImage.height * mag; Bitmap result = new Bitmap(width, height); Graphics gr = Graphics.FromImage(result); for (int i = 0; i < dImage.height; ++i) { for (int j = 0; j < dImage.width; ++j) { int pixelColor = 255 - dImage.pixels[i][j]; // white background, black digits //int pixelColor = dImage.pixels[i][j]; // black background, white digits Color c = Color.FromArgb(pixelColor, pixelColor, pixelColor); // gray scale //Color c = Color.FromArgb(pixelColor, 0, 0); // red scale SolidBrush sb = new SolidBrush(c); gr.FillRectangle(sb, j * mag, i * mag, mag, mag); // fills bitmap via Graphics object } } return(result); }
public static DigitImage[] LoadData(string pixelFile, string labelFile) { // Load MNIST training set of 60,000 images into memory // remove static to access listBox1 int numImages = 60000; DigitImage[] result = new DigitImage[numImages]; byte[][] pixels = new byte[28][]; for (int i = 0; i < pixels.Length; ++i) { pixels[i] = new byte[28]; } FileStream ifsPixels = new FileStream(pixelFile, FileMode.Open); FileStream ifsLabels = new FileStream(labelFile, FileMode.Open); BinaryReader brImages = new BinaryReader(ifsPixels); BinaryReader brLabels = new BinaryReader(ifsLabels); int magic1 = brImages.ReadInt32(); // stored as Big Endian magic1 = ReverseBytes(magic1); // convert to Intel format int imageCount = brImages.ReadInt32(); imageCount = ReverseBytes(imageCount); int numRows = brImages.ReadInt32(); numRows = ReverseBytes(numRows); int numCols = brImages.ReadInt32(); numCols = ReverseBytes(numCols); int magic2 = brLabels.ReadInt32(); magic2 = ReverseBytes(magic2); int numLabels = brLabels.ReadInt32(); numLabels = ReverseBytes(numLabels); // each image for (int di = 0; di < numImages; ++di) { for (int i = 0; i < 28; ++i) // get 28x28 pixel values { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i][j] = b; } } byte lbl = brLabels.ReadByte(); // get the label DigitImage dImage = new DigitImage(28, 28, pixels, lbl); result[di] = dImage; } // each image ifsPixels.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); return(result); } // LoadData