Ejemplo n.º 1
0
        public static ImageDataBlock[] LoadImageData(string labelFilename, string imageFilename)
        {
            uint numberOfImages = 0;
            uint numberOfRows;
            uint numberOfColumns;

            byte[] pixels;

            if (File.Exists(imageFilename))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(imageFilename, FileMode.Open)))
                {
                    reader.ReadUInt32BE();
                    numberOfImages  = reader.ReadUInt32BE();
                    numberOfRows    = reader.ReadUInt32BE();
                    numberOfColumns = reader.ReadUInt32BE();
                    pixels          = new byte[numberOfImages * numberOfRows * numberOfColumns];
                    for (int i = 0; i < pixels.Length; i++)
                    {
                        pixels[i] = reader.ReadByte();
                    }
                }
            }
            else
            {
                throw new FileNotFoundException();
            }


            uint numberOfLabels = 0;

            byte[] labels;

            if (File.Exists(labelFilename))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(labelFilename, FileMode.Open)))
                {
                    reader.ReadUInt32BE();
                    numberOfLabels = reader.ReadUInt32BE();
                    labels         = new byte[numberOfLabels];
                    for (int i = 0; i < numberOfLabels; i++)
                    {
                        labels[i] = reader.ReadByte();
                    }
                }
            }
            else
            {
                throw new FileNotFoundException();
            }

            if (numberOfImages != numberOfLabels)
            {
                throw new InvalidDataException("label and image should have the same count");
            }

            ImageDataBlock[]    imagesDataBlocks = new ImageDataBlock[numberOfLabels];
            DataBlockCommonInfo commonInfo       = new DataBlockCommonInfo()
            {
                Height = (int)numberOfColumns, Width = (int)numberOfRows
            };

            for (int i = 0; i < numberOfLabels; i++)
            {
                byte[] image = new byte[numberOfRows * numberOfColumns];

                Array.Copy(pixels, i * (numberOfRows * numberOfColumns), image, 0, numberOfRows * numberOfColumns);
                imagesDataBlocks[i] = new ImageDataBlock(image, commonInfo, labels[i]);
            }

            return(imagesDataBlocks);
        }
Ejemplo n.º 2
0
 public ImageDataBlock(byte[] image, DataBlockCommonInfo commonInfo, byte label)
 {
     _commonInfo = commonInfo;
     _image      = image;
     _label      = label;
 }