Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are pixel values of an image different between reading methods?
    primarykey
    data
    text
    <p>This puzzles me for last two hours. Reading an image file results in different pixel values between imread in Matlab and Image.FromFile in C#?</p> <pre><code>aa=imread('myfile.tif') max(aa(:)) = 248 in matlab </code></pre> <p>In C#</p> <pre><code>var image2Array = imageToByteArray((Bitmap) Image.FromFile("myfile.tif")); byte maxx = 0; foreach(var a in image2Array) { maxx = Math.Max(maxx, a); } //maxx = 255 </code></pre> <p>Futhermore, in Matlab, </p> <pre><code>aa(1,1) = 13, aa(1,2) = 13 </code></pre> <p>but in C#</p> <pre><code>image2Array[0]=17, image2Array[1]=0 </code></pre> <p>They should be the same.</p> <p>BTW, in this case, pixel type is uint8. so there is no dimensional difference. </p> <p>If you ask me how I got byte array from Image, I used <a href="http://msdn.microsoft.com/en-us/library/5ey6h79d%28v=vs.80%29.aspx" rel="nofollow">MSDN document</a> to make this method.</p> <pre><code> 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; } </code></pre> <p>What did I do wrong here? I suspect that they use different reading algorithms because two resulting images look same.</p> <p><strong>UPDATE:</strong></p> <p>I don't think there is anything wrong with what I was doing. I concluded that reading tif as a bitmap was the cause of the problem. To confirm this theory, </p> <ol> <li><p>I displayed the two images and they looked exactly the same. So there is no mistake on my part, I think.</p></li> <li><p>I tried to read the same file with opencv and its pixel values were exactly the same as the ones from matlab. This was surprisingly to me. I would very cautiously use Bitmap in C# from now on.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload