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.
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++;
}
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)
...
...
...
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);