【问题标题】:Getting different bitmap object from same image file从同一个图像文件中获取不同的位图对象
【发布时间】:2013-11-17 22:38:08
【问题描述】:

我从同一个图像创建位图对象,但最终得到不同的结果。它应该计算图像颜色和图像的偏差,为什么结果不同?

 double test1 = GetStdDev("C:\\temp\\images\\file.jpg");
 Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg");
 double test2 = GetStdDev(img);

 public static double GetStdDev(string imageFileName)
 {
        double total = 0, totalVariance = 0;
        int count = 0;
        double stdDev = 0;

        // First get all the bytes
        using (Bitmap b = new Bitmap(imageFileName))
        {
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
            int stride = bmData.Stride;
            IntPtr Scan0 = bmData.Scan0;
            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b.Width * 3;
                for (int y = 0; y < b.Height; ++y)
                {
                    for (int x = 0; x < b.Width; ++x)
                    {
                        count++;

                        byte blue = p[0];
                        byte green = p[1];
                        byte red = p[2];

                        int pixelValue = Color.FromArgb(0, red, green, blue).ToArgb();
                        total += pixelValue;
                        double avg = total / count;
                        totalVariance += Math.Pow(pixelValue - avg, 2);
                        stdDev = Math.Sqrt(totalVariance / count);

                        p += 3;
                    }
                    p += nOffset;
                }
            }

            b.UnlockBits(bmData);
        }

        return stdDev;
  }

 private static double GetStdDev(Bitmap img)
 {
        double total = 0, totalVariance = 0;
        int count = 0;
        double stdDev = 0;

        // First get all the bytes
        using (Bitmap b = new Bitmap(img))
        {
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
            int stride = bmData.Stride;
            IntPtr Scan0 = bmData.Scan0;
            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b.Width * 3;
                for (int y = 0; y < b.Height; ++y)
                {
                    for (int x = 0; x < b.Width; ++x)
                    {
                        count++;

                        byte blue = p[0];
                        byte green = p[1];
                        byte red = p[2];

                        int pixelValue = Color.FromArgb(0, red, green, blue).ToArgb();
                        total += pixelValue;
                        double avg = total / count;
                        totalVariance += Math.Pow(pixelValue - avg, 2);
                        stdDev = Math.Sqrt(totalVariance / count);

                        p += 3;
                    }
                    p += nOffset;
                }
            }

            b.UnlockBits(bmData);
        }

        return stdDev;
    }

【问题讨论】:

  • 1) 为什么GetStdDev(Bitmap img) 会创建一个新图像而不是迭代给定的图像? 2) 为什么不让GetStdDev(string imageFileName) 加载图像并调用GetStdDev(Bitmap img)
  • 问题是当我调用 GetStdDev(Bitmap img) 得到错误的结果并且无法理解为什么......
  • 我不知道为什么会有差异,但我不明白这两种方法如何产生任何有意义的结果。您确定您了解Color.ToArgb() 返回的内容吗?
  • 我正在尝试使用此功能chinhdo.com/20080910/detect-blank-images,但在我的情况下,我需要使用位图而不是图像文件。

标签: c# .net bitmap bitmapimage


【解决方案1】:

刚刚找到解决方案,是深度复制位图对象的问题。而是使用 new Bitmap(img) 使用 Bitmap img2 = (Bitmap) img.Clone();不知道这是正确的解决方案,但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2016-11-19
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多