Beispiel #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);
        }
Beispiel #2
0
        static GrayscaleFloatImage dir(ColorFloatImage image)
        {
            string          mode       = "even";
            ColorFloatImage temp_image = img_expansion(image, mode, 1);

            ColorFloatImage temp_image_x = new ColorFloatImage(image.Width, image.Height);
            ColorFloatImage temp_image_y = new ColorFloatImage(image.Width, image.Height);

            for (int y = 1; y < temp_image_x.Height + 1; y++)
            {
                for (int x = 1; x < temp_image_x.Width + 1; x++)
                {
                    temp_image_x[x - 1, y - 1] = temp_image[x + 1, y] + (-1) * temp_image[x, y];
                }
            }
            for (int y = 1; y < temp_image_y.Height + 1; y++)
            {
                for (int x = 1; x < temp_image_y.Width + 1; x++)
                {
                    temp_image_y[x - 1, y - 1] = temp_image[x, y + 1] + (-1) * temp_image[x, y];
                }
            }

            GrayscaleFloatImage out_img = new GrayscaleFloatImage(image.Width, image.Height);
            GrayscaleFloatImage x_grad  = temp_image_x.ToGrayscaleFloatImage();
            GrayscaleFloatImage y_grad  = temp_image_y.ToGrayscaleFloatImage();

            for (int y = 0; y < y_grad.Height; y++)
            {
                for (int x = 0; x < x_grad.Width; x++)
                {
                    if (x_grad[x, y] == 0 && y_grad[x, y] != 0)
                    {
                        out_img[x, y] = 64;
                        continue;
                    }
                    double theta = Math.Atan2(y_grad[x, y], x_grad[x, y]) * (180 / Math.PI);
                    if (theta <= 22.5 && theta > -22.5 || theta <= -157.5 && theta > 157.5)
                    {
                        out_img[x, y] = 64; // ->
                    }
                    else if (theta <= 67.5 && theta > 22.5 || theta >= -157.5 && theta < -112.5)
                    {
                        out_img[x, y] = 192; // /
                    }
                    else if (theta > 67.5 && theta <= 112.5 || theta >= -112.5 && theta < -67.5)
                    {
                        out_img[x, y] = 128; // ^
                    }
                    else if (theta > 112.5 && theta <= 157.5 || theta >= -67.5 && theta < -22.5)
                    {
                        out_img[x, y] = 255; // \
                    }
                }
            }
            return(out_img);
        }
Beispiel #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);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            GrayscaleFloatImage output_image = null;

            if (args[0] == "mse")
            {
                GrayscaleFloatImage image  = ImageIO.FileToGrayscaleFloatImage(args[1]);
                GrayscaleFloatImage image1 = ImageIO.FileToGrayscaleFloatImage(args[2]);
                Console.WriteLine(MSE(image, image1));
            }
            else if (args[0] == "psnr")
            {
                GrayscaleFloatImage image  = ImageIO.FileToGrayscaleFloatImage(args[1]);
                GrayscaleFloatImage image1 = ImageIO.FileToGrayscaleFloatImage(args[2]);
                Console.WriteLine(PSNR(image, image1));
            }
            else if (args[0] == "ssim")
            {
                GrayscaleFloatImage image  = ImageIO.FileToGrayscaleFloatImage(args[1]);
                GrayscaleFloatImage image1 = ImageIO.FileToGrayscaleFloatImage(args[2]);
                Console.WriteLine(SSIM(image, image1));
            }
            else if (args[0] == "mssim")
            {
                GrayscaleFloatImage image  = ImageIO.FileToGrayscaleFloatImage(args[1]);
                GrayscaleFloatImage image1 = ImageIO.FileToGrayscaleFloatImage(args[2]);
                Console.WriteLine(MSSIM(image, image1));
            }
            else if (args[0] == "dir")
            {
                ColorFloatImage image = ImageIO.FileToColorFloatImage(args[2]);
                image        = gauss(image, "even", (float)Convert.ToDouble(args[1]));
                output_image = dir(image);
                ImageIO.ImageToFile(output_image, args[3]);
            }
            else if (args[0] == "nonmax")
            {
                ColorFloatImage image = ImageIO.FileToColorFloatImage(args[2]);
                image        = gauss(image, "even", (float)Convert.ToDouble(args[1]));
                output_image = nonmax(image);
                ImageIO.ImageToFile(output_image, args[3]);
            }
            else if (args[0] == "canny")
            {
                ColorFloatImage image = ImageIO.FileToColorFloatImage(args[4]);
                image        = gauss(image, "even", (float)Convert.ToDouble(args[1]));
                output_image = canny(image, Convert.ToInt32(args[2]), Convert.ToInt32(args[3]));
                ImageIO.ImageToFile(output_image, args[5]);
            }
        }
Beispiel #5
0
        static double MSE(GrayscaleFloatImage img1, GrayscaleFloatImage img2)
        {
            double dif    = 0;
            int    height = Math.Min(img1.Height, img2.Height);
            int    width  = Math.Min(img1.Width, img2.Width);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    dif += Math.Pow(img1[x, y] - img2[x, y], 2);
                }
            }
            return(Math.Sqrt(dif));
        }
Beispiel #6
0
 public static void ImageToFile(GrayscaleFloatImage image, string filename)
 {
     using (Bitmap B = ImageToBitmap(image))
         B.Save(filename);
 }
Beispiel #7
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);
        }
Beispiel #8
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();
            }
        }