//定义Bitmap图像类 public static Bitmap MakeBitmap(DigitImage dImage, int mag) { int width = dImage.width * mag; int height = dImage.height * mag;//获取图像宽和高 //构造Bitmap数据 Bitmap result = new Bitmap(width, height); //通过Bitmap数据信息构造图片 Graphics gr = Graphics.FromImage(result); for (int i = 0; i < dImage.height; ++i) { for (int j = 0; j < dImage.width; ++j) { //设置pixelColor的值 int pixelColor = 255 - dImage.pixels[i][j]; // black digits //定义RGB值 Color c = Color.FromArgb(pixelColor, pixelColor, pixelColor); //定义画笔 SolidBrush sb = new SolidBrush(c); //填充像素 gr.FillRectangle(sb, j * mag, i * mag, mag, mag); } } return(result); }
//读取图片 public static DigitImage[] LoadData(string pixelFile, string labelFile) { int numImages = 60000; //图片总数量 DigitImage[] result = new DigitImage[numImages]; //定义result数组 //创建28 x 28 矩阵 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); //MNIST 培训像素文件的格式已初始魔法的整数(32 位)值 //在打开后的二进制文件,第一次四个整数是阅读使用 ReadInt32 方法 int magic1 = brImages.ReadInt32(); //读取4字节整数 magic1 = ReverseBytes(magic1); // 数据格式化 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); 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(); //读取字节 DigitImage dImage = new DigitImage(28, 28, pixels, lbl); //定义DigitImage对象 result[di] = dImage; } // Each image //关闭操作 ifsPixels.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); //返回结果 return(result); }
//像素点的值 public static string PixelValues(DigitImage dImage) { string s = "";//定义string变量 //for循环输出像素值 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); }
//button2_Click的触发函数定义 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()); Bitmap bitMap = MakeBitmap(currImage, mag); //显示图片 pictureBox1.Image = bitMap; //输出像素二进制数据 string pixelVals = PixelValues(currImage); textBox5.Text = pixelVals; //标签赋值 textBox3.Text = textBox4.Text; // Update curr idx textBox4.Text = (nextIndex + 1).ToString(); // ++next index //输出结果 listBox1.Items.Add("当前图像索引 = " + textBox3.Text + " 识别结果 = " + currImage.label); }
//读取图片 public static DigitImage[] LoadData(string pixelFile, string labelFile) { int numImages = 60000;//图片总数量 DigitImage[] result = new DigitImage[numImages];//定义result数组 //创建28 x 28 矩阵 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); //MNIST 培训像素文件的格式已初始魔法的整数(32 位)值 //在打开后的二进制文件,第一次四个整数是阅读使用 ReadInt32 方法 int magic1 = brImages.ReadInt32(); //读取4字节整数 magic1 = ReverseBytes(magic1); // 数据格式化 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); 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(); //读取字节 DigitImage dImage = new DigitImage(28, 28, pixels, lbl);//定义DigitImage对象 result[di] = dImage; } // Each image //关闭操作 ifsPixels.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); //返回结果 return result; }
//像素点的值 public static string PixelValues(DigitImage dImage) { string s = "";//定义string变量 //for循环输出像素值 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; }
//定义Bitmap图像类 public static Bitmap MakeBitmap(DigitImage dImage, int mag) { int width = dImage.width * mag; int height = dImage.height * mag;//获取图像宽和高 //构造Bitmap数据 Bitmap result = new Bitmap(width, height); //通过Bitmap数据信息构造图片 Graphics gr = Graphics.FromImage(result); for (int i = 0; i < dImage.height; ++i) { for (int j = 0; j < dImage.width; ++j) { //设置pixelColor的值 int pixelColor = 255 - dImage.pixels[i][j]; // black digits //定义RGB值 Color c = Color.FromArgb(pixelColor, pixelColor, pixelColor); //定义画笔 SolidBrush sb = new SolidBrush(c); //填充像素 gr.FillRectangle(sb, j * mag, i * mag, mag, mag); } } return result; }