Page 1 of 1

Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-07T07:34:57-07:00
by nhainer
Hi,

I am new to ImageMagick and have been knocking my head against the wall trying to resolve the following problem:

I read a gray scale image into memory:

Code: Select all

 Image image;

 ColorGray grayScale;

 image.read("myimageFile.jpg"); // I have also tried a .bmp version of the same image.
using the following code sample supplied by ImageMagick:

Code: Select all

 map<Color,unsigned long> histogram;

   colorHistogram( &histogram, image );

   std::map<Color,unsigned long>::const_iterator p=histogram.begin();

   while (p != histogram.end()) {
      cout << setw(10) << (int)p->second << ": ("
         << setw(8) << (int)p->first.redQuantum() << ","
         << setw(8) << (int)p->first.greenQuantum() << ","
         << setw(8) << (int)p->first.blueQuantum() << ")"
         << endl;
       p++;
    }
This produces the following histogram:

Code: Select all

 
   9307952: (       0,       0,       0)
     36823: (       1,       1,       1)
     36489: (       2,       2,       2)
     36273: (       3,       3,       3)
     35919: (       4,       4,       4)
     35283: (       5,       5,       5)
     30253: (       6,       6,       6)
     26474: (       7,       7,       7)
     22730: (       8,       8,       8)
     22188: (       9,       9,       9)
     19014: (      10,      10,      10)
     17445: (      11,      11,      11)
     16621: (      12,      12,      12)
     12547: (      13,      13,      13)
     11290: (      14,      14,      14)
     10129: (      15,      15,      15)
     ...
     ...
     ...
Notice the number of zeroes equals: 9,307,952

When I write each value of the image to a text file and count the number of zeroes in it, I get: 9,199,169.

Why is the following code that I wrote not giving the same number of zeroes and the above ImageMagick sample histogram code
(I have verified through a Matlab program that the number of zeroes in this image is in fact 9,307,952)?

Code: Select all

  Image image;

   ColorGray grayScale;

   image.read("myimageFile.jpg");

   FILE * imggreyFD = fopen("./output/imggrey_DSC_0845.txt", "w");

   int cols = image.columns();
   int rows = image.rows();

   for (int j = 0; j < cols; j++) {
      for (int i = 0; i < rows; i++) {
         grayScale = image.pixelColor(i, j);
         fprintf(imggreyFD, "%d\n", (int)grayScale.redQuantum());
         fflush(imggreyFD);
      }
   }

   fclose(imggreyFD);

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-09T21:17:23-07:00
by anthony
The histogram uses a 'binning' technique to generate the histogram. This may be the case of the problem.

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-10T09:04:33-07:00
by nhainer
Anthony,

I see. The Matlab version is just writing out the values to a file, it is not using any binning techniques. This all started with attempting to prove that the image read in by the Matlab program was what was read in by my ImageMagick (Magick++) application. Once the image is read in each pixel value is mathematically transformed to another value. We want to make sure that the initial value of each pixel is the same in both the Magick++ and the Matlab program. Any ideas why writing each value out to a file by both programs produces different results?

Regards,

Neil Hainer

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-10T16:20:19-07:00
by anthony
If you just want to DIY it yourself. You can use TXT: output to output the values one line per pixel.
Matlab can do it without binning as it make no attempt to use less memory, and probably makes use of sparse arrays (arrays in which not all values are filled or allocated)

IM however could have 16 bit color values, requiring a histogram array of at least 64K integers for just one channel! That is very expensive!

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-10T16:21:39-07:00
by anthony
If you just want to DIY it yourself. You can use TXT: output to output the values one line per pixel.
Matlab can do it without binning as it make no attempt to use less memory, and probably makes use of sparse arrays (arrays in which not all values are filled or allocated)

IM however could have 16 bit color values, requiring a histogram array of at least 64K integers for just one channel! That is very expensive when most routines only require a rough idea of the histogram. I believe it currently works with 1024 bins for collecting histogram values.

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-10T16:33:31-07:00
by fmw42
Anthony,

Magick told me that histograms do not have bins (any longer?), if I did not misunderstand. I do believe at one time they were binned to 1024 max bins. For example:

convert rose: -format %c histogram:info:

shows 3019 entries. But you have to sort them yourself. Also if you have say grayvalues only ranging from 0 to 1023, you may not see 1024 entries as any empty value (no counts) will be skipped in the reporting of the histogram.

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-10T18:09:08-07:00
by anthony
It may be , it may not. But you neglecteted three channels.

Check the values and see if they correspond to a 1024 bin values.
With 1024 bin, you can get output histogram of up to 1024^3 colors (that is about a billion color values!)

One method may be to use a greyscale image say 8000 colors and see what histogram you get.

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-10T18:17:36-07:00
by fmw42
This gives 8000 entries in the histogram:

convert -size 1x8000 gradient: -depth 16 -format %c histogram:info:

Re: Can't Write Pixel Values to ASCII File Correctly

Posted: 2011-01-11T09:12:10-07:00
by nhainer
Everyone,

Please help -- I am still at a loss as to why when I write out a text file containing an image in memory with IM and then count the number of zero values
(grep -c "^0$") I get a count of 9,199,169 and when I write the image out using Matlab lab the count of the numbers of zeroes is 9,307,952.

Why is the count not the same for both programs? Am I doing something wrong is my code (originally submitted above)?

Thanks,

Neil