Example #1
0
        public fData Next()
        {
            // 0. assert images
            if (images.Count == 0)
            {
                return(null);
            }
            // 1. get next fData
            int   i = Math.Daemon.Random.Next(0, idx.Count);
            fData f = images[idx[i]];

            idx.RemoveAt(i);
            return(f);
        }
Example #2
0
        public static IList <fData> ReadColorImage(string ubyteImageFileName, int?number,
                                                   double dataMin, double dataMax, double labelMin, double labelMax)
        {
            const int imageSize   = 32;
            const int imageLength = imageSize * imageSize * 3;

            byte[]        pixels;
            IList <fData> images = new List <fData>();
            fData         image;
            FileStream    fileStreamImage;
            BinaryReader  imageReader;

            byte[] byteImage;
            Random random = new Random();

            try
            {
                //Stream byte data from file
                fileStreamImage = new FileStream(ubyteImageFileName, FileMode.Open, FileAccess.Read, FileShare.Read); // train images
                imageReader     = new BinaryReader(fileStreamImage);
                byteImage       = new byte[fileStreamImage.Length];
                for (int i = 0; i < byteImage.Length; i++)
                {
                    byteImage[i] = imageReader.ReadByte();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            int nofStreamImages = byteImage.Length / (imageLength + 1);

            bool[] retr = new bool[nofStreamImages];
            long[] cntr = new long[10];

            int  j, n = 0, r;
            byte l;

            switch (number == null)
            {
            case false:
                int nofImages = number.Value * 10;
                while (n < nofImages)
                {
                    r      = random.Next(0, nofStreamImages);
                    j      = r * (imageLength + 1);
                    pixels = new byte[imageLength];
                    l      = byteImage[j];

                    if ((retr[r] == true) || (cntr[l] >= number))
                    {
                        continue;
                    }

                    for (int i = 1; i < imageLength; i++)
                    {
                        pixels[i] = byteImage[j + i];
                    }

                    // create image
                    image = new fData(pixels, dataMin, dataMax, EncodeLabel(l), labelMin, labelMax);

                    // add to list of images
                    images.Add(image);

                    // update records
                    retr[r]  = true;
                    cntr[l] += 1;
                    ++n;
                }

                break;

            case true:

                while (n < nofStreamImages)
                {
                    j      = n * (imageLength + 1);
                    pixels = new byte[imageLength];
                    l      = byteImage[j];
                    for (int i = 1; i < imageLength; i++)
                    {
                        pixels[i] = byteImage[j + i];
                    }

                    // create image
                    image = new fData(pixels, dataMin, dataMax, EncodeLabel(l), labelMin, labelMax);

                    // add to list of images
                    images.Add(image);
                    ++n;
                }

                break;
            }

            fileStreamImage.Close();
            imageReader.Close();
            return(images);
        }