【问题标题】:Why are pixel values of an image different between reading methods?为什么读取方法之间图像的像素值不同?
【发布时间】:2012-09-13 09:22:08
【问题描述】:

这让我困惑了过去两个小时。读取图像文件会导致 Matlab 中的 imread 和 C# 中的 Image.FromFile 之间的像素值不同?

aa=imread('myfile.tif')

max(aa(:)) = 248 in matlab

在 C# 中

var image2Array = imageToByteArray((Bitmap) Image.FromFile("myfile.tif"));
byte maxx = 0;
foreach(var a in image2Array)
{
     maxx = Math.Max(maxx, a);
}
//maxx = 255

此外,在 Matlab 中,

aa(1,1) = 13, 
aa(1,2) = 13 

但在 C# 中

image2Array[0]=17,  
image2Array[1]=0

它们应该是一样的。

顺便说一句,在这种情况下,像素类型是 uint8。所以没有尺寸差异。

如果你问我是如何从 Image 中得到字节数组的,我使用 MSDN document 来制作这个方法。

    public byte[] imageToByteArray(Bitmap bmp)
    {
        // Lock the bitmap's bits.  
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        BitmapData bmpData = bmp.LockBits(
            rect,
            ImageLockMode.ReadWrite,
            bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap. 
        int bytes = Math.Abs(bmpData.Stride)*bmp.Height;
        byte[] rgbValues = new byte[bytes];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
        // Unlock the bits.
        bmp.UnlockBits(bmpData);
        return rgbValues;
    }

我在这里做错了什么?我怀疑他们使用了不同的读取算法,因为两个生成的图像看起来一样。

更新:

我不认为我所做的有什么问题。我得出的结论是,将 tif 作为位图读取是问题的原因。为了证实这一理论,

  1. 我显示了这两个图像,它们看起来完全一样。所以我认为我没有错。

  2. 我尝试使用 opencv 读取相同的文件,其像素值与 matlab 中的完全相同。这让我很意外。从现在开始,我会非常谨慎地在 C# 中使用 Bitmap。

【问题讨论】:

    标签: c# image matlab image-processing


    【解决方案1】:

    你的imageToByteArray 方法确实返回一个字节数组,但你不能假设每个字节都是一个像素。 PixelFormat 决定了像素数据在字节数组中的存储方式。

    我见过的最好的网站是Bob Powell's lockbits page

    如果 PixelFormat 是 Format8bppIndexed,那么这个(未经测试的)代码应该会为您提供每个像素的颜色值。

    var bmp = (Bitmap)Bitmap.FromFile("myfile.tif");
    
    // ******* Begin copying your imageToByteArray method
    // Lock the bitmap's bits.  
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData bmpData = bmp.LockBits(
        rect,
        ImageLockMode.ReadWrite,
        bmp.PixelFormat);
    
    // Get the address of the first line.
    IntPtr ptr = bmpData.Scan0;
    
    // Declare an array to hold the bytes of the bitmap. 
    int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
    byte[] imageData = new byte[bytes];
    
    // Copy the RGB values into the array.
    System.Runtime.InteropServices.Marshal.Copy(ptr, imageData, 0, bytes);
    // Unlock the bits.
    bmp.UnlockBits(bmpData);
    // ******* End copying your imageToByteArray method
    
    // Now loop through each pixel...  The byte array contains extra bytes
    // used for padding so we can't just loop through every byte in the array.
    // This is done by using the Stride property on bmpData.
    for (int y = 0; y < bmpData.Height; y++)
    {
        for (int x = 0; x < bmpData.Width; x++)
        {
            var offset = (y * bmpData.Stride) + x;
    
            // The byte in the image array gives the offset into the palette
            var paletteIndex = imageData[offset];
    
            // Given the offset, find the matching color in the palette
            var color = bmp.Palette.Entries[offset];
    
            // Look at the color value here...
        }
    }
    

    【讨论】:

    • 对此+1。作为一个快速的试金石测试,检查字节数组中的元素数量与 Matlab 报告的图像大小。我敢猜测它更大。
    • 我想我明白这一点。但本例中的 tif 文件是 uint8,与 byte 或 unsigned char 相同。
    • @peakxu 我已经检查过了。两者之间的元素数量相同。
    • @david,PixelFormat 属性的值是多少?
    • @David 这是 PixelFormat.Format8bppIndexed
    【解决方案2】:

    TIFF 有多种格式,您尝试将其作为位图读取。

    我建议使用专有的 TIFF 阅读器来阅读它:Good Tiff library for .NET

    【讨论】:

    • 是的,我正在尝试使用 OpenCV。感谢您确认。
    • 我能够使用 OpenCV 在 Matlab 中复制 imread 中的图像。所以我认为将其作为位图阅读并不是一个好主意,这让我感到惊讶。
    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多