为什么读取方法之间图像的像素值不同?

Tae*_*hin 1 c# matlab image image-processing

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

aa=imread('myfile.tif')

max(aa(:)) = 248 in matlab
Run Code Online (Sandbox Code Playgroud)

在C#中

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

此外,在Matlab中,

aa(1,1) = 13, 
aa(1,2) = 13 
Run Code Online (Sandbox Code Playgroud)

但在C#

image2Array[0]=17,  
image2Array[1]=0
Run Code Online (Sandbox Code Playgroud)

它们应该是一样的.

BTW,在这种情况下,像素类型是uint8.所以没有尺寸差异.

如果你问我如何从Image获得字节数组,我使用MSDN文档来制作这个方法.

    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;
    }
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?我怀疑他们使用不同的阅读算法,因为两个结果图像看起来相同.

更新:

我不认为我在做什么有什么问题.我得出结论,将tif作为位图读取是导致问题的原因.为了证实这个理论,

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

  2. 我尝试用opencv读取相同的文件,其像素值与matlab中的像素值完全相同.这对我来说令人惊讶.从现在开始,我会非常小心地在C#中使用Bitmap.

Dav*_*vid 5

您的imageToByteArray方法确实返回一个字节数组,但您不能假设每个字节都是一个像素.所述PixelFormat确定的像素数据是如何被存储的字节数组英寸

我见过的最好的网站是http://www.bobpowell.net/lockingbits.htm文件,但它看起来不像现在的网站,所以这里是Google缓存版本.

编辑:

如果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...
    }
}
Run Code Online (Sandbox Code Playgroud)