Note that there are some explanatory texts on larger screens.

plurals
  1. POTroubles reading pixel values in C#, .Net 4.0
    text
    copied!<p>I am learning C# and I am trying this making my homeworks on image processing examples. I am trying to read the pixel values along an horizontal line in a gray levels jpeg image. Some prints I made inform me that I am reading a Format8bppIndexed image (this was a kind of surprise to me, because my belief was that jpeg images do not use a palette at all). The code to load the image in the proper control in the windows form is something like this:</p> <pre><code> try { myImage = ( Bitmap ) Image.FromFile(imageName); } catch { MessageBox .Show("Unable to load the image" , Text, MessageBoxButtons .OK, MessageBoxIcon .Hand); } pictureBox1.Image = myImage ; </code></pre> <p>Then I try to read the pixel along an arbitrary straight path in the image listing them in a csv file when pressing a button. I assume that, being the image a gray level one, reading the red color is enough (is it? other color components are equal; using the getBrightness seems to me an overkill):</p> <pre><code> cursorStartx = 0; cursorStarty = 256; cursorEndx = myImage.Width; cursorEndy = 256; Color pixel; StreamWriter fs = new StreamWriter( "pixels.csv" , true ); for (var i = 0; i &lt; cursorEndx; i++) { pixel = myImage.GetPixel(i, cursorStarty); fs.WriteLine( String .Format("{0}; {1}; {2}" , i, cursorStarty, pixel.R)); } fs.Close(); </code></pre> <p>When reading the cross section in the file I see values that make no sense at all: they are all multiple of 17 (???):</p> <pre><code>0; 256; 17 1; 256; 0 2; 256; 17 3; 256; 0 4; 256; 17 5; 256; 0 6; 256; 17 7; 256; 0 8; 256; 17 9; 256; 0 10; 256; 17 ............ 66; 256; 17 67; 256; 34 68; 256; 51 69; 256; 68 70; 256; 85 71; 256; 85 72; 256; 102 73; 256; 85 </code></pre> <p>An histogram made in the same program shows clearly distinct count peaks at multiple of 17 (????)</p> <p>Just to perform a reality check, I made something similar in Perl (I have a lot more experience in perl, I am just learning C#) using the module GD and, reading the pixel on the very same path in the same image, I get very different values.</p> <p>First rows of the histogram drwan by imageJ are the following:</p> <pre><code>0 0 1 0 2 1 3 1 4 15 5 81 6 304 7 984 8 2362 9 206144 10 2582 11 1408 12 653 13 451 14 345 15 321 16 277 17 288 </code></pre> <p>a quite different story.</p> <p>Someone could possibly explain to me what horribly stupid kind of error I am doing?</p> <p>Thanks a lot</p> <p>----------------------------------- EDITED LATER --------------------------------------</p> <p>Things are becoming intriguing. Reading the same image using <strong>Aforge</strong> image lab, written in C#, I got the same kind of error: the histogram shows discrete count at pixel values that are multiple of 17. Reading the image with <strong>ImageJ</strong>, written in Java, I get a correct histogram. Now I am trying to figure out if there is a way, using C#, to manage the image reading somehow, in order to be able to get the correct values, or this is a major issue and the alternatives are: to give up or to perform some sort of low level image reading (hard stuff for jpeg format...) </p>
 

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