Exemple #1
0
        public static Bitmap ImageToBitmap(GrayscaleFloatImage image)
        {
            Bitmap B = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);

            LockBitmapInfo lbi = LockBitmap(B);

            try
            {
                for (int j = 0; j < image.Height; j++)
                {
                    for (int i = 0; i < image.Width; i++)
                    {
                        byte c = image[i, j] < 0.0f ? (byte)0 : image[i, j] > 255.0f ? (byte)255 : (byte)image[i, j];
                        lbi.data[lbi.linewidth * j + i * 4]     = c;
                        lbi.data[lbi.linewidth * j + i * 4 + 1] = c;
                        lbi.data[lbi.linewidth * j + i * 4 + 2] = c;
                    }
                }
            }
            finally
            {
                UnlockBitmap(lbi);
            }

            return(B);
        }
        public static void gabor(ref GrayscaleFloatImage image, float sigma, float gamma, float theta, float lambda, float psi)
        {
            GrayscaleFloatImage tempImg = new GrayscaleFloatImage(image.Width, image.Height);
            int n = (int)(6 * Math.Abs(sigma) + 1);

            n             = n == 1 || n % 2 == 0 ? 3 : n;
            float[,] matr = new float[n, n];
            float normValue = 0;

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    double x = i * Math.Cos(theta) + j * Math.Sin(theta);
                    double y = -i *Math.Sin(theta) + j * Math.Cos(theta);

                    matr[i, j] = (float)(Math.Exp(-(x * x + y * y * gamma * gamma) / (2 * sigma * sigma)) * Math.Cos(2 * x * Math.PI / lambda + psi));
                    normValue += matr[i, j];
                }
            }


            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    tempImg[x, y] = GaborConversion1(image, x, y, matr, normValue);
                }
            }

            float max = image[0, 0], min = image[0, 0];

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    if (image[x, y] > max)
                    {
                        max = image[x, y];
                    }
                    if (image[x, y] < min)
                    {
                        min = image[x, y];
                    }
                }
            }

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    tempImg[x, y] = tempImg[x, y] / ((max * min) / (max + min));
                }
            }
            image = tempImg;
        }
Exemple #3
0
        public static GrayscaleFloatImage FileToGrayscaleFloatImage(string filename)
        {
            if (CheckPGM(filename))
            {
                return(ReadPGM(filename));
            }

            Bitmap B = new Bitmap(filename);
            GrayscaleFloatImage res = BitmapToGrayscaleFloatImage(B);

            B.Dispose();
            return(res);
        }
        GaborConversion1(GrayscaleFloatImage image, int x, int y, float[,] matr, float normValue)
        {
            float tempPixel = 0;


            int n = matr.GetLength(0);

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    tempPixel += matr[i, j] * image[((x + i - (n - 1) / 2) < 0 || (x + i - (n - 1) / 2) >= image.Width ? x : (x + i - (n - 1) / 2)), ((y + j - (n - 1) / 2) < 0 || (y + j - (n - 1) / 2) >= image.Height ? y : (y + j - (n - 1) / 2))];
                }
            }


            return(tempPixel);
        }
Exemple #5
0
 public static void ImageToFile(GrayscaleFloatImage image, string filename)
 {
     using (Bitmap B = ImageToBitmap(image))
         B.Save(filename);
 }
Exemple #6
0
        public static GrayscaleFloatImage BitmapToGrayscaleFloatImage(Bitmap B)
        {
            int W = B.Width, H = B.Height;
            GrayscaleFloatImage res = new GrayscaleFloatImage(W, H);

            if (B.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                Color[] pi  = B.Palette.Entries;
                byte[]  pal = new byte[1024];
                for (int i = 0; i < pi.Length; i++)
                {
                    Color C = pi[i];
                    pal[i * 4]     = C.B;
                    pal[i * 4 + 1] = C.G;
                    pal[i * 4 + 2] = C.R;
                    pal[i * 4 + 3] = C.A;
                }

                LockBitmapInfo lbi = LockBitmap(B, PixelFormat.Format8bppIndexed, 1);
                try
                {
                    for (int j = 0; j < H; j++)
                    {
                        for (int i = 0; i < W; i++)
                        {
                            int c = lbi.data[lbi.linewidth * j + i];
                            int b = pal[c * 4];
                            int g = pal[c * 4 + 1];
                            int r = pal[c * 4 + 2];
                            res[i, j] = 0.114f * b + 0.587f * g + 0.299f * r;
                        }
                    }
                }
                finally
                {
                    UnlockBitmap(lbi);
                }
            }
            else
            {
                LockBitmapInfo lbi = LockBitmap(B);
                try
                {
                    for (int j = 0; j < H; j++)
                    {
                        for (int i = 0; i < W; i++)
                        {
                            int b = lbi.data[lbi.linewidth * j + i * 4];
                            int g = lbi.data[lbi.linewidth * j + i * 4 + 1];
                            int r = lbi.data[lbi.linewidth * j + i * 4 + 2];
                            res[i, j] = 0.114f * b + 0.587f * g + 0.299f * r;
                        }
                    }
                }
                finally
                {
                    UnlockBitmap(lbi);
                }
            }

            return(res);
        }
Exemple #7
0
        static GrayscaleFloatImage ReadPGM(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            try
            {
                while (true)
                {
                    string str = ReadString(fs).Trim();
                    if (str == null)
                    {
                        return(null);
                    }
                    else if (str == "" || str.StartsWith("#"))
                    {
                        continue;
                    }
                    else if (str != "P5")
                    {
                        return(null);
                    }
                    else
                    {
                        break;
                    }
                }

                int Width = -1, Height = -1, MaxL = -1;

                while (true)
                {
                    string str = ReadString(fs).Trim();
                    if (str == null)
                    {
                        return(null);
                    }
                    else if (str == "" || str.StartsWith("#"))
                    {
                        continue;
                    }
                    string[] arr = str.Split(' ', '\t');
                    Width  = int.Parse(arr[0]);
                    Height = int.Parse(arr[1]);
                    break;
                }

                while (true)
                {
                    string str = ReadString(fs).Trim();
                    if (str == null)
                    {
                        return(null);
                    }
                    else if (str == "" || str.StartsWith("#"))
                    {
                        continue;
                    }
                    MaxL = int.Parse(str);
                    break;
                }

                GrayscaleFloatImage res = new GrayscaleFloatImage(Width, Height);

                if (MaxL <= 255)
                {
                    byte[] raw = new byte[Width * Height];
                    fs.Read(raw, 0, raw.Length);
                    for (int j = 0; j < Height; j++)
                    {
                        for (int i = 0; i < Width; i++)
                        {
                            res[i, j] = raw[j * Width + i];
                        }
                    }
                }
                else
                {
                    byte[] raw = new byte[Width * Height * 2];
                    fs.Read(raw, 0, raw.Length * 2);
                    for (int j = 0; j < Height; j++)
                    {
                        for (int i = 0; i < Width; i++)
                        {
                            res[i, j] = raw[(j * Width + i) * 2] + raw[(j * Width + i) * 2 + 1] * 255;
                        }
                    }
                }

                return(res);
            }
            finally
            {
                fs.Close();
            }
        }